Skip to content

Commit 8d92f03

Browse files
committed
Merge remote-tracking branch 'chrisleishman/daemonize'
2 parents 05b579b + dcfbe36 commit 8d92f03

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ worker process. Use the PIDFILE option for easy access to the PID:
265265

266266
$ PIDFILE=./resque.pid QUEUE=file_serve rake environment resque:work
267267

268+
### Running in the background
269+
270+
(Only supported with ruby >= 1.9). There are scenarios where it's helpful for
271+
the resque worker to run itself in the background (usually in combination with
272+
PIDFILE). Use the BACKGROUND option so that rake will return as soon as the
273+
worker is started.
274+
275+
$ PIDFILE=./resque.pid BACKGROUND=yes QUEUE=file_serve \
276+
rake environment resque:work
268277

269278
### Priorities and Queue Lists
270279

lib/resque/tasks.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work"
1919
end
2020

21+
if ENV['BACKGROUND']
22+
unless Process.respond_to?('daemon')
23+
abort "env var BACKGROUND is set, which requires ruby >= 1.9"
24+
end
25+
Process.daemon(true)
26+
end
27+
2128
if ENV['PIDFILE']
2229
File.open(ENV['PIDFILE'], 'w') { |f| f << worker.pid }
2330
end

lib/resque/worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def hostname
480480

481481
# Returns Integer PID of running worker
482482
def pid
483-
@pid ||= to_s.split(":")[1].to_i
483+
Process.pid
484484
end
485485

486486
# Returns an Array of string pids of all the other workers on this

0 commit comments

Comments
 (0)