Class: Workhorse::Daemon
- Inherits:
-
Object
show all
- Defined in:
- lib/workhorse/daemon.rb
Defined Under Namespace
Classes: ShellHandler, Worker
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(pidfile: nil, quiet: false) {|ScopedEnv.new(self, [:worker])| ... } ⇒ Daemon
Returns a new instance of Daemon.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/workhorse/daemon.rb', line 19
def initialize(pidfile: nil, quiet: false, &_block)
@pidfile = pidfile
@quiet = quiet
@workers = []
yield ScopedEnv.new(self, [:worker])
@count = @workers.count
fail 'No workers are defined.' if @count < 1
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 Attribute Details
#workers ⇒ Object
17
18
19
|
# File 'lib/workhorse/daemon.rb', line 17
def workers
@workers
end
|
Instance Method Details
#restart ⇒ Object
122
123
124
125
|
# File 'lib/workhorse/daemon.rb', line 122
def restart
stop
return start
end
|
#restart_logging ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/workhorse/daemon.rb', line 127
def restart_logging
code = 0
for_each_worker do |worker|
_pid_file, pid = read_pid(worker)
begin
Process.kill 'HUP', pid
puts "Worker (#{worker.name}) ##{worker.id}: Sent signal for restart-logging"
rescue Errno::ESRCH
warn "Worker (#{worker.name}) ##{worker.id}: Could not send signal for restart-logging, process not found"
code = 2
end
end
return code
end
|
#start(quiet: false) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/workhorse/daemon.rb', line 45
def start(quiet: false)
code = 0
for_each_worker do |worker|
pid_file, pid = read_pid(worker)
if pid_file && pid
warn "Worker ##{worker.id} (#{worker.name}): Already started (PID #{pid})" unless quiet
code = 2
elsif pid_file
File.delete pid_file
puts "Worker ##{worker.id} (#{worker.name}): Starting (stale pid file)" unless quiet
start_worker worker
else
warn "Worker ##{worker.id} (#{worker.name}): Starting" unless quiet
start_worker worker
end
end
return code
end
|
#status(quiet: false) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/workhorse/daemon.rb', line 88
def status(quiet: false)
code = 0
for_each_worker do |worker|
pid_file, pid = read_pid(worker)
if pid_file && pid
puts "Worker ##{worker.id} (#{worker.name}): Running" unless quiet
elsif pid_file
warn "Worker ##{worker.id} (#{worker.name}): Not running (stale PID file)" unless quiet
code = 2
else
warn "Worker ##{worker.id} (#{worker.name}): Not running" unless quiet
code = 2
end
end
return code
end
|
#stop(kill = false) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/workhorse/daemon.rb', line 67
def stop(kill = false)
code = 0
for_each_worker do |worker|
pid_file, pid = read_pid(worker)
if pid_file && pid
puts "Worker (#{worker.name}) ##{worker.id}: Stopping"
stop_worker pid_file, pid, kill: kill
elsif pid_file
File.delete pid_file
puts "Worker (#{worker.name}) ##{worker.id}: Already stopped (stale PID file)"
else
warn "Worker (#{worker.name}) ##{worker.id}: Already stopped"
code = 2
end
end
return code
end
|
#watch ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/workhorse/daemon.rb', line 108
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(quiet: Workhorse.silence_watcher)
else
return 0
end
end
|
#worker(name = 'Job Worker', &block) ⇒ Object
41
42
43
|
# File 'lib/workhorse/daemon.rb', line 41
def worker(name = 'Job Worker', &block)
@workers << Worker.new(@workers.size + 1, name, &block)
end
|