Ruby on Rails: How to run things in the background?

前端 未结 7 1919
暖寄归人
暖寄归人 2020-12-07 12:32

When a new resource is created and it needs to do some lengthy processing before the resource is ready, how do I send that processing away into the

7条回答
  •  情歌与酒
    2020-12-07 13:03

    I've just been experimenting with the 'delayed_job' gem because it works with the Heroku hosting platform and it was ridiculously easy to setup!!

    Add gem to Gemfile, bundle install, rails g delayed_job, rake db:migrate Then start a queue handler with;

    RAILS_ENV=production script/delayed_job start
    

    Where you have a method call which is your lengthy process i.e

    company.send_mail_to_all_users
    

    you change it to;

    company.delay.send_mail_to_all_users
    

    Check the full docs on github: https://github.com/collectiveidea/delayed_job

提交回复
热议问题