# vivek-raj/node-resque architecture

> Architecture diagram of the GitHub repository vivek-raj/node-resque, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vivek-raj/node-resque
- Repository: https://github.com/vivek-raj/node-resque

## Overview

1. **Project role**
   - This repository is a small Node.js implementation of a Resque-compatible job queue client/worker.
   - It is designed to let Node code enqueue jobs into Redis and run long-lived worker processes that consume those jobs.

2. **Main runtime boundary**
   - The system has two execution modes: a **producer** side that enqueues jobs, and a **worker** side that executes them.
   - Both modes communicate through **Redis**, which is the only durable system boundary in the architecture.

3. **Core library**
   - `lib/resque.js` is the central module.
   - It likely exposes the connection/setup API described in the README: `connect(...)`, `enqueue(...)`, namespace selection, Redis client injection, and stale-worker cleanup.
   - This is the shared service layer used by both application code and the worker CLI.

4. **Worker entrypoint**
   - `bin/node-resque-worker` is the process launcher for background workers.
   - It reads environment configuration such as `WORKER`, `QUEUE`, and optional `REDIS`, then starts a worker loop around the task definitions in the specified worker file.

5. **Job definition model**
   - Worker code is not hardcoded into the library; instead, the worker file exports named task functions.
   - Each function acts as a job handler and receives a `this` context representing the current job, including success/failure completion callbacks.

6. **Asynchronous execution flow**
   - The worker does not block the queue while a job runs.
   - Instead, job handlers call `succeed()` or `fail(error)` when asynchronous work finishes, allowing the worker to move on to the next job only after completion.

7. **Queueing flow**
   - Application code uses the library as a client: connect to Redis, then `enqueue(queue, method, payload)`.
   - Jobs are placed onto a named queue, and workers pull from those queues by name, enabling decoupled producers and consumers.

8. **Redis usage**
   - Redis stores the queue state, worker state, and whatever metadata is needed for Resque-style compatibility.
   - The README notes a default namespace of `resque:` with support for custom namespaces and for supplying an existing Redis client instead of creating a new one.

9. **Operational lifecycle**
   - Worker processes support graceful shutdown behavior.
   - A `QUIT` signal finishes the current job before exit, while `KILL` terminates more abruptly.
   - Stale worker cleanup happens at startup of the worker process, which keeps Redis worker metadata consistent.

10. **Compatibility goal**
   - A key architectural intent is compatibility with the original Resque ecosystem, including shared queue semantics and the Resque web monitoring model.
   - That makes this repository a bridge between Node.js workers and a broader Resque/Redis job infrastructure.

11. **Packaging structure**
   - The repo is minimal: one library file, one executable script, and package metadata.
   - That suggests a thin wrapper around Redis-based queue semantics rather than a large framework with internal subsystems.

12. **External dependencies and tooling**
   - The runtime dependency is Node.js plus the Redis client library (`redis` / `node_redis`).
   - No database, web server, or messaging broker is involved beyond Redis.

13. **Architecture diagram guidance**
   - A good diagram would show:
     - Node application code on the left enqueuing jobs
     - Redis in the middle as the shared queue/state backend
     - `node-resque-worker` processes on the right consuming jobs
     - Optional monitoring/administration via Resque tooling
   - The main control points are the library API, the worker CLI, and Redis queue names/namespaces.

Main entry points: [`bin/node-resque-worker`](https://github.com/vivek-raj/node-resque/tree/master/bin/node-resque-worker) (Worker CLI), [`lib/resque.js`](https://github.com/vivek-raj/node-resque/blob/master/lib/resque.js) (Enqueue API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_producer["Producer side"]
  node_app["App code<br/>producer"]
  node_resque_lib["Resque lib<br/>client/workers<br/>[resque.js]"]
  node_enqueue_api["Enqueue API<br/>client api<br/>[resque.js]"]
end

subgraph group_backend["Shared backend"]
  node_redis[("Redis<br/>queue store")]
  node_namespace["Namespace<br/>key prefix"]
  node_queue_state["Queue state<br/>job data"]
  node_worker_meta["Worker state<br/>runtime state"]
end

subgraph group_worker["Worker side"]
  node_worker_bin["Worker CLI<br/>entrypoint"]
  node_worker_loop(("Worker loop<br/>runtime"))
  node_job_defs["Job defs<br/>handlers"]
  node_job_ctx["Job context<br/>execution context"]
  node_shutdown{{"Shutdown<br/>signal handling"}}
  node_cleanup["Stale cleanup<br/>lifecycle"]
end

node_app -->|"uses"| node_resque_lib
node_resque_lib -->|"exposes"| node_enqueue_api
node_enqueue_api -->|"writes"| node_redis
node_enqueue_api -->|"prefixes"| node_namespace
node_redis -->|"contains"| node_queue_state
node_redis -->|"contains"| node_worker_meta
node_worker_bin -->|"loads"| node_resque_lib
node_worker_bin -->|"starts"| node_cleanup
node_worker_bin -->|"launches"| node_worker_loop
node_worker_loop -->|"reads"| node_redis
node_worker_loop -->|"dispatches to"| node_job_defs
node_job_defs -->|"receives"| node_job_ctx
node_job_ctx -->|"reports"| node_worker_loop
node_worker_loop -->|"handles"| node_shutdown
node_cleanup -->|"prunes"| node_worker_meta

click node_resque_lib "https://github.com/vivek-raj/node-resque/blob/master/lib/resque.js"
click node_enqueue_api "https://github.com/vivek-raj/node-resque/blob/master/lib/resque.js"
click node_worker_bin "https://github.com/vivek-raj/node-resque/tree/master/bin/node-resque-worker"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_resque_lib,node_enqueue_api toneBlue
class node_redis,node_namespace,node_queue_state,node_worker_meta toneAmber
class node_worker_bin,node_worker_loop,node_job_defs,node_job_ctx,node_shutdown,node_cleanup toneMint
```

## Components

### Producer side

Application code that enqueues jobs

- **App code**: producer · Node.js application using the client API to enqueue work
- [Resque lib](https://github.com/vivek-raj/node-resque/blob/master/lib/resque.js): client/workers · Core library that connects to Redis, enqueues jobs, and manages worker compatibility · `lib/resque.js`
- [Enqueue API](https://github.com/vivek-raj/node-resque/blob/master/lib/resque.js): client api · connect() and enqueue() entry points used by producers · `lib/resque.js`

### Shared backend

Redis-backed queue and worker state

- **Redis**: queue store · Durable backend for queues, worker state, and namespace-scoped metadata
- **Namespace**: key prefix · Default resque: prefix with support for custom Redis namespaces
- **Queue state**: job data · Named queues and queued job payloads shared between producers and workers
- **Worker state**: runtime state · Worker registry, stale-worker tracking, and Resque-compatible metadata

### Worker side

Long-lived processes that consume and execute jobs

- [Worker CLI](https://github.com/vivek-raj/node-resque/tree/master/bin/node-resque-worker): entrypoint · bin/node-resque-worker process launcher that reads environment configuration · `bin/node-resque-worker`
- **Worker loop**: runtime · Long-lived consumer loop that pulls jobs from a named queue
- **Job defs**: handlers · Worker-file exports of named task functions that handle each job
- **Job context**: execution context · this-bound job object passed to handlers with succeed() and fail() callbacks
- **Shutdown**: signal handling · QUIT waits for the current job; KILL stops more abruptly
- **Stale cleanup**: lifecycle · Startup cleanup that removes stale worker metadata

## Connections

- App code → Resque lib: uses · Application code calls into the shared client/library layer
- Resque lib → Enqueue API: exposes · Library provides connect() and enqueue() as the primary producer API
- Enqueue API → Redis: writes · Queued jobs are stored in Redis
- Enqueue API → Namespace: prefixes · Queue keys and metadata are namespaced in Redis
- Redis → Queue state: contains · Redis holds queue contents and payloads
- Redis → Worker state: contains · Redis also tracks workers and compatibility metadata
- Worker CLI → Resque lib: loads · CLI bootstraps the shared library
- Worker CLI → Stale cleanup: starts · Worker startup performs stale-worker cleanup
- Worker CLI → Worker loop: launches · Entry point starts the long-lived consumer loop
- Worker loop → Redis: reads · Worker consumes jobs from Redis queues
- Worker loop → Job defs: dispatches to · Jobs are routed to exported task functions
- Job defs → Job context: receives · Handlers run with a job object as their execution context
- Job context → Worker loop: reports · Handlers signal completion or failure back to the worker
- Worker loop → Shutdown: handles · Worker responds to QUIT and KILL lifecycle signals
- Stale cleanup → Worker state: prunes · Startup cleanup keeps worker metadata consistent
