angael 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- workers.each do |w|
57
- result = exit_status(w.pid)
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
- stop! if @interrupted
81
-
82
- # This ensures we are checking @interrupted every 1 second, but we
83
- # can set @restart_after to something greater than zero.
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
- stop! if @interrupted
103
- sleep 1
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
@@ -1,3 +1,3 @@
1
1
  module Angael
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -143,7 +143,8 @@ describe Angael::Manager do
143
143
  subject.start!
144
144
  end
145
145
 
146
- sleep 0.1 # Give the process a chance to start.
146
+ # The main loop only checks every 1 second for a SIGCHLD signal.
147
+ sleep 1.1
147
148
  end
148
149
  end
149
150
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: angael
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.8
5
+ version: 0.0.9
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paul Cortens