Class: Demon::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/demon/base.rb
Overview
intelligent fork based demonizer
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(index, rails_root: nil, parent_pid: nil, verbose: false) ⇒ Base
Returns a new instance of Base.
42
43
44
45
46
47
48
49
50
|
# File 'lib/demon/base.rb', line 42
def initialize(index, rails_root: nil, parent_pid: nil, verbose: false)
@index = index
@pid = nil
@parent_pid = parent_pid || Process.pid
@started = false
@stop_timeout = 10
@rails_root = rails_root || Rails.root
@verbose = verbose
end
|
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
39
40
41
|
# File 'lib/demon/base.rb', line 39
def index
@index
end
|
#parent_pid ⇒ Object
Returns the value of attribute parent_pid.
39
40
41
|
# File 'lib/demon/base.rb', line 39
def parent_pid
@parent_pid
end
|
#pid ⇒ Object
Returns the value of attribute pid.
39
40
41
|
# File 'lib/demon/base.rb', line 39
def pid
@pid
end
|
#started ⇒ Object
Returns the value of attribute started.
39
40
41
|
# File 'lib/demon/base.rb', line 39
def started
@started
end
|
#stop_timeout ⇒ Object
Returns the value of attribute stop_timeout.
40
41
42
|
# File 'lib/demon/base.rb', line 40
def stop_timeout
@stop_timeout
end
|
Class Method Details
.alive?(pid) ⇒ Boolean
169
170
171
172
173
174
|
# File 'lib/demon/base.rb', line 169
def self.alive?(pid)
Process.kill(0, pid)
true
rescue StandardError
false
end
|
.demons ⇒ Object
8
9
10
|
# File 'lib/demon/base.rb', line 8
def self.demons
@demons
end
|
.ensure_running ⇒ Object
30
31
32
|
# File 'lib/demon/base.rb', line 30
def self.ensure_running
@demons.values.each { |demon| demon.ensure_running }
end
|
.kill(signal) ⇒ Object
34
35
36
37
|
# File 'lib/demon/base.rb', line 34
def self.kill(signal)
return unless @demons
@demons.values.each { |demon| demon.kill(signal) }
end
|
.restart ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/demon/base.rb', line 22
def self.restart
return unless @demons
@demons.values.each do |demon|
demon.stop
demon.start
end
end
|
.start(count = 1, verbose: false) ⇒ Object
12
13
14
15
|
# File 'lib/demon/base.rb', line 12
def self.start(count = 1, verbose: false)
@demons ||= {}
count.times { |i| (@demons["#{prefix}_#{i}"] ||= new(i, verbose: verbose)).start }
end
|
.stop ⇒ Object
17
18
19
20
|
# File 'lib/demon/base.rb', line 17
def self.stop
return unless @demons
@demons.values.each { |demon| demon.stop }
end
|
Instance Method Details
#alive?(pid = nil) ⇒ Boolean
56
57
58
59
60
61
62
63
|
# File 'lib/demon/base.rb', line 56
def alive?(pid = nil)
pid ||= @pid
if pid
Demon::Base.alive?(pid)
else
false
end
end
|
#already_running? ⇒ Boolean
160
161
162
163
164
165
166
167
|
# File 'lib/demon/base.rb', line 160
def already_running?
if File.exist? pid_file
pid = File.read(pid_file).to_i
return pid if Demon::Base.alive?(pid)
end
nil
end
|
#ensure_running ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/demon/base.rb', line 113
def ensure_running
return unless @started
if !@pid
@started = false
start
return
end
dead =
begin
Process.waitpid(@pid, Process::WNOHANG)
rescue StandardError
-1
end
if dead
STDERR.puts "Detected dead worker #{@pid}, restarting..."
@pid = nil
@started = false
start
end
end
|
#kill(signal) ⇒ Object
65
66
67
|
# File 'lib/demon/base.rb', line 65
def kill(signal)
Process.kill(signal, @pid)
end
|
#pid_file ⇒ Object
52
53
54
|
# File 'lib/demon/base.rb', line 52
def pid_file
"#{@rails_root}/tmp/pids/#{self.class.prefix}_#{@index}.pid"
end
|
#run ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/demon/base.rb', line 149
def run
@pid =
fork do
Process.setproctitle("discourse #{self.class.prefix}")
monitor_parent
establish_app
after_fork
end
write_pid_file
end
|
#start ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/demon/base.rb', line 136
def start
return if @pid || @started
if existing = already_running?
STDERR.puts "Attempting to kill pid #{existing}"
Process.kill("TERM", existing)
end
@started = true
run
end
|
#stop ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/demon/base.rb', line 73
def stop
@started = false
if @pid
Process.kill(stop_signal, @pid)
wait_for_stop =
lambda do
timeout = @stop_timeout
while alive? && timeout > 0
timeout -= (@stop_timeout / 10.0)
sleep(@stop_timeout / 10.0)
begin
Process.waitpid(@pid, Process::WNOHANG)
rescue StandardError
-1
end
end
begin
Process.waitpid(@pid, Process::WNOHANG)
rescue StandardError
-1
end
end
wait_for_stop.call
if alive?
STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid} #{self.class}"
Process.kill("KILL", @pid)
end
wait_for_stop.call
@pid = nil
@started = false
end
end
|
#stop_signal ⇒ Object
69
70
71
|
# File 'lib/demon/base.rb', line 69
def stop_signal
"HUP"
end
|