Class: Workhorse::Daemon
- Inherits:
-
Object
- Object
- Workhorse::Daemon
- Defined in:
- lib/workhorse/daemon.rb
Defined Under Namespace
Classes: ShellHandler
Instance Method Summary collapse
-
#initialize(count: 1, pidfile: nil, quiet: false, &block) ⇒ Daemon
constructor
A new instance of Daemon.
- #restart ⇒ Object
- #start ⇒ Object
- #status(quiet: false) ⇒ Object
- #stop ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(count: 1, pidfile: nil, quiet: false, &block) ⇒ Daemon
Returns a new instance of Daemon.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/workhorse/daemon.rb', line 3 def initialize(count: 1, pidfile: nil, quiet: false, &block) @count = count @pidfile = pidfile @quiet = quiet @block = block fail 'Count must be an integer > 0.' unless count.is_a?(Integer) && count > 0 FileUtils.mkdir_p('tmp/pids') if @pidfile.nil? @pidfile = count > 1 ? 'tmp/pids/workhorse.%i.pid' : 'tmp/pids/workhorse.pid' elsif @count > 1 && !@pidfile.include?('%s') fail 'Pidfile must include placeholder "%s" for worker id when specifying a count > 1.' elsif @count == 0 && @pidfile.include?('%s') fail 'Pidfile must not include placeholder "%s" for worker id when specifying a count of 1.' end end |
Instance Method Details
#restart ⇒ Object
99 100 101 102 |
# File 'lib/workhorse/daemon.rb', line 99 def restart stop return start end |
#start ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/workhorse/daemon.rb', line 22 def start code = 0 for_each_worker do |worker_id| pid_file, pid = read_pid(worker_id) if pid_file && pid warn "Worker ##{worker_id}: Already started (PID #{pid})" code = 1 elsif pid_file File.delete pid_file puts "Worker ##{worker_id}: Starting (stale pid file)" start_worker worker_id else warn "Worker ##{worker_id}: Starting" start_worker worker_id end end return code end |
#status(quiet: false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/workhorse/daemon.rb', line 65 def status(quiet: false) code = 0 for_each_worker do |worker_id| pid_file, pid = read_pid(worker_id) if pid_file && pid puts "Worker ##{worker_id}: Running" unless quiet elsif pid_file warn "Worker ##{worker_id}: Not running (stale PID file)" unless quiet code = 1 else warn "Worker ##{worker_id}: Not running" unless quiet code = 1 end end return code end |
#stop ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/workhorse/daemon.rb', line 44 def stop code = 0 for_each_worker do |worker_id| pid_file, pid = read_pid(worker_id) if pid_file && pid puts "Worker ##{worker_id}: Stopping" stop_worker pid_file, pid elsif pid_file File.delete pid_file puts "Worker ##{worker_id}: Already stopped (stale PID file)" else warn "Worker ##{worker_id}: Already stopped" code = 1 end end return code end |
#watch ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/workhorse/daemon.rb', line 85 def watch if defined?(Rails) should_be_running = !File.exist?(Rails.root.join('tmp/stop.txt')) else should_be_running = true end if should_be_running && status(quiet: true) != 0 return start else return 0 end end |