Ali Khan
Ali Khan

Reputation: 59

How to run 'resque:work' rake task from rails application?

Having followed the rails-cast (http://railscasts.com/episodes/271-resque?view=comments), I was able to setup my rails application similarly with worker and rake tasks.

However, as specified in the rails-cast, once I start my rails server, I have to issue the following command in a separate terminal window:

TERM_CHILD=1 QUEUES=* rake resque:work

This gets me up-and-running and I have no issues. However, instead of firing off a rake command on terminal (or writing a startup shell script), I would like to issue this terminal command (TERM_CHILD=1 QUEUES=* rake resque:work) from Rails itself when the application starts up so its available during the duration of the application's uptime on the server.

I am in the process of learning Rails framework so I would greatly appreciate if you can give me some help and let me know how/where to place any code snippet you may provide me with.

Thank you very much in advance.

Here are my environment specs:

Upvotes: 1

Views: 1193

Answers (1)

probablykabari
probablykabari

Reputation: 1389

That approach would make it difficult to turn off your resque workers. I recommend using a common gem called foreman to handle this. I'll let you look a the documentation, but your Procfile would look something like:

web: rails s -p $PORT
resque: TERM_CHILD=1 QUEUES=* rake resque:work

And you would be able to start both rails and resque by just running the foreman command in console. That will also allow you to exit out of both easily.

Updated per OP's message:

require 'resque/tasks'
ENV['TERM_CHILD'] = 1
ENV['QUEUES'] = '*'
Thread.new { Rake::Task["resque:work"] }

The Thread.new is there because it would otherwise cause the rest of the app to never load.

Upvotes: 1

Related Questions