wonko9-i_can_daemonize 0.3.0 → 0.6.0
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/Rakefile +34 -5
- data/VERSION.yml +4 -0
- data/lib/i_can_daemonize.rb +109 -91
- data/test/simple_daemon.rb +26 -0
- data/test/test_helper.rb +14 -0
- metadata +17 -21
- data/examples/feature_demo.rb +0 -45
- data/examples/rails_daemon.rb +0 -20
- data/examples/simple_daemon.rb +0 -11
- data/examples/starling_queue_daemon.rb +0 -41
- data/i_can_daemonize.gemspec +0 -29
- data/lib/i_can_daemonize/version.rb +0 -10
data/Rakefile
CHANGED
@@ -1,8 +1,37 @@
|
|
1
|
-
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rcov/rcovtask'
|
2
5
|
|
3
|
-
|
4
|
-
require '
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |s|
|
9
|
+
s.name = "i_can_daemonize"
|
10
|
+
s.summary = "ICanDaemonize makes it dead simple to create daemons of your own"
|
11
|
+
s.email = "wonko9@gmail.com"
|
12
|
+
s.homepage = "http://github.com/wonko9/i_can_daemonize"
|
13
|
+
s.description = "ICanDaemonize makes it dead simple to create daemons of your own"
|
14
|
+
s.authors = ["Adam Pisoni", "Amos Elliston"]
|
15
|
+
s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::TestTask.new
|
5
22
|
|
6
|
-
|
7
|
-
|
23
|
+
Rake::RDocTask.new do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'test'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README*')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
8
29
|
end
|
30
|
+
|
31
|
+
Rcov::RcovTask.new do |t|
|
32
|
+
t.libs << 'test'
|
33
|
+
t.test_files = FileList['test/**/*_test.rb']
|
34
|
+
t.verbose = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :test
|
data/VERSION.yml
ADDED
data/lib/i_can_daemonize.rb
CHANGED
@@ -45,12 +45,12 @@ module ICanDaemonize
|
|
45
45
|
options[:loop_every] = value
|
46
46
|
end
|
47
47
|
|
48
|
-
opt.on('-t', '--ontop', 'Stay on top (does not daemonize)') do
|
49
|
-
options[:ontop] =
|
48
|
+
opt.on('-t', '--ontop', 'Stay on top (does not daemonize)') do
|
49
|
+
options[:ontop] = true
|
50
50
|
end
|
51
51
|
|
52
52
|
opt.on('--instances=NUM', 'Allow multiple instances to run simultaneously? 0 for infinite. default: 1') do |value|
|
53
|
-
|
53
|
+
self.instances = value.to_i
|
54
54
|
end
|
55
55
|
|
56
56
|
opt.on('--log-file=LOGFILE', 'Logfile to log to') do |value|
|
@@ -61,10 +61,8 @@ module ICanDaemonize
|
|
61
61
|
options[:pid_file] = File.expand_path(value)
|
62
62
|
end
|
63
63
|
|
64
|
-
opt.on('--log-prefix
|
65
|
-
|
66
|
-
options[:log_prefix] = false
|
67
|
-
end
|
64
|
+
opt.on('--no-log-prefix', 'Do not prefix PID and date/time in log file.') do
|
65
|
+
options[:log_prefix] = false
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
@@ -75,17 +73,14 @@ module ICanDaemonize
|
|
75
73
|
end
|
76
74
|
|
77
75
|
opts.parse!
|
78
|
-
options[:ontop] ||= !ARGV.include?('start')
|
79
76
|
|
80
77
|
if ARGV.include?('stop')
|
81
|
-
|
82
|
-
stop_daemons(@instances)
|
78
|
+
stop_daemons
|
83
79
|
elsif ARGV.include?('restart')
|
84
80
|
restart_daemons
|
85
|
-
elsif ARGV.include?('start')
|
86
|
-
|
87
|
-
|
88
|
-
@restarted = true if ARGV.include?("HUP")
|
81
|
+
elsif ARGV.include?('start') or ontop?
|
82
|
+
self.running = true
|
83
|
+
self.restarted = true if ARGV.include?('HUP')
|
89
84
|
else
|
90
85
|
puts opts.help
|
91
86
|
end
|
@@ -104,7 +99,7 @@ module ICanDaemonize
|
|
104
99
|
end
|
105
100
|
|
106
101
|
def options
|
107
|
-
@options ||= {
|
102
|
+
@options ||= {}
|
108
103
|
end
|
109
104
|
|
110
105
|
def config
|
@@ -165,23 +160,26 @@ module ICanDaemonize
|
|
165
160
|
# Run this check after each iteration of the loop. If the block returns true, exit gracefully
|
166
161
|
# You can also define the after block by putting an exit_if do/end block in your class.
|
167
162
|
#
|
168
|
-
# <tt>:log_prefix</tt> BOOL (DEFAULT
|
163
|
+
# <tt>:log_prefix</tt> BOOL (DEFAULT true)
|
169
164
|
# Prefix log file entries with PID and timestamp
|
170
|
-
def daemonize(
|
165
|
+
def daemonize(opts={}, &block)
|
171
166
|
parse_options
|
172
167
|
return unless ok_to_start?
|
173
168
|
|
174
|
-
options.merge!(
|
175
|
-
puts "Starting #{
|
169
|
+
options.merge!(opts)
|
170
|
+
puts "Starting #{instances_to_start} #{script_name} #{pluarlize('instance', instances_to_start)}..."
|
171
|
+
puts "Logging to: #{log_file}" unless ontop?
|
176
172
|
|
177
|
-
|
173
|
+
unless ontop?
|
178
174
|
instances_to_start.times do
|
179
175
|
safefork do
|
180
|
-
|
176
|
+
open(pid_file, 'a+') {|f| f << Process.pid << "\n"}
|
177
|
+
at_exit { remove_pid! }
|
181
178
|
|
182
|
-
|
183
|
-
trap('
|
184
|
-
trap('
|
179
|
+
|
180
|
+
trap('TERM') { callback!(:sig_term) ; self.running = false }
|
181
|
+
trap('INT') { callback!(:sig_int) ; Process.kill('TERM', $$) }
|
182
|
+
trap('HUP') { callback!(:sig_hup) ; restart_self }
|
185
183
|
|
186
184
|
sess_id = Process.setsid
|
187
185
|
reopen_filehandes
|
@@ -210,7 +208,7 @@ module ICanDaemonize
|
|
210
208
|
|
211
209
|
def run_block(&block)
|
212
210
|
loop do
|
213
|
-
break unless
|
211
|
+
break unless running?
|
214
212
|
if options[:timeout]
|
215
213
|
begin
|
216
214
|
Timeout::timeout(options[:timeout].to_i) do
|
@@ -218,9 +216,9 @@ module ICanDaemonize
|
|
218
216
|
end
|
219
217
|
rescue Timeout::Error => e
|
220
218
|
if options[:die_on_timeout]
|
221
|
-
raise TimeoutError.new("#{self}
|
219
|
+
raise TimeoutError.new("#{self} timed out after #{options[:timeout]} seconds while executing block in loop")
|
222
220
|
else
|
223
|
-
$stderr.puts "#{self}
|
221
|
+
$stderr.puts "#{self} timed out after #{options[:timeout]} seconds while executing block in loop #{e.backtrace.join("\n")}"
|
224
222
|
end
|
225
223
|
end
|
226
224
|
else
|
@@ -233,17 +231,18 @@ module ICanDaemonize
|
|
233
231
|
sleep 0.1
|
234
232
|
end
|
235
233
|
break if should_exit?
|
236
|
-
raise DieTime.new(
|
234
|
+
raise DieTime.new('Die if conditions were met!') if should_die?
|
237
235
|
end
|
238
236
|
exit(0)
|
239
237
|
end
|
240
238
|
|
241
239
|
def should_die?
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
240
|
+
die_if = options[:die_if]
|
241
|
+
if die_if
|
242
|
+
if die_if.is_a?(Symbol) or die_if.is_a?(String)
|
243
|
+
self.send(die_if)
|
244
|
+
elsif die_if.is_a?(Proc)
|
245
|
+
die_if.call
|
247
246
|
end
|
248
247
|
else
|
249
248
|
false
|
@@ -251,11 +250,12 @@ module ICanDaemonize
|
|
251
250
|
end
|
252
251
|
|
253
252
|
def should_exit?
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
253
|
+
exit_if = options[:exit_if]
|
254
|
+
if exit_if
|
255
|
+
if exit_if.is_a?(Symbol) or exit_if.is_a?(String)
|
256
|
+
self.send(exit_if.to_sym)
|
257
|
+
elsif exit_if.is_a?(Proc)
|
258
|
+
exit_if.call
|
259
259
|
end
|
260
260
|
else
|
261
261
|
false
|
@@ -263,27 +263,27 @@ module ICanDaemonize
|
|
263
263
|
end
|
264
264
|
|
265
265
|
def instances_to_start
|
266
|
-
return 1 if
|
267
|
-
|
266
|
+
return 1 if restarted?
|
267
|
+
instances - pids.size
|
268
268
|
end
|
269
269
|
|
270
270
|
def ok_to_start?
|
271
|
-
return false unless
|
272
|
-
return true if
|
273
|
-
|
271
|
+
return false unless running?
|
272
|
+
return true if restarted?
|
273
|
+
|
274
274
|
living_pids = []
|
275
275
|
if pids and pids.any?
|
276
276
|
pids.each do |pid|
|
277
277
|
if process_alive?(pid)
|
278
278
|
living_pids << pid
|
279
279
|
else
|
280
|
-
$stderr.puts "Removing stale pid: #{pid}"
|
280
|
+
$stderr.puts "Removing stale pid: #{pid}..."
|
281
281
|
pids -= [pid]
|
282
|
-
|
282
|
+
self.pids = pids
|
283
283
|
end
|
284
284
|
end
|
285
|
-
if
|
286
|
-
$stderr.puts "#{script_name} is already running #{living_pids.size} out of #{
|
285
|
+
if instances > 0 and living_pids.size >= instances
|
286
|
+
$stderr.puts "#{script_name} is already running #{living_pids.size} out of #{pluralize('instance', instances)}"
|
287
287
|
return false
|
288
288
|
end
|
289
289
|
end
|
@@ -291,43 +291,37 @@ module ICanDaemonize
|
|
291
291
|
end
|
292
292
|
|
293
293
|
# stop the daemon, nicely at first, and then forcefully if necessary
|
294
|
-
def stop_daemons
|
295
|
-
|
296
|
-
|
297
|
-
number_of_pids_to_stop = pids.size if number_of_pids_to_stop == 0
|
298
|
-
puts "stopping #{number_of_pids_to_stop} pids"
|
294
|
+
def stop_daemons
|
295
|
+
self.running = false
|
296
|
+
puts "Stopping #{instances} #{script_name} #{pluarlize('instance', instances)}..."
|
299
297
|
if pids.empty?
|
300
298
|
$stderr.puts "#{script_name} doesn't appear to be running"
|
301
299
|
exit
|
302
300
|
end
|
303
|
-
pids.each_with_index do |pid,ii|
|
301
|
+
pids.each_with_index do |pid, ii|
|
304
302
|
kill_pid(pid)
|
305
|
-
break if ii == (
|
303
|
+
break if ii == (instances - 1)
|
306
304
|
end
|
307
305
|
end
|
308
306
|
|
309
307
|
def restart_daemons
|
310
|
-
|
308
|
+
pids.each do |pid|
|
311
309
|
kill_pid(pid, 'HUP')
|
312
310
|
end
|
313
311
|
end
|
314
312
|
|
315
|
-
def
|
316
|
-
|
317
|
-
end
|
318
|
-
|
319
|
-
def kill_pid(pid,signal="TERM")
|
320
|
-
$stdout.puts("stopping pid: #{pid} sig: #{signal} #{script_name}...")
|
313
|
+
def kill_pid(pid, signal='TERM')
|
314
|
+
$stdout.puts("Stopping pid #{pid} with #{signal}...")
|
321
315
|
begin
|
322
316
|
Process.kill(signal, pid)
|
323
317
|
if pid_running?(pid, options[:timeout] || 120)
|
324
|
-
$stdout.puts("
|
318
|
+
$stdout.puts("Using kill -9 #{pid}")
|
325
319
|
Process.kill(9, pid)
|
326
320
|
else
|
327
|
-
$stdout.puts("
|
321
|
+
$stdout.puts("Process #{pid} stopped")
|
328
322
|
end
|
329
323
|
rescue Errno::ESRCH
|
330
|
-
$stdout.puts("
|
324
|
+
$stdout.puts("Couldn't #{signal} #{pid} as it wasn't running")
|
331
325
|
end
|
332
326
|
end
|
333
327
|
|
@@ -349,21 +343,21 @@ module ICanDaemonize
|
|
349
343
|
end
|
350
344
|
|
351
345
|
def restart_self
|
352
|
-
|
346
|
+
remove_pid!
|
353
347
|
cmd = "#{@@config.script_path}/#{script_name} "
|
354
348
|
cmd << 'HUP ' unless ARGV.include?('HUP')
|
355
349
|
cmd << ARGV.join(' ')
|
356
|
-
puts "
|
350
|
+
puts "Restarting #{cmd} pid: #{$$}..."
|
357
351
|
system(cmd)
|
358
352
|
Process.kill('TERM', $$)
|
359
353
|
end
|
360
354
|
|
361
355
|
def safefork(&block)
|
362
|
-
|
356
|
+
fork_tries ||= 0
|
363
357
|
fork(&block)
|
364
358
|
rescue Errno::EWOULDBLOCK
|
365
|
-
raise if
|
366
|
-
|
359
|
+
raise if fork_tries >= 20
|
360
|
+
fork_tries += 1
|
367
361
|
sleep 5
|
368
362
|
retry
|
369
363
|
end
|
@@ -385,9 +379,9 @@ module ICanDaemonize
|
|
385
379
|
if log_prefix?
|
386
380
|
def STDOUT.write(string)
|
387
381
|
if @no_prefix
|
388
|
-
@no_prefix = false if string[-1,1] == "\n"
|
382
|
+
@no_prefix = false if string[-1, 1] == "\n"
|
389
383
|
else
|
390
|
-
string = LOG_FORMAT % [$$,Time.now.strftime(TIME_FORMAT),string]
|
384
|
+
string = LOG_FORMAT % [$$, Time.now.strftime(TIME_FORMAT), string]
|
391
385
|
@no_prefix = true
|
392
386
|
end
|
393
387
|
super(string)
|
@@ -395,40 +389,64 @@ module ICanDaemonize
|
|
395
389
|
end
|
396
390
|
end
|
397
391
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
at_exit { remove_self_from_pidfile }
|
392
|
+
def remove_pid!(pid=Process.pid)
|
393
|
+
pids.delete(pid)
|
394
|
+
self.pids = pids
|
402
395
|
end
|
403
396
|
|
404
|
-
def
|
397
|
+
def pids=(pids)
|
405
398
|
if pids.any?
|
406
399
|
open(pid_file, 'w') {|f| f << pids.join("\n") << "\n"}
|
407
400
|
else
|
408
|
-
|
401
|
+
File.unlink(pid_file) if File.exists?(pid_file)
|
409
402
|
end
|
403
|
+
@pids = pids
|
410
404
|
end
|
411
405
|
|
412
|
-
def
|
413
|
-
pids
|
414
|
-
|
415
|
-
|
406
|
+
def pids
|
407
|
+
@pids ||= begin
|
408
|
+
if File.exist?(pid_file)
|
409
|
+
File.readlines(pid_file).collect {|p| p.to_i}
|
410
|
+
else
|
411
|
+
[]
|
412
|
+
end
|
413
|
+
end
|
416
414
|
end
|
417
415
|
|
418
|
-
def
|
419
|
-
|
416
|
+
def instances=(num)
|
417
|
+
@instances = num
|
420
418
|
end
|
421
419
|
|
422
|
-
def
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
420
|
+
def instances
|
421
|
+
@instances ||= 1
|
422
|
+
end
|
423
|
+
|
424
|
+
def pluarlize(name, num)
|
425
|
+
num == 1 ? name : "#{name}s"
|
426
|
+
end
|
427
|
+
|
428
|
+
def running?
|
429
|
+
@running || false
|
430
|
+
end
|
431
|
+
|
432
|
+
def running=(bool)
|
433
|
+
@running = bool
|
434
|
+
end
|
435
|
+
|
436
|
+
def restarted?
|
437
|
+
@restarted || false
|
438
|
+
end
|
439
|
+
|
440
|
+
def restarted=(bool)
|
441
|
+
@restarted = bool
|
442
|
+
end
|
443
|
+
|
444
|
+
def ontop?
|
445
|
+
options[:ontop]
|
428
446
|
end
|
429
447
|
|
430
448
|
def log_prefix?
|
431
|
-
options[:log_prefix]
|
449
|
+
options[:log_prefix] || true
|
432
450
|
end
|
433
451
|
|
434
452
|
LOG_PATHS = ['log/', 'logs/', '../log/', '../logs/', '../../log', '../../logs', '.']
|
@@ -454,7 +472,7 @@ module ICanDaemonize
|
|
454
472
|
end
|
455
473
|
|
456
474
|
def script_name
|
457
|
-
@script_name ||= File.basename($0)
|
475
|
+
@script_name ||= File.basename($0).gsub('.rb', '')
|
458
476
|
end
|
459
477
|
|
460
478
|
def script_name=(script_name)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/i_can_daemonize'
|
3
|
+
|
4
|
+
class SimpleDaemon
|
5
|
+
include ICanDaemonize
|
6
|
+
|
7
|
+
arg '--test=VALUE', 'Test Arg' do |value|
|
8
|
+
@test = value
|
9
|
+
end
|
10
|
+
|
11
|
+
arg '-s', '--short-test=VALUE', 'Test arg with shortname' do |value|
|
12
|
+
@short_test = value
|
13
|
+
end
|
14
|
+
|
15
|
+
counter = 0
|
16
|
+
daemonize do
|
17
|
+
if @options[:loop_every]
|
18
|
+
counter += 1
|
19
|
+
File.open(TEST_FILE, 'w'){|f| f << counter}
|
20
|
+
elsif @test
|
21
|
+
File.open(TEST_FILE, 'w'){|f| f << "#{@test}|#{@short_test}"}
|
22
|
+
else
|
23
|
+
File.open(TEST_FILE, 'w'){|f| f << "#{log_file}|#{pid_file}"}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
TEST_FILE = File.dirname(__FILE__) + '/test.txt' unless defined?(TEST_FILE)
|
5
|
+
|
6
|
+
unless Test::Unit::TestCase.respond_to?(:test)
|
7
|
+
class << Test::Unit::TestCase
|
8
|
+
def test(name, &block)
|
9
|
+
test_name = "test_#{name.gsub(/[\s\W]/,'_')}"
|
10
|
+
raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name
|
11
|
+
define_method test_name, &block
|
12
|
+
end
|
13
|
+
end
|
14
|
+
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.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Pisoni
|
@@ -10,38 +10,34 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-01-
|
13
|
+
date: 2009-01-19 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
17
|
-
description:
|
18
|
-
email:
|
17
|
+
description: ICanDaemonize makes it dead simple to create daemons of your own
|
18
|
+
email: wonko9@gmail.com
|
19
19
|
executables: []
|
20
20
|
|
21
21
|
extensions: []
|
22
22
|
|
23
|
-
extra_rdoc_files:
|
24
|
-
|
25
|
-
- Manifest.txt
|
26
|
-
- README.txt
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
27
25
|
files:
|
28
26
|
- History.txt
|
29
|
-
-
|
27
|
+
- Manifest.txt
|
30
28
|
- Rakefile
|
31
|
-
-
|
29
|
+
- README.txt
|
30
|
+
- VERSION.yml
|
32
31
|
- lib/i_can_daemonize.rb
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
- examples/simple_daemon.rb
|
37
|
-
- examples/starling_queue_daemon.rb
|
38
|
-
- Manifest.txt
|
32
|
+
- test/simple_daemon.rb
|
33
|
+
- test/test_helper.rb
|
34
|
+
- test/test_i_can_daemonize.rb
|
39
35
|
has_rdoc: true
|
40
36
|
homepage: http://github.com/wonko9/i_can_daemonize
|
41
37
|
post_install_message:
|
42
38
|
rdoc_options:
|
43
|
-
- --
|
44
|
-
-
|
39
|
+
- --inline-source
|
40
|
+
- --charset=UTF-8
|
45
41
|
require_paths:
|
46
42
|
- lib
|
47
43
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -62,6 +58,6 @@ rubyforge_project:
|
|
62
58
|
rubygems_version: 1.2.0
|
63
59
|
signing_key:
|
64
60
|
specification_version: 2
|
65
|
-
summary:
|
66
|
-
test_files:
|
67
|
-
|
61
|
+
summary: ICanDaemonize makes it dead simple to create daemons of your own
|
62
|
+
test_files: []
|
63
|
+
|
data/examples/feature_demo.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'i_can_daemonize'
|
3
|
-
|
4
|
-
class ICanDaemonize::FeatureDemo
|
5
|
-
include ICanDaemonize
|
6
|
-
|
7
|
-
def self.define_args(args)
|
8
|
-
# "See the OptionParser docs for more info on how to define your own args.\n http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html\n"
|
9
|
-
@options[:nobugs] = true
|
10
|
-
args.on("--scott-rocks=TRUE", "Thanks scott") do |t|
|
11
|
-
@options[:scott_rocks] = t
|
12
|
-
end
|
13
|
-
args.on("--nobugs=TRUE", "No bugs flag") do |t|
|
14
|
-
@options[:nobugs] = false if t == "1" or t.downcase == "false"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
before do
|
19
|
-
puts "The before block is executed after daemonizing, but before looping over the daemonize block"
|
20
|
-
if @options[:nobugs]
|
21
|
-
puts "Running with no bugs. Pass nobugs=false to run with bugs."
|
22
|
-
else
|
23
|
-
puts "There mite be busg"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
after do
|
28
|
-
puts "The after block is executed before the program exits gracefully, but is not run if the program dies."
|
29
|
-
end
|
30
|
-
|
31
|
-
die_if do
|
32
|
-
puts "The die_if block is executed after every loop and dies if true is returned."
|
33
|
-
false
|
34
|
-
end
|
35
|
-
|
36
|
-
exit_if do
|
37
|
-
puts "The exit_if block is executed after every loop and exits gracefully if true is returned."
|
38
|
-
false
|
39
|
-
end
|
40
|
-
|
41
|
-
daemonize(:loop_every => 3, :timeout=>2, :die_on_timeout => false) do
|
42
|
-
puts "The daemonize block is called in a loop."
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
data/examples/rails_daemon.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'i_can_daemonize'
|
3
|
-
begin
|
4
|
-
require File.dirname(__FILE__) + "/../config/environment"
|
5
|
-
rescue LoadError
|
6
|
-
puts "\n****** ERROR LOADING RAILS ******\n\trails_daemon.rb should be put in your RAILS_ROOT/script directory so it can find your environment.rb\n\tOr you can change the environment require on line 4.\n*********************************\n\n"
|
7
|
-
end
|
8
|
-
|
9
|
-
class ICanDaemonize::RailsDaemon
|
10
|
-
include ICanDaemonize
|
11
|
-
|
12
|
-
before do
|
13
|
-
puts "This daemon has access to your entire rails stack and will log to RAILS_ROOT/log"
|
14
|
-
end
|
15
|
-
|
16
|
-
daemonize(:loop_every => 3, :timeout=>2, :die_on_timeout => false) do
|
17
|
-
puts "The daemonize block is called in a loop."
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
data/examples/simple_daemon.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'pp'
|
3
|
-
require 'i_can_daemonize'
|
4
|
-
begin
|
5
|
-
require 'starling'
|
6
|
-
rescue LoadError
|
7
|
-
puts "\n****** ERROR LOADING STARLING ******\n\tStarling is not installed. Please run 'sudo gem install starling' before running this script.\n*********************************\n\n"
|
8
|
-
end
|
9
|
-
|
10
|
-
class ICanDaemonize::StarlingDaemon
|
11
|
-
include ICanDaemonize
|
12
|
-
|
13
|
-
if ARGV.include?('start')
|
14
|
-
puts <<-DOC
|
15
|
-
|
16
|
-
This daemon will listen to a starling queue called '#{@queue_name}' and print out whatever is added
|
17
|
-
First tail this daemon's log in another window.
|
18
|
-
The log is @ #{log_file}
|
19
|
-
Run irb at the console and type
|
20
|
-
> require 'rubygems'
|
21
|
-
> require 'starling'
|
22
|
-
> starling = Starling.new('127.0.0.1:22122')
|
23
|
-
> starling.set('#{@queue_name}','Hi there!')
|
24
|
-
Now watch the log file.
|
25
|
-
|
26
|
-
DOC
|
27
|
-
end
|
28
|
-
|
29
|
-
before do
|
30
|
-
@queue_name = "starlingdeamon"
|
31
|
-
@starling = Starling.new("127.0.0.1:22122")
|
32
|
-
@fetch_count = 0
|
33
|
-
end
|
34
|
-
|
35
|
-
daemonize(:log_prefix => false) do
|
36
|
-
puts "Trying to fetch from the '#{@queue_name}' queue. Dequeued #{@fetch_count} so far"
|
37
|
-
pp "GOT: ", @starling.get(@queue_name)
|
38
|
-
@fetch_count += 1
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/i_can_daemonize.gemspec
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Gem::Specification.new do |s|
|
2
|
-
s.name = "i_can_daemonize"
|
3
|
-
s.version = "0.3.0"
|
4
|
-
s.date = "2009-01-15"
|
5
|
-
s.summary = "Better way to build daemons"
|
6
|
-
s.email = "apisoni@yammer-inc.com"
|
7
|
-
s.homepage = "http://github.com/wonko9/i_can_daemonize"
|
8
|
-
s.description = "Better daemonizer."
|
9
|
-
s.has_rdoc = true
|
10
|
-
s.authors = ["Adam Pisoni", "Amos Elliston"]
|
11
|
-
s.files = [
|
12
|
-
"History.txt",
|
13
|
-
"README.txt",
|
14
|
-
"Rakefile",
|
15
|
-
"i_can_daemonize.gemspec",
|
16
|
-
"lib/i_can_daemonize.rb",
|
17
|
-
"lib/i_can_daemonize/version.rb",
|
18
|
-
"examples/feature_demo.rb",
|
19
|
-
"examples/rails_daemon.rb",
|
20
|
-
"examples/simple_daemon.rb",
|
21
|
-
"examples/starling_queue_daemon.rb",
|
22
|
-
]
|
23
|
-
|
24
|
-
s.test_files = ["test/test_i_can_daemonize.rb"]
|
25
|
-
s.rdoc_options = ["--main", "README.txt"]
|
26
|
-
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
27
|
-
# s.add_dependency("diff-lcs", ["> 0.0.0"])
|
28
|
-
end
|
29
|
-
|