简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM