Class: Demon::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/demon/base.rb
Overview
intelligent fork based demonizer
Constant Summary
collapse
- HOSTNAME =
Socket.gethostname
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(index, rails_root: nil, parent_pid: nil, verbose: false, logger: nil) ⇒ Base
Returns a new instance of Base.
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/demon/base.rb', line 55
def initialize(index, rails_root: nil, parent_pid: nil, verbose: false, logger: nil)
@index = index
@pid = nil
@parent_pid = parent_pid || Process.pid
@started = false
@stop_timeout = 10
@rails_root = rails_root || Rails.root
@verbose = verbose
@logger = logger || Logger.new(STDERR)
end
|
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
52
53
54
|
# File 'lib/demon/base.rb', line 52
def index
@index
end
|
#parent_pid ⇒ Object
Returns the value of attribute parent_pid.
52
53
54
|
# File 'lib/demon/base.rb', line 52
def parent_pid
@parent_pid
end
|
#pid ⇒ Object
Returns the value of attribute pid.
52
53
54
|
# File 'lib/demon/base.rb', line 52
def pid
@pid
end
|
#started ⇒ Object
Returns the value of attribute started.
52
53
54
|
# File 'lib/demon/base.rb', line 52
def started
@started
end
|
#stop_timeout ⇒ Object
Returns the value of attribute stop_timeout.
53
54
55
|
# File 'lib/demon/base.rb', line 53
def stop_timeout
@stop_timeout
end
|
Class Method Details
.alive?(pid) ⇒ Boolean
199
200
201
202
203
204
|
# File 'lib/demon/base.rb', line 199
def self.alive?(pid)
Process.kill(0, pid)
true
rescue StandardError
false
end
|
.demons ⇒ Object
10
11
12
|
# File 'lib/demon/base.rb', line 10
def self.demons
@demons
end
|
.ensure_running ⇒ Object
43
44
45
|
# File 'lib/demon/base.rb', line 43
def self.ensure_running
@demons.values.each { |demon| demon.ensure_running }
end
|
.kill(signal) ⇒ Object
47
48
49
50
|
# File 'lib/demon/base.rb', line 47
def self.kill(signal)
return unless @demons
@demons.values.each { |demon| demon.kill(signal) }
end
|
.reset_demons ⇒ Object
19
20
21
|
# File 'lib/demon/base.rb', line 19
def self.reset_demons
@demons = {}
end
|
.restart ⇒ Object
38
39
40
41
|
# File 'lib/demon/base.rb', line 38
def self.restart
return unless @demons
@demons.values.each { |demon| demon.restart }
end
|
.set_demons(demons) ⇒ Object
15
16
17
|
# File 'lib/demon/base.rb', line 15
def self.set_demons(demons)
@demons = demons
end
|
.start(count = 1, verbose: false, logger: nil) ⇒ Object
28
29
30
31
|
# File 'lib/demon/base.rb', line 28
def self.start(count = 1, verbose: false, logger: nil)
@demons ||= {}
count.times { |i| (@demons["#{prefix}_#{i}"] ||= new(i, verbose:, logger:)).start }
end
|
.stop ⇒ Object
33
34
35
36
|
# File 'lib/demon/base.rb', line 33
def self.stop
return unless @demons
@demons.values.each { |demon| demon.stop }
end
|
Instance Method Details
#alive?(pid = nil) ⇒ Boolean
74
75
76
77
78
79
80
81
|
# File 'lib/demon/base.rb', line 74
def alive?(pid = nil)
pid ||= @pid
if pid
Demon::Base.alive?(pid)
else
false
end
end
|
#already_running? ⇒ Boolean
190
191
192
193
194
195
196
197
|
# File 'lib/demon/base.rb', line 190
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/demon/base.rb', line 139
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
log("Detected dead worker #{@pid}, restarting...")
@pid = nil
@started = false
start
end
end
|
#kill(signal) ⇒ Object
83
84
85
|
# File 'lib/demon/base.rb', line 83
def kill(signal)
Process.kill(signal, @pid)
end
|
#log(message, level: :info) ⇒ Object
66
67
68
|
# File 'lib/demon/base.rb', line 66
def log(message, level: :info)
@logger.public_send(level, message)
end
|
#pid_file ⇒ Object
70
71
72
|
# File 'lib/demon/base.rb', line 70
def pid_file
"#{@rails_root}/tmp/pids/#{self.class.prefix}_#{@index}.pid"
end
|
#restart ⇒ Object
91
92
93
94
|
# File 'lib/demon/base.rb', line 91
def restart
stop
start
end
|
#run ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/demon/base.rb', line 176
def run
Discourse.before_fork if defined?(Discourse)
@pid =
fork do
Process.setproctitle("discourse #{self.class.prefix}")
monitor_parent
establish_app
after_fork
end
write_pid_file
end
|
#set_pid(pid) ⇒ Object
23
24
25
|
# File 'lib/demon/base.rb', line 23
def set_pid(pid)
@pid = pid
end
|
#start ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/demon/base.rb', line 163
def start
return if @pid || @started
if existing = already_running?
log("Attempting to kill pid #{existing}")
Process.kill("TERM", existing)
end
@started = true
run
end
|
#stop ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/demon/base.rb', line 96
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?
log(
"Process would not terminate cleanly, force quitting. pid: #{@pid} #{self.class}\n#{caller.join("\n")}",
)
Process.kill("KILL", @pid)
end
wait_for_stop.call
@pid = nil
@started = false
end
end
|
#stop_signal ⇒ Object
87
88
89
|
# File 'lib/demon/base.rb', line 87
def stop_signal
"HUP"
end
|