Class: Delayed::Daemon
- Inherits:
-
Object
- Object
- Delayed::Daemon
- Defined in:
- lib/delayed/daemon.rb
Overview
Daemon controls the parent proces that runs the Pool and monitors the Worker processes.
Instance Attribute Summary collapse
-
#pid_folder ⇒ Object
readonly
Returns the value of attribute pid_folder.
Instance Method Summary collapse
- #daemonize! ⇒ Object
- #daemonized? ⇒ Boolean
-
#initialize(pid_folder) ⇒ Daemon
constructor
A new instance of Daemon.
- #pid ⇒ Object
- #pid_file ⇒ Object
- #status(print: true, pid: self.pid) ⇒ Object
-
#stop(kill: false, pid: self.pid) ⇒ Object
stop the currently running daemon (not this current process, the one in the pid_file).
- #wait(kill) ⇒ Object
Constructor Details
#initialize(pid_folder) ⇒ Daemon
Returns a new instance of Daemon.
10 11 12 |
# File 'lib/delayed/daemon.rb', line 10 def initialize(pid_folder) @pid_folder = pid_folder end |
Instance Attribute Details
#pid_folder ⇒ Object (readonly)
Returns the value of attribute pid_folder.
8 9 10 |
# File 'lib/delayed/daemon.rb', line 8 def pid_folder @pid_folder end |
Instance Method Details
#daemonize! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/delayed/daemon.rb', line 25 def daemonize! FileUtils.mkdir_p(pid_folder) puts "Daemonizing..." exit if fork Process.setsid exit if fork Process.setpgrp @daemon = true lock_file = File.open(pid_file, 'wb') # someone else is already running; just exit unless lock_file.flock(File::LOCK_EX | File::LOCK_NB) exit end at_exit { lock_file.flock(File::LOCK_UN) } lock_file.puts(Process.pid.to_s) lock_file.flush # if we blow up so badly that we can't syslog the error, try to send # it somewhere useful last_ditch_logfile = Settings.last_ditch_logfile || "log/delayed_job.log" if last_ditch_logfile[0] != '|' last_ditch_logfile = Settings.(last_ditch_logfile) end STDIN.reopen("/dev/null") STDOUT.reopen(open(last_ditch_logfile, 'a')) STDERR.reopen(STDOUT) STDOUT.sync = STDERR.sync = true end |
#daemonized? ⇒ Boolean
93 94 95 |
# File 'lib/delayed/daemon.rb', line 93 def daemonized? !!@daemon end |
#pid ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/delayed/daemon.rb', line 85 def pid if File.file?(pid_file) pid = File.read(pid_file).to_i pid = nil unless pid > 0 end pid end |
#pid_file ⇒ Object
81 82 83 |
# File 'lib/delayed/daemon.rb', line 81 def pid_file File.join(pid_folder, 'delayed_jobs_pool.pid') end |
#status(print: true, pid: self.pid) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/delayed/daemon.rb', line 14 def status(print: true, pid: self.pid) alive = pid && (Process.kill(0, pid) rescue false) && :running alive ||= :draining if pid && Process.kill(0, -pid) rescue false if alive puts "Delayed jobs #{alive}, pool PID: #{pid}" if print else puts "No delayed jobs pool running" if print && print != :alive end alive end |
#stop(kill: false, pid: self.pid) ⇒ Object
stop the currently running daemon (not this current process, the one in the pid_file)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/delayed/daemon.rb', line 57 def stop(kill: false, pid: self.pid) alive = status(pid: pid, print: false) if alive == :running || (kill && alive == :draining) puts "Stopping pool #{pid}..." signal = kill ? 'TERM' : 'QUIT' begin Process.kill(signal, pid) rescue Errno::ESRCH # ignore if the pid no longer exists end wait(kill) else status end end |
#wait(kill) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/delayed/daemon.rb', line 73 def wait(kill) if kill sleep(0.5) while status(pid: pid, print: false) else sleep(0.5) while status(pid: pid, print: false) == :running end end |