pastry 0.3.0 → 0.3.1

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.
Files changed (2) hide show
  1. data/lib/pastry.rb +30 -13
  2. metadata +2 -2
data/lib/pastry.rb CHANGED
@@ -95,6 +95,31 @@ class Pastry
95
95
  @logger ||= Logger.new(logfile || (daemonize ? '/tmp/pastry.log' : $stdout), 0)
96
96
  end
97
97
 
98
+ def respawn!
99
+ Thread.exclusive do
100
+ died = pids.select {|pid| Process.waitpid(pid, Process::WNOHANG) rescue 0} # find dead pids
101
+ died = died.reject {|pid| Process.kill(0, pid) rescue nil} # make sure what's dead is dead
102
+
103
+ died.each do |pid|
104
+ logger.info "process #{pid} died, starting a new one"
105
+ if idx = pids.index(pid)
106
+ pids[idx] = run(server, idx)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def start_command_listener io
113
+ Thread.new do
114
+ while command = io.gets
115
+ case command
116
+ when /SIGCHLD/
117
+ respawn! if @running
118
+ end
119
+ end
120
+ end
121
+ end
122
+
98
123
  def start!
99
124
  create_pidfile
100
125
  server = ENV['PASTRY_FD'] && Socket.for_fd(ENV['PASTRY_FD'].to_i)
@@ -121,19 +146,11 @@ class Pastry
121
146
  @running = true
122
147
  @pids = pool.times.map {|idx| run(server, idx) }
123
148
 
124
- Signal.trap('CHLD') do
125
- if @running
126
- Thread.exclusive do
127
- died = pids.select {|pid| Process.waitpid(pid, Process::WNOHANG) rescue 0} # find dead pids
128
- died = died.reject {|pid| Process.kill(0, pid) rescue nil} # make sure what's dead is dead
129
-
130
- died.each do |pid|
131
- logger.info "process #{pid} died, starting a new one"
132
- if idx = pids.index(pid)
133
- pids[idx] = run(server, idx)
134
- end
135
- end
136
- end
149
+ # hack to avoid deadlocks in signal handler
150
+ IO.pipe.tap do |reader, writer|
151
+ start_command_listener(reader)
152
+ Signal.trap('CHLD') do
153
+ writer.puts 'SIGCHLD'
137
154
  end
138
155
  end
139
156
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pastry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -48,7 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
49
  - - ! '>='
50
50
  - !ruby/object:Gem::Version
51
- hash: 3318781981847808244
51
+ hash: -4338099220466715262
52
52
  version: '0'
53
53
  segments:
54
54
  - 0