diff --git a/README.markdown b/README.markdown index 0a95f7bb9..e3ea2b8da 100644 --- a/README.markdown +++ b/README.markdown @@ -265,6 +265,15 @@ worker process. Use the PIDFILE option for easy access to the PID: $ PIDFILE=./resque.pid QUEUE=file_serve rake environment resque:work +### Running in the background + +(Only supported with ruby >= 1.9). There are scenarios where it's helpful for +the resque worker to run itself in the background (usually in combination with +PIDFILE). Use the BACKGROUND option so that rake will return as soon as the +worker is started. + + $ PIDFILE=./resque.pid BACKGROUND=yes QUEUE=file_serve \ + rake environment resque:work ### Priorities and Queue Lists diff --git a/lib/resque/tasks.rb b/lib/resque/tasks.rb index 1e2e07942..4abf7ccc1 100644 --- a/lib/resque/tasks.rb +++ b/lib/resque/tasks.rb @@ -18,6 +18,13 @@ abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work" end + if ENV['BACKGROUND'] + unless Process.respond_to?('daemon') + abort "env var BACKGROUND is set, which requires ruby >= 1.9" + end + Process.daemon(true) + end + if ENV['PIDFILE'] File.open(ENV['PIDFILE'], 'w') { |f| f << worker.pid } end diff --git a/lib/resque/worker.rb b/lib/resque/worker.rb index 32c450d99..02d3ce98f 100644 --- a/lib/resque/worker.rb +++ b/lib/resque/worker.rb @@ -480,7 +480,7 @@ def hostname # Returns Integer PID of running worker def pid - @pid ||= to_s.split(":")[1].to_i + Process.pid end # Returns an Array of string pids of all the other workers on this