Skip to content

Queues ​

Use a queue when work can finish after the response, such as checking a saved photo. Run a Photo Job walks through sending the work to a local worker and observing a retry.

When to Use Queues ​

Keep work in the request path when you need its result to build the response. Use the default workerpool driver to run jobs in the background within one app process. Use sync when you want a job to execute inline, such as in a test or CLI command.

Choose a shared queue driver when the API and workers run in separate processes. Check the driver's durability if jobs need to survive a restart.

Dispatch on the Default Queue ​

Use make:job to register a job. Without --queue, the generated Queue helper uses default. Call that helper from the controller, command, or service that requests the work. Jobs shows the dispatch code, including how an HTTP producer lets a local worker continue after the response.

bash
forj build

The build wires the new handler into the App. Stop forj dev before running the combined App manually so the HTTP listener and local worker share one process.

bash
forj app

Invoke the controller or command that dispatches the job. Verify the Worker shows the expected response and log for the photo App. A handler test checks payload decoding and delegation separately.

workerpool is process-local. If forj api and forj worker run as separate processes, configure a shared queue driver before expecting one process to receive work dispatched by the other.

The default queue remains available through app.Queue() and the generated queue manager. Add named queues only when work needs separate concurrency, resources, or operational priority.

Generate a Named Queue ​

Add a named queue only when reports need separate capacity:

bash
forj make:queue reports --workers 2

This adds QUEUE_REPORTS_NAME=reports and QUEUE_REPORTS_WORKERS=2 to .env. The next forj build generates Manager.Reports() in internal/queues/accessors_gen.go. No Wire registration changes are needed for the resource itself.

Run forj make:queue without arguments for the resource wizard. Use --name production-report-jobs only when the physical backend name should differ from the logical reports name. The Make Command Reference owns full output, custom env-file, and removal options.

For a new report job on this queue, use forj make:job reports:generate --queue reports. If the job already exists, change its dispatch policy from OnQueue("default") to OnQueue("reports") rather than regenerating application-owned code.

Accessors ​

Use queues.Reports() on the injected queue manager to access the named runtime. The default queue remains available through app.Queue(). If a generated named accessor is missing or misaligned with runtime environment, the App should fail fast.

Use named queues when the App has distinct classes of work. For example, emails, reports, and critical can each have their own generated accessor, backend configuration, worker count, metrics labels, and operational process.

One generated queue resource represents one queue. The resource name is the app-facing queue name, and by default it is also the backend queue name. Use QUEUE_<NAME>_NAME only when the backend queue name must differ.

In a multi-app Project, app code still uses the logical queue name, such as reports. Additional apps prefix backend queue names by default so two apps do not collide on the same backend.

For example, the admin app dispatches to logical queue default while the backend receives admin_default. Application code still says default; GoForj owns the app-aware backend name.

Driver Configuration ​

Compile-time support:

text
QUEUE_SUPPORTED_DRIVERS=workerpool,redis

Runtime selection:

text
QUEUE_DRIVER=workerpool
QUEUE_CRITICAL_DRIVER=redis
QUEUE_NAME=default
QUEUE_WORKERS=30
QUEUE_SHUTDOWN_TIMEOUT=10s

Use sync or workerpool locally. Use durable or broker-backed drivers when production work needs shared state, retries, and independent workers.

Named queues inherit the root queue driver unless they override it:

text
QUEUE_DRIVER=redis
QUEUE_EMAILS_WORKERS=6
QUEUE_REPORTS_WORKERS=2

In this example, both named queues use Redis. emails gets more worker capacity than reports, so it is prioritized by runtime allocation rather than by leaking backend-specific weighting into the main App model.

Use about to verify what the App will run:

bash
forj about

The queue section shows the app queue name, driver, backend queue name, and worker count. For example, reports may show Queue Name: reports, Driver: redis, and Workers: 2.

Dispatching Work ​

Application services usually dispatch jobs through injected job types or queue dependencies.

Do not make HTTP controllers build raw queue payloads when a job type can own the payload shape and dispatch behavior.

The generated job owns payload encoding and its logical queue selection.

Generated jobs dispatch through the Queue manager. For manual dispatch, pass the logical name such as reports to Manager.Dispatch; the manager selects that generated runtime and applies its configured physical backend name, including the app prefix for an additional app.

Named accessors also expose the direct runtime handle for worker lifecycle, readiness, and driver inspection. Supplying a logical queue name to that low-level handle bypasses App namespace translation, so prefer generated jobs or the manager for ordinary dispatch.

Workers ​

With a shared queue driver configured for both producer and worker, start a separate worker process:

bash
forj worker

For an additional app, prefix the command with the app name:

bash
forj admin worker

Without --queue, the worker process starts workers for every configured generated queue. To run only one queue:

bash
forj worker --queue reports

Repeat --queue to run a subset:

bash
forj worker --queue emails --queue reports

With the process-local workerpool driver, run producers and workers together:

bash
forj app

For deployment, supervise the built artifact instead:

bash
./bin/app worker --queue reports

Before release, run ./bin/app about and confirm the logical queue, Driver, backend queue name, and worker count. Then dispatch one safe job through the normal application path. Success means the selected worker runs the registered handler and records the stable job name and outcome; a running process by itself does not prove backend reachability or handler registration.

On SIGTERM, workers drain or stop within QUEUE_SHUTDOWN_TIMEOUT. Keep the process supervisor's stop timeout longer than the queue and App shutdown budgets, and keep handlers idempotent because forced termination can cause redelivery.

Regeneration ​

After changing supported drivers or named queues, use the normal build path:

bash
forj build

During forj dev, an app listed in dev.apps rebuilds automatically. Generation Commands covers focused maintainer workflows.

Next Steps ​