user2980139
user2980139

Reputation: 95

How does Redis work with Rails and Sidekiq

Problem: need to send e-mails from Rails asynchronously. Environment: Windows 7, Ruby 2.0, Rails 4.1, Sidekiq, Redis

After setting everything up, starting Sidekiq and starting Redis, I can see the mail request queued to Redis through the monitor:

1414256204.699674 "exec"
1414256204.710675 "multi"
1414256204.710675 "sadd" "queues" "default"
1414256204.710675 "lpush" "queue:default" "{\"retry\":true,\"queue\":\"default\",\"class\":\"Sidekiq::Extensions::DelayedMailer\",\"args\":[\"---\\n- !ruby/class 'UserMailer'\\n- :async_reminder\\n- - 673\\n\"],\"jid\":\"d4024c0c219201e5d1649c54\",\"enqueued_at\":1414256204.709674}"

But the mailer method never seems to get executed. The mail doesn't get sent and none of the log messages show up.

How does Redis know to execute the job on the queue and does something else need to be setup in the environment for it to know where the application resides?

Is delayed_job a better solution?

I started redis in one window, bundle exec sidekiq in another window, and rails server in a third window.

How does an item on the redis queue get picked up and processed? Is sidekiq both putting things on the redis queue and checking to see if something was added that needs to be processed?

Upvotes: 5

Views: 2937

Answers (1)

Ilya Vassilevsky
Ilya Vassilevsky

Reputation: 1001

Redis is used just for storage. It stores jobs to be done. It does not execute anything. DelayedJob uses your database for job storage instead of Redis.

Rails process pushes new jobs to Redis.

Sidekiq process pops jobs from Redis and executes them.

In your MONITOR output, you should see LPUSH commands when Rails sends mail. You should also see BRPOP commands from Sidekiq.

You need to make sure that both Rails and Sidekiq processes use the same Redis server, database number, and namespace (if any). It's a frequent problem that they don't.

Upvotes: 6

Related Questions