Docs Active Job
Integrations

Active Job

Setup

Set the queue adapter in config/application.rb:

ruby
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:

ruby
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:

ruby
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:

yaml
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):

bash
cosmo -S

Delayed jobs

set(wait:) and set(wait_until:) are fully supported via the built-in scheduled stream, with no extra configuration:

ruby
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:

ruby
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:

ruby
class UrgentPaymentJob < ProcessPaymentJob
  cosmo_options retry: 1   # inherits dead: true, overrides retry
end

retry_on vs cosmo_options retry: — these are two independent retry mechanisms at different layers:

  • retry_on is handled entirely inside Executor#perform by 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 when Executor#perform raises an unhandled exception (i.e. one not caught by retry_on).

In practice: use retry_on for expected, recoverable errors (network blips, rate limits) and cosmo_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::Batch and ActiveJob — the ActiveJob adapter enqueues through its own Job::Data construction, not Cosmo::Job#perform, so perform_later calls made inside a batch.jobs { } block are not currently tracked by the batch. Use Cosmo::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:

bash
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:

text
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:

ruby
# 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:

ruby
expect { ProcessPaymentJob.perform_later(order.id) }
  .to have_enqueued_job(ProcessPaymentJob).with(order.id)

To run job logic synchronously without any queue:

ruby
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:

bash
nats stream view dead

Edit this page on GitHub ↗