How to invoke job from the queue manually in Sidekiq

Ly Channa
2 min readJul 9, 2024

--

The Delayed job gem has aninvoke_job method that is a straightforward way to manually run a Delayed::Job from the queue. This method is specifically designed to run a job directly from the Delayed::Job instance. Here's how you can do it:

# Start Rails console
rails console

# Get the job to run
job = Delayed::Job.first

# Manually invoke the job's perform method
job.invoke_job

however, in Sidekiq to manually pick a job from the Sidekiq queue and run it, you’ll need to perform a few steps. This process involves retrieving the job from the Redis-backed Sidekiq queue and executing its logic manually. Here’s a step-by-step guide:

1. Install Redis CLI

Ensure you have Redis CLI installed to interact with the Redis database used by Sidekiq.

2. Find the Job in the Queue

Sidekiq jobs are stored in Redis. You can use the Redis CLI to inspect the contents of the Sidekiq queue. The default queue name in Sidekiq is queue:default, but your application might use custom queue names.

Open a terminal and run:

redis-cli

Once in the Redis CLI, you can list the queues:

keys queue:*

or if you wanna see the jobs in a specific queue (e.g., queue:default):

LRANGE queue:default 0 -1

This command will list all jobs in the default queue. Each job will be displayed in a JSON format.

3. Extract the Job

Copy the JSON data for the job you want to run manually. Here is an example of what a job might look like:

{
"class": "MyWorker",
"args": [123, 456],
"retry": true,
"queue": "default",
"jid": "abcdef123456",
"created_at": 1625235553.12345,
"enqueued_at": 1625235553.12345
}

4. Manually Run the Job

Open your Rails console:

rails console

In the Rails console, you can deserialize the job and run it manually. Here is a method to do this:

# Paste the JSON string of the job here
job_json = '{
"class": "MyWorker",
"args": [123, 456],
"retry": true,
"queue": "default",
"jid": "abcdef123456",
"created_at": 1625235553.12345,
"enqueued_at": 1625235553.12345
}'
job_data = JSON.parse(job_json)
worker_class = job_data["class"].constantize
args = job_data["args"]

# Manually perform the job
worker_class.new.perform(*args)

This script will manually execute the job logic with the provided arguments.

Notes

  • Running jobs this way bypasses Sidekiq’s normal execution flow, including retries and middleware. Use this method only for debugging or exceptional cases.
  • Be cautious when manually manipulating jobs from the queue to avoid potential data inconsistency or job duplication issues.

By following these steps, you can pick a job from the Sidekiq queue and run it manually, allowing for detailed inspection and debugging of job execution.

--

--

Ly Channa

Highly skilled: REST API, OAuth2, OpenIDConnect, SSO, TDD, RubyOnRails, CI/CD, Infrastruct as Code, AWS.