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

前端 未结 7 1875
暖寄归人
暖寄归人 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 12:48

    You should definitely check out the following Railscasts:

    • http://railscasts.com/episodes/127-rake-in-background
    • http://railscasts.com/episodes/128-starling-and-workling
    • http://railscasts.com/episodes/129-custom-daemon
    • http://railscasts.com/episodes/366-sidekiq

    They explain how to run background processes in Rails in every possible way (with or without a queue ...)

    0 讨论(0)
  • 2020-12-07 12:59

    Start a separate process, which is probably most easily done with system, prepending a 'nohup' and appending an '&' to the end of the command you pass it. (Make sure the command is just one string argument, not a list of arguments.)

    There are several reasons you want to do it this way, rather than, say, trying to use threads:

    1. Ruby's threads can be a bit tricky when it comes to doing I/O; you have to take care that some things you do don't cause the entire process to block.

    2. If you run a program with a different name, it's easily identifiable in 'ps', so you don't accidently think it's a FastCGI back-end gone wild or something, and kill it.

    Really, the process you start should be "deamonized," see the Daemonize class for help.

    0 讨论(0)
  • 2020-12-07 12:59

    I like to use backgroundrb, its nice it allows you to communicate to it during long processes. So you can have status updates in your rails app

    0 讨论(0)
  • 2020-12-07 13:02

    What about:

    def background_check
       exec("script/runner check_for_record_in_www.rb #{self.username}") if fork == nil
    end
    

    The program "check_for_record_in_www.rb" will then run in another process and will have access to ActiveRecord, being able to access the database.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 13:08

    I think spawn is a great way to fork your process, do some processing in background, and show user just some confirmation that this processing was started.

    0 讨论(0)
提交回复
热议问题