angael 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/angael/manager.rb +49 -33
- data/lib/angael/version.rb +1 -1
- data/spec/lib/angael/manager_spec.rb +2 -1
- metadata +1 -1
data/lib/angael/manager.rb
CHANGED
@@ -5,6 +5,7 @@ module Angael
|
|
5
5
|
# for SIGINT or SIGTERM. When either of those is received, the manager will
|
6
6
|
# call #stop_with_wait on each Worker.
|
7
7
|
class Manager
|
8
|
+
LOOP_SLEEP_SECONDS = 1
|
8
9
|
include ProcessHelper
|
9
10
|
attr_reader :workers
|
10
11
|
|
@@ -53,17 +54,8 @@ module Angael
|
|
53
54
|
workers.each { |w| w.start! }
|
54
55
|
|
55
56
|
trap("CHLD") do
|
56
|
-
|
57
|
-
|
58
|
-
#print w.pid.to_s
|
59
|
-
#print "\t"
|
60
|
-
#p result
|
61
|
-
if result
|
62
|
-
# worker terminated
|
63
|
-
# Restart it unless we asked it to stop.
|
64
|
-
w.restart! unless w.stopping?
|
65
|
-
end
|
66
|
-
end
|
57
|
+
log("SIGCHLD Received")
|
58
|
+
@sigchld = true
|
67
59
|
end
|
68
60
|
trap("INT") do
|
69
61
|
log("SIGINT Received")
|
@@ -75,32 +67,17 @@ module Angael
|
|
75
67
|
end
|
76
68
|
|
77
69
|
if @restart_after
|
78
|
-
restart_after_counter = @restart_after
|
79
70
|
loop do
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if restart_after_counter > 0
|
85
|
-
sleep 1
|
86
|
-
restart_after_counter -= 1
|
87
|
-
else
|
88
|
-
# Periodically restart workers, 1 at a time.
|
89
|
-
log("Sleeping for #@restart_after seconds")
|
90
|
-
w = next_worker_to_restart
|
91
|
-
log("Time to restart a worker: Calling #stop_with_wait for worker #{w.inspect}")
|
92
|
-
w.stop_with_wait
|
93
|
-
log("Worker has been stopped: #{w.inspect}")
|
94
|
-
w.start!
|
95
|
-
log("Worker has been restarted: #{w.inspect}")
|
96
|
-
w = nil
|
97
|
-
restart_after_counter = @restart_after
|
98
|
-
end
|
71
|
+
interrupted_handler
|
72
|
+
sigchld_handler
|
73
|
+
restart_worker_if_needed
|
74
|
+
sleep LOOP_SLEEP_SECONDS
|
99
75
|
end
|
100
76
|
else
|
101
77
|
loop do
|
102
|
-
|
103
|
-
|
78
|
+
interrupted_handler
|
79
|
+
sigchld_handler
|
80
|
+
sleep LOOP_SLEEP_SECONDS
|
104
81
|
end
|
105
82
|
end
|
106
83
|
end
|
@@ -142,5 +119,44 @@ module Angael
|
|
142
119
|
workers[@next_worker_to_restart_index]
|
143
120
|
end
|
144
121
|
|
122
|
+
|
123
|
+
def sigchld_handler
|
124
|
+
if @sigchld
|
125
|
+
workers.each do |w|
|
126
|
+
result = exit_status(w.pid)
|
127
|
+
if result
|
128
|
+
# worker terminated
|
129
|
+
# Restart it unless we asked it to stop.
|
130
|
+
w.restart! unless w.stopping?
|
131
|
+
end
|
132
|
+
end
|
133
|
+
@sigchld = false
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def interrupted_handler
|
138
|
+
stop! if @interrupted
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
# Periodically restart workers, 1 at a time.
|
143
|
+
def restart_worker_if_needed
|
144
|
+
@seconds_until_restart_next_worker ||= @restart_after
|
145
|
+
|
146
|
+
if @seconds_until_restart_next_worker > 0
|
147
|
+
@seconds_until_restart_next_worker -= LOOP_SLEEP_SECONDS
|
148
|
+
else
|
149
|
+
w = next_worker_to_restart
|
150
|
+
log("Time to restart a worker: Calling #stop_with_wait for worker #{w.inspect}")
|
151
|
+
w.stop_with_wait
|
152
|
+
log("Worker has been stopped: #{w.inspect}")
|
153
|
+
w.start!
|
154
|
+
log("Worker has been restarted: #{w.inspect}")
|
155
|
+
w = nil
|
156
|
+
|
157
|
+
# Reset the counter
|
158
|
+
@seconds_until_restart_next_worker = @restart_after
|
159
|
+
end
|
160
|
+
end
|
145
161
|
end
|
146
162
|
end
|
data/lib/angael/version.rb
CHANGED