wonko9-i_can_daemonize 0.7.0 → 0.7.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.
data/README.txt CHANGED
@@ -62,7 +62,7 @@ Your daemon can be called with a number of options
62
62
  You can add your own command line options by using the 'arg' class macro.
63
63
  See http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html for more info on OptionParser.
64
64
 
65
- arg '--scott-rocks', 'Does scott rock?') do |value|
65
+ arg('--scott-rocks', 'Does scott rock?') do |value|
66
66
  @scott_rocks = value
67
67
  end
68
68
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
2
+ :patch: 1
3
3
  :major: 0
4
4
  :minor: 7
@@ -38,7 +38,7 @@ module ICanDaemonize
38
38
 
39
39
  opt.on_tail('-h', '--help', 'Show this message') do
40
40
  puts opt
41
- exit
41
+ exit(1)
42
42
  end
43
43
 
44
44
  opt.on('--loop-every=SECONDS', 'How long to sleep between each loop') do |value|
@@ -102,6 +102,10 @@ module ICanDaemonize
102
102
  @options ||= {}
103
103
  end
104
104
 
105
+ def options=(options)
106
+ @options = options
107
+ end
108
+
105
109
  def config
106
110
  yield @@config
107
111
  end
@@ -114,8 +118,10 @@ module ICanDaemonize
114
118
  callbacks[:after] = block
115
119
  end
116
120
 
117
- def sig(signal, &block)
118
- callbacks["sig_#{signal}".to_sym] = block
121
+ def sig(*signals, &block)
122
+ signals.each do |s|
123
+ callbacks["sig_#{s}".to_sym] = block
124
+ end
119
125
  end
120
126
 
121
127
  def die_if(method=nil,&block)
@@ -138,6 +144,9 @@ module ICanDaemonize
138
144
  # <tt>:timeout</tt> Fixnum (DEFAULT 0)
139
145
  # Timeout in if block does not execute withing passed number of seconds
140
146
  #
147
+ # <tt>:kill_timeout</tt> Fixnum (DEFAULT 120)
148
+ # Wait number of seconds before using kill -9 on daemon
149
+ #
141
150
  # <tt>:die_on_timeout</tt> BOOL (DEFAULT False)
142
151
  # Should the daemon continue running if a block times out, or just run the block again
143
152
  #
@@ -163,10 +172,10 @@ module ICanDaemonize
163
172
  # <tt>:log_prefix</tt> BOOL (DEFAULT true)
164
173
  # Prefix log file entries with PID and timestamp
165
174
  def daemonize(opts={}, &block)
175
+ self.options = opts
166
176
  parse_options
167
177
  return unless ok_to_start?
168
178
 
169
- options.merge!(opts)
170
179
  puts "Starting #{instances_to_start} #{script_name} #{pluralize('instance', instances_to_start)}..."
171
180
  puts "Logging to: #{log_file}" unless ontop?
172
181
 
@@ -209,6 +218,7 @@ module ICanDaemonize
209
218
  def run_block(&block)
210
219
  loop do
211
220
  break unless running?
221
+
212
222
  if options[:timeout]
213
223
  begin
214
224
  Timeout::timeout(options[:timeout].to_i) do
@@ -223,13 +233,14 @@ module ICanDaemonize
223
233
  end
224
234
  else
225
235
  block.call if block
226
-
227
236
  end
237
+
228
238
  if options[:loop_every]
229
239
  sleep options[:loop_every].to_i
230
240
  elsif not block
231
241
  sleep 0.1
232
242
  end
243
+
233
244
  break if should_exit?
234
245
  raise DieTime.new('Die if conditions were met!') if should_die?
235
246
  end
@@ -278,12 +289,12 @@ module ICanDaemonize
278
289
  living_pids << pid
279
290
  else
280
291
  $stderr.puts "Removing stale pid: #{pid}..."
281
- pids -= [pid]
292
+ pids.delete(pid)
282
293
  self.pids = pids
283
294
  end
284
295
  end
285
296
  if instances > 0 and living_pids.size >= instances
286
- $stderr.puts "#{script_name} is already running #{living_pids.size} out of #{pluralize('instance', instances)}"
297
+ $stderr.puts "#{script_name} is already running #{living_pids.size} out of #{instances} #{pluralize('instance', instances)}"
287
298
  return false
288
299
  end
289
300
  end
@@ -296,7 +307,7 @@ module ICanDaemonize
296
307
  puts "Stopping #{instances} #{script_name} #{pluralize('instance', instances)}..."
297
308
  if pids.empty?
298
309
  $stderr.puts "#{script_name} doesn't appear to be running"
299
- exit
310
+ exit(1)
300
311
  end
301
312
  pids.each_with_index do |pid, ii|
302
313
  kill_pid(pid)
@@ -314,7 +325,7 @@ module ICanDaemonize
314
325
  $stdout.puts("Stopping pid #{pid} with #{signal}...")
315
326
  begin
316
327
  Process.kill(signal, pid)
317
- if pid_running?(pid, options[:timeout] || 120)
328
+ if pid_running?(pid, options[:kill_timeout] || 120)
318
329
  $stdout.puts("Using kill -9 #{pid}")
319
330
  Process.kill(9, pid)
320
331
  else
@@ -12,6 +12,9 @@ class SimpleDaemon
12
12
  @short_test = value
13
13
  end
14
14
 
15
+ sig(:int, :term) do
16
+ end
17
+
15
18
  counter = 0
16
19
  daemonize do
17
20
  if @options[:loop_every]
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class TestICanDaemonize < Test::Unit::TestCase
4
- DEFAULT_LOG_FILE = File.join(File.dirname(__FILE__), 'test_daemon.rb.log')
4
+ DEFAULT_LOG_FILE = File.dirname(__FILE__) + '/simple_daemon.log'
5
5
 
6
6
  def setup
7
7
  File.delete(TEST_FILE) if File.exist?(TEST_FILE)
@@ -36,4 +36,12 @@ class TestICanDaemonize < Test::Unit::TestCase
36
36
  assert_equal "test|short-test", File.read(TEST_FILE)
37
37
  end
38
38
 
39
+ test "delete stale pid" do
40
+ pidfile = File.dirname(__FILE__) + '/testpids.pid'
41
+ File.open(pidfile, 'w'){|f| f << '999999999'}
42
+ `ruby #{@daemon} start --pid-file=#{pidfile}`
43
+ `ruby #{@daemon} stop --pid-file=#{pidfile}`
44
+ assert !File.exist?(pidfile)
45
+ end
46
+
39
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wonko9-i_can_daemonize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Pisoni
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-01-19 00:00:00 -08:00
13
+ date: 2009-03-17 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16