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.
8 9 10 |
# File 'lib/delayed/daemon.rb', line 8 def initialize(pid_folder) @pid_folder = pid_folder end |
Instance Attribute Details
#pid_folder ⇒ Object (readonly)
Returns the value of attribute pid_folder.
6 7 8 |
# File 'lib/delayed/daemon.rb', line 6 def pid_folder @pid_folder end |
Instance Method Details
#daemonize! ⇒ Object
23 24 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 |
# File 'lib/delayed/daemon.rb', line 23 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
99 100 101 |
# File 'lib/delayed/daemon.rb', line 99 def daemonized? !!@daemon end |
#pid ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/delayed/daemon.rb', line 91 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
87 88 89 |
# File 'lib/delayed/daemon.rb', line 87 def pid_file File.join(pid_folder, 'delayed_jobs_pool.pid') end |
#status(print: true, pid: self.pid) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/delayed/daemon.rb', line 12 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)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/delayed/daemon.rb', line 55 def stop(kill: false, pid: self.pid) alive = status(pid: pid, print: false) if alive == :running || (kill && alive == :draining) puts "Stopping pool #{pid}..." signal = 'INT' if kill pid = -pid # send to the whole group if kill == 9 signal = 'KILL' else signal = 'TERM' end end 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
79 80 81 82 83 84 85 |
# File 'lib/delayed/daemon.rb', line 79 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 |