Search code examples
rubyresque

How do I start Redis, a Ruby script, and Resque worker as one daemon


I am building a Slack Bot that integrates with some internal tooling.

I see this solution: Trying to start redis and resque scheduler within a rake task

But is there a cleaner way to do this?

Currently I am starting everything in yet another ruby script start.rb that is essentially this:

system('nohup redis-server &')
system('nohup bundle exec ruby slack_bot.rb &')
system("nohup bundle exec rake resque:workers QUEUE=* COUNT=#{$workers} &")

Generally this is fine, but for some reason the slack_bot hangs sometimes. To restart, I have to kill that one process and restart it. I would prefer to have an easy way to restart everything together just to be clean, essentially daemonizing the processes. In a perfect world I could run this all as a system service and start/stop with service bot start or /etc/init.d or have a single process to start/kill.


Solution

  • I guess you are looking for foreman gem. With foreman you can declare various processes that are needed to run your application using a Procfile.

    So, the solution can be:

    1 gem install foreman

    2 create Procfile in your project directory. Put into the file:

    redis: redis-server  
    slack_bot: bundle exec ruby slack_bot.rb  
    workers: bundle exec rake resque:workers QUEUE=* COUNT=*
    

    2.5 Now, you can use everything together through foreman:

    $ foreman start  
    

    3 It is also possible to export foreman to system service.

    $ sudo foreman export --app app_name --user your_user_name systemd /etc/systemd/system/  
    

    3.5 Launch the service

    $ systemctl start app_name.target
    

    Foreman detailed documentation available here.