Skip to content

feat(server): add BullMQ queue and worker with Redis#1

Closed
JoaoPixelCode wants to merge 3 commits intomainfrom
feature/message-list
Closed

feat(server): add BullMQ queue and worker with Redis#1
JoaoPixelCode wants to merge 3 commits intomainfrom
feature/message-list

Conversation

@JoaoPixelCode
Copy link
Collaborator

  • Adicionado Redis no docker-compose
  • Criado queue.ts com a fila de mensagens
  • Criado worker.ts para processar as mensagens
  • Adicionado rota /api/queue no servidor
  • Adicionado REDIS_URL nas variáveis de ambiente

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces Redis-backed background processing to the server using BullMQ, adding a queue, a worker to consume jobs, and an API endpoint to enqueue messages.

Changes:

  • Add REDIS_URL to server environment validation and introduce BullMQ as a dependency.
  • Create chatQueue (queue.ts) and chatWorker (worker.ts) wired to Redis.
  • Add Redis service to docker-compose.yml and expose a new POST /api/queue route to enqueue jobs.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/env/src/server.ts Adds REDIS_URL to required server env vars.
package.json Adds BullMQ dependency at repo root.
package-lock.json Locks BullMQ and transitive dependencies.
docker-compose.yml Adds Redis service and a (currently unused) redis volume declaration.
apps/server/src/queue.ts Defines the BullMQ queue instance for chat jobs.
apps/server/src/worker.ts Defines the BullMQ worker to process chat jobs.
apps/server/src/index.ts Wires queue route and starts worker via side-effect import.
Comments suppressed due to low confidence (5)

apps/server/src/index.ts:116

  • /api/queue destructures message directly from req.body, which will throw if the body is missing/invalid (e.g., empty request). It also enqueues potentially non-string/blank messages. Align validation with /api/chat (treat body as unknown, validate message is a non-empty string, and return 400 on invalid input).
app.post("/api/queue", async (req, res) => {
	const { message } = req.body;

	await chatQueue.add("chat-message", { message });

apps/server/src/index.ts:5

  • Importing ./worker from the main server entrypoint starts the BullMQ worker inside every API process. In production this can unintentionally scale workers with web replicas (and makes API availability depend on Redis/worker connectivity). Consider running the worker in a separate process/entrypoint, or gating worker startup behind an env flag (e.g., RUN_WORKER=true).
import { chatQueue } from "./queue";
import "./worker";

apps/server/src/worker.ts:19

  • chatWorker is created at module load and never closed. Add graceful shutdown handling (SIGINT/SIGTERM) to call chatWorker.close() (and any related Redis connections) so containers can terminate cleanly and not hang during deploys/restarts.
export const chatWorker = new Worker(
	"chat",
	async (job) => {
		console.log(`Processando mensagem: ${job.data.message}`);

		// Aqui vai entrar futuramente:
		// 1. Buscar no banco vetorial (RAG)
		// 2. Mandar pra IA
		// 3. Retornar a resposta
	},
	{
		connection: {
			url: env.REDIS_URL,
		},
	},
);

docker-compose.yml:81

  • ifinho-redis-data volume is declared but the redis service does not mount it, so Redis data will be ephemeral. Either mount the volume under the Redis service (e.g., /data) or remove the unused volume declaration to avoid confusion.
volumes:
  ifinho-postgres-data:
  ifinho-ollama-data:
  ifinho-redis-data:

package.json:26

  • bullmq is added to the root package.json, but it is only imported by apps/server. To keep workspace dependency graphs accurate (and avoid relying on hoisting), add bullmq to apps/server/package.json dependencies and remove it from the root if nothing else uses it.
		"@ifinho/env": "*",
		"bullmq": "^5.70.1",
		"dotenv": "^17.2.2",

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +33 to +40
redis:
container_name: ifinho-redis
image: redis:7-alpine
restart: unless-stopped
ports:
- "6379:6379"
networks:
- ifinho-network
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis was added, but the server container is not being given REDIS_URL and does not depend on redis. Since @ifinho/env/server now requires REDIS_URL, the server will fail to start under this compose file. Add REDIS_URL to the server.environment (e.g., redis://ifinho-redis:6379) and consider adding a depends_on/healthcheck for Redis.

Copilot uses AI. Check for mistakes.
@patrickluzdev
Copy link
Owner

o BullMQ não suporta postgres apenas redis, então por conta disso não vamos usar ele e sim o pg-boss para essa parte de jobs e message queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants