Setup
Set the queue adapter in config/application.rb:
config.active_job.queue_adapter = :cosmonats
That’s it. The Railtie that ships with cosmonats registers the :cosmonats adapter and autoloads config/cosmo.yml.
Outside Rails, require the adapter and set it yourself:
require "cosmo/active_job"
ActiveJob::Base.queue_adapter = Cosmo::ActiveJobAdapter::Adapter.new
Queues
Use queue_as exactly as you would with any other ActiveJob backend:
class ProcessPaymentJob < ApplicationJob
queue_as :critical
def perform(order_id)
Order.find(order_id).charge!
end
end
Each queue name maps to a NATS stream of the same name. Declare every stream you plan to use in your config/cosmo.yml:
stream_config: &stream_config
storage: file
retention: workqueue
duplicate_window: 120
discard: new
allow_direct: true
subjects: ["jobs.%{name}.>"]
consumer_config: &consumer_config
ack_policy: explicit
max_deliver: 30
max_ack_pending: 20
ack_wait: 60
subject: jobs.%{name}.>
setup:
jobs:
default: { <<: *stream_config }
critical: { <<: *stream_config }
mailers: { <<: *stream_config }
consumers:
jobs:
default: { <<: *consumer_config, priority: 15 }
critical: { <<: *consumer_config, priority: 50 }
mailers: { <<: *consumer_config, priority: 10 }
The scheduled and dead streams are Cosmo’s own: cosmo --setup creates them, and they’re tuned in
Cosmo.configure rather than in cosmo.yml (see Configuration).
Provision streams (safe to run on every deploy):
cosmo -S
Delayed jobs
set(wait:) and set(wait_until:) are fully supported via the built-in scheduled stream, with no extra
configuration:
ProcessPaymentJob.set(wait: 5.minutes).perform_later(order.id)
ProcessPaymentJob.set(wait_until: Date.tomorrow.noon).perform_later(order.id)
Cosmo-specific options
cosmo_options is available on every ActiveJob class. Use it to set NATS-level retry and dead-letter behavior:
class ProcessPaymentJob < ApplicationJob
queue_as :critical
cosmo_options retry: 5, dead: true
def perform(order_id)
Order.find(order_id).charge!
end
end
| Option | Default | Description |
|---|---|---|
retry: |
3 |
How many times Cosmonats retries the job at the NATS level before giving up |
dead: |
true |
Move the message to the dead-letter stream after retries are exhausted |
stream: |
(queue_name) | Override the NATS stream regardless of queue_as |
retry_in: |
(backoff) | ->(count, exception) { seconds }, see Retry delay |
Options are inherited and can be overridden in subclasses:
class UrgentPaymentJob < ProcessPaymentJob
cosmo_options retry: 1 # inherits dead: true, overrides retry
end
retry_onvscosmo_options retry:— these are two independent retry mechanisms at different layers:
retry_onis handled entirely insideExecutor#performby ActiveJob before the method returns. From Cosmonats’ perspective the job succeeded — the message is acked and removed from the stream. ActiveJob re-enqueues a brand new message for each attempt.cosmo_options retry:tells Cosmonats how many times to redeliver the same NATS message whenExecutor#performraises an unhandled exception (i.e. one not caught byretry_on).In practice: use
retry_onfor expected, recoverable errors (network blips, rate limits) andcosmo_options retry:as the last-resort safety net for anything unexpected. Avoid setting both for the same error class, or you will get retries multiplied across both layers.
Cosmo::Batchand ActiveJob — the ActiveJob adapter enqueues through its ownJob::Dataconstruction, notCosmo::Job#perform, soperform_latercalls made inside abatch.jobs { }block are not currently tracked by the batch. UseCosmo::Job-based jobs (perform_async/perform_at/perform_in) for anything that needs to participate in a batch.
Running the worker
The Cosmo worker is a separate process. In a Rails app the CLI auto-discovers
config/boot.rb and config/environment.rb:
bundle exec cosmo -C config/cosmo.yml -c 10 jobs
| Flag | Description |
|---|---|
-C config/cosmo.yml |
Cosmo config file |
-c 10 |
Worker threads |
jobs |
Run job processor only (omit to also run stream processors) |
Procfile:
web: bundle exec puma -C config/puma.rb
worker: bundle exec cosmo -C config/cosmo.yml -c 5 jobs
Testing
Use the built-in :test adapter in your test suite:
# spec/rails_helper.rb (or test/test_helper.rb)
config.active_job.queue_adapter = :test
With the test adapter, perform_later enqueues the job into an in-memory queue
that you can assert against:
expect { ProcessPaymentJob.perform_later(order.id) }
.to have_enqueued_job(ProcessPaymentJob).with(order.id)
To run job logic synchronously without any queue:
ProcessPaymentJob.perform_now(order.id)
Dead-letter queue (DLQ)
When a job exhausts all retries, Cosmo moves it to the dead stream under the
subject jobs.dead.cosmo-active_job_adapter-executor. Inspect failed jobs with
the NATS CLI or the built-in monitoring UI:
nats stream view dead