From 60b157d164a8f056450fb96059666b93215c3ebb Mon Sep 17 00:00:00 2001 From: Chris Leishman Date: Fri, 19 Aug 2011 14:11:45 +0200 Subject: [PATCH 1/3] Daemonizes when BACKGROUND environment var is set --- lib/resque/tasks.rb | 4 ++++ lib/resque/worker.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/resque/tasks.rb b/lib/resque/tasks.rb index 086bd8b1a..1d952b3a9 100644 --- a/lib/resque/tasks.rb +++ b/lib/resque/tasks.rb @@ -18,6 +18,10 @@ abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work" end + if ENV['BACKGROUND'] + 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 ebb057fd2..dd762fb5e 100644 --- a/lib/resque/worker.rb +++ b/lib/resque/worker.rb @@ -470,7 +470,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 From 39d51cb19283e2a1f25820a069c07fe2df3e5e97 Mon Sep 17 00:00:00 2001 From: Chris Leishman Date: Fri, 19 Aug 2011 16:05:20 +0200 Subject: [PATCH 2/3] Added documentation of daemonization --- README.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.markdown b/README.markdown index 66407daa5..041f03686 100644 --- a/README.markdown +++ b/README.markdown @@ -265,6 +265,14 @@ 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 + +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 From dcfbe365c256bb5705739739b569f62fa7ae52a0 Mon Sep 17 00:00:00 2001 From: Chris Leishman Date: Wed, 24 Aug 2011 11:50:53 +0200 Subject: [PATCH 3/3] Add a notes regarding ruby >= 1.9 requirement for BACKGROUND --- README.markdown | 7 ++++--- lib/resque/tasks.rb | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index 041f03686..ebfca8ff1 100644 --- a/README.markdown +++ b/README.markdown @@ -267,9 +267,10 @@ worker process. Use the PIDFILE option for easy access to the PID: ### Running in the background -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. +(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 diff --git a/lib/resque/tasks.rb b/lib/resque/tasks.rb index 1d952b3a9..da4fb9437 100644 --- a/lib/resque/tasks.rb +++ b/lib/resque/tasks.rb @@ -19,6 +19,9 @@ 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