blue-daemons 1.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +27 -0
  5. data/LICENSE +22 -0
  6. data/README-mlanett.rdoc +8 -0
  7. data/README.rdoc +214 -0
  8. data/Rakefile +32 -0
  9. data/Releases +201 -0
  10. data/TODO +2 -0
  11. data/daemons.gemspec +27 -0
  12. data/examples/call/call.rb +57 -0
  13. data/examples/call/call.rb.log +1 -0
  14. data/examples/call/call_monitor.rb +55 -0
  15. data/examples/daemonize/daemonize.rb +27 -0
  16. data/examples/run/ctrl_crash.rb +17 -0
  17. data/examples/run/ctrl_exec.rb +16 -0
  18. data/examples/run/ctrl_exit.rb +15 -0
  19. data/examples/run/ctrl_hanging.rb +19 -0
  20. data/examples/run/ctrl_keep_pid_files.rb +17 -0
  21. data/examples/run/ctrl_monitor.rb +16 -0
  22. data/examples/run/ctrl_monitor_multiple.rb +18 -0
  23. data/examples/run/ctrl_multiple.rb +16 -0
  24. data/examples/run/ctrl_normal.rb +11 -0
  25. data/examples/run/ctrl_ontop.rb +16 -0
  26. data/examples/run/ctrl_optionparser.rb +43 -0
  27. data/examples/run/ctrl_proc.rb +25 -0
  28. data/examples/run/ctrl_proc.rb.output +121 -0
  29. data/examples/run/ctrl_proc_multiple.rb +22 -0
  30. data/examples/run/ctrl_proc_multiple.rb.output +2 -0
  31. data/examples/run/ctrl_proc_rand.rb +23 -0
  32. data/examples/run/ctrl_proc_simple.rb +17 -0
  33. data/examples/run/ctrl_slowstop.rb +16 -0
  34. data/examples/run/myserver.rb +12 -0
  35. data/examples/run/myserver_crashing.rb +14 -0
  36. data/examples/run/myserver_exiting.rb +8 -0
  37. data/examples/run/myserver_hanging.rb +21 -0
  38. data/examples/run/myserver_slowstop.rb +21 -0
  39. data/lib/daemons.rb +312 -0
  40. data/lib/daemons/application.rb +481 -0
  41. data/lib/daemons/application_group.rb +200 -0
  42. data/lib/daemons/change_privilege.rb +19 -0
  43. data/lib/daemons/cmdline.rb +121 -0
  44. data/lib/daemons/controller.rb +140 -0
  45. data/lib/daemons/daemonize.rb +182 -0
  46. data/lib/daemons/etc_extension.rb +12 -0
  47. data/lib/daemons/exceptions.rb +31 -0
  48. data/lib/daemons/monitor.rb +144 -0
  49. data/lib/daemons/pid.rb +114 -0
  50. data/lib/daemons/pidfile.rb +118 -0
  51. data/lib/daemons/pidmem.rb +19 -0
  52. data/lib/daemons/version.rb +3 -0
  53. data/setup.rb +1360 -0
  54. data/spec/pidfile_spec.rb +12 -0
  55. data/spec/spec_helper.rb +1 -0
  56. metadata +146 -0
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ * put TODOS here
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'daemons/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blue-daemons"
8
+ spec.version = Daemons::VERSION
9
+
10
+ spec.authors = ["Thomas Uehlinger"]
11
+ spec.autorequire = %q{daemons}
12
+ spec.description = %q{Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands. You can also call blocks as daemons and control them from the parent or just daemonize the current process. Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash.}
13
+ spec.email = %q{th.uehlinger@gmx.ch}
14
+ spec.has_rdoc = true
15
+ spec.homepage = %q{http://daemons.rubyforge.org}
16
+ spec.rubyforge_project = %q{daemons}
17
+ spec.summary = %q{A toolkit to create and control daemons in different ways}
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", ">= 1.0"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,57 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+
10
+ require 'daemons'
11
+
12
+ testfile = File.expand_path(__FILE__) + '.log'
13
+
14
+
15
+ # On the first call to <tt<call</tt>, an application group (accessible by <tt>Daemons.group</tt>)
16
+ # will be created an the options will be kept within, so you only have to specify
17
+ # <tt>:multiple</tt> once.
18
+ #
19
+
20
+ options = {
21
+ :app_name => 'mytask',
22
+ # :ontop => true,
23
+ :multiple => true
24
+ }
25
+
26
+
27
+ Daemons.call(options) do
28
+ File.open(testfile, 'w') {|f|
29
+ f.puts "test"
30
+ }
31
+
32
+ loop { puts "1"; sleep 5 }
33
+ end
34
+ puts "first task started"
35
+
36
+ Daemons.call do
37
+ loop { puts "2"; sleep 4 }
38
+ end
39
+ puts "second task started"
40
+
41
+ # NOTE: this process will exit after 5 seconds
42
+ Daemons.call do
43
+ puts "3"
44
+ sleep 5
45
+ end
46
+ puts "third task started"
47
+
48
+ puts "waiting 20 seconds..."
49
+ sleep(20)
50
+
51
+ # This call would result in an exception as it will try to kill the third process
52
+ # which has already terminated by that time; but using the 'true' parameter forces the
53
+ # stop_all procedure.
54
+ puts "trying to stop all tasks..."
55
+ Daemons.group.stop_all(true)
56
+
57
+ puts "done"
@@ -0,0 +1 @@
1
+ test
@@ -0,0 +1,55 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+
10
+ require 'daemons'
11
+
12
+ testfile = File.expand_path(__FILE__) + '.log'
13
+
14
+
15
+ # On the first call to <tt<call</tt>, an application group (accessible by <tt>Daemons.group</tt>)
16
+ # will be created an the options will be kept within, so you only have to specify
17
+ # <tt>:multiple</tt> once.
18
+ #
19
+
20
+ options = {
21
+ # :ontop => true,
22
+ :multiple => true,
23
+ :monitor => true
24
+ }
25
+
26
+
27
+ Daemons.call(options) do
28
+ loop { puts "1"; sleep 20 }
29
+ end
30
+ puts "first task started"
31
+
32
+
33
+ # NOTE: this process will exit after 5 seconds
34
+ Daemons.call do
35
+ File.open(testfile, 'a') {|f|
36
+ f.puts "started..."
37
+ puts "2"
38
+
39
+ sleep 5
40
+
41
+ f.puts "...exit"
42
+ }
43
+ end
44
+ puts "second task started"
45
+
46
+ puts "waiting 100 seconds..."
47
+ sleep(100)
48
+
49
+ # This call would result in an exception as it will try to kill the third process
50
+ # which has already terminated by that time; but using the 'true' parameter forces the
51
+ # stop_all procedure.
52
+ puts "trying to stop all tasks..."
53
+ Daemons.group.stop_all(true)
54
+
55
+ puts "done"
@@ -0,0 +1,27 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+
10
+
11
+ require 'daemons'
12
+
13
+
14
+ options = {
15
+ :log_output => true
16
+ }
17
+
18
+
19
+ testfile = File.expand_path(__FILE__) + '.txt'
20
+
21
+ Daemons.daemonize(options)
22
+
23
+ puts "some output..."
24
+
25
+ File.open(testfile, 'w') {|f|
26
+ f.write("test")
27
+ }
@@ -0,0 +1,17 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :log_output => true,
14
+ :backtrace => true
15
+ }
16
+
17
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options)
@@ -0,0 +1,16 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :mode => :exec
14
+ }
15
+
16
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)
@@ -0,0 +1,15 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ }
14
+
15
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_exiting.rb'), options)
@@ -0,0 +1,19 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+ options = {
12
+ #:mode => :exec,
13
+ :multiple => true,
14
+ :no_pidfiles => true,
15
+ :force_kill_waittime => 5
16
+ #:force_kill_waittime => -1 # do not wait before killing -9
17
+ }
18
+
19
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_hanging.rb'), options)
@@ -0,0 +1,17 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :keep_pid_files => true
14
+ }
15
+
16
+
17
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)
@@ -0,0 +1,16 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :monitor => true
14
+ }
15
+
16
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options)
@@ -0,0 +1,18 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :multiple => true,
14
+ :monitor => true,
15
+ :log_output => true,
16
+ }
17
+
18
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options)
@@ -0,0 +1,16 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :multiple => true
14
+ }
15
+
16
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)
@@ -0,0 +1,11 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'))
@@ -0,0 +1,16 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :ontop => true
14
+ }
15
+
16
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)
@@ -0,0 +1,43 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+ require 'optparse'
11
+ require 'logger'
12
+ require 'ostruct'
13
+
14
+
15
+ class MyApp < Logger::Application
16
+ def initialize(args)
17
+ super(self.class)
18
+ @options = OpenStruct.new(:daemonize => true)
19
+ opts = OptionParser.new do |opts|
20
+ opts.banner = 'Usage: myapp [options]'
21
+ opts.separator ''
22
+ opts.on('-N','--no-daemonize',"Don't run as a daemon") do
23
+ @options.daemonize = false
24
+ end
25
+ end
26
+ @args = opts.parse!(args)
27
+ end
28
+
29
+ def run
30
+ Daemons.run_proc('myapp',{:ARGV => @args, :ontop => !@options.daemonize}) do
31
+ puts "@options.daemonize: #{@options.daemonize}"
32
+ STDOUT.sync = true
33
+ loop do
34
+ print '.'
35
+ sleep(2)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ myapp = MyApp.new(ARGV)
43
+ myapp.run
@@ -0,0 +1,25 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :multiple => false,
14
+ :ontop => false,
15
+ :backtrace => true,
16
+ :log_output => true,
17
+ :monitor => true
18
+ }
19
+
20
+ Daemons.run_proc('ctrl_proc.rb', options) do
21
+ loop do
22
+ puts 'ping from proc!'
23
+ sleep(3)
24
+ end
25
+ end
@@ -0,0 +1,121 @@
1
+ ping from proc!
2
+ ping from proc!
3
+ ping from proc!
4
+ ping from proc!
5
+ ping from proc!
6
+ ping from proc!
7
+ ping from proc!
8
+ ping from proc!
9
+ ping from proc!
10
+ ping from proc!
11
+ ping from proc!
12
+ ping from proc!
13
+ ping from proc!
14
+ ping from proc!
15
+ ping from proc!
16
+ ping from proc!
17
+ ping from proc!
18
+ ping from proc!
19
+ ping from proc!
20
+ ping from proc!
21
+ ping from proc!
22
+ ping from proc!
23
+ ping from proc!
24
+ ping from proc!
25
+ ping from proc!
26
+ ping from proc!
27
+ ping from proc!
28
+ ping from proc!
29
+ ping from proc!
30
+ ping from proc!
31
+ ping from proc!
32
+ ping from proc!
33
+ ping from proc!
34
+ ping from proc!
35
+ ping from proc!
36
+ ping from proc!
37
+ ping from proc!
38
+ ping from proc!
39
+ ping from proc!
40
+ ping from proc!
41
+ ping from proc!
42
+ ping from proc!
43
+ ping from proc!
44
+ ping from proc!
45
+ ping from proc!
46
+ ping from proc!
47
+ ping from proc!
48
+ ping from proc!
49
+ ping from proc!
50
+ ping from proc!
51
+ ping from proc!
52
+ ping from proc!
53
+ ping from proc!
54
+ ping from proc!
55
+ ping from proc!
56
+ ping from proc!
57
+ ping from proc!
58
+ ping from proc!
59
+ ping from proc!
60
+ ping from proc!
61
+ ping from proc!
62
+ ping from proc!
63
+ ping from proc!
64
+ ping from proc!
65
+ ping from proc!
66
+ ping from proc!
67
+ ping from proc!
68
+ ping from proc!
69
+ ping from proc!
70
+ ping from proc!
71
+ ping from proc!
72
+ ping from proc!
73
+ ping from proc!
74
+ ping from proc!
75
+ ping from proc!
76
+ ping from proc!
77
+ ping from proc!
78
+ ping from proc!
79
+ ping from proc!
80
+ ping from proc!
81
+ ping from proc!
82
+ ping from proc!
83
+ ping from proc!
84
+ ping from proc!
85
+ ping from proc!
86
+ ping from proc!
87
+ ping from proc!
88
+ ping from proc!
89
+ ping from proc!
90
+ ping from proc!
91
+ ping from proc!
92
+ ping from proc!
93
+ ping from proc!
94
+ ping from proc!
95
+ ping from proc!
96
+ ping from proc!
97
+ ping from proc!
98
+ ping from proc!
99
+ ping from proc!
100
+ ping from proc!
101
+ ping from proc!
102
+ ping from proc!
103
+ ping from proc!
104
+ ping from proc!
105
+ ping from proc!
106
+ ping from proc!
107
+ ping from proc!
108
+ ping from proc!
109
+ ctrl_proc.rb: process with pid 3187 started.
110
+ ping from proc!
111
+ ping from proc!
112
+ ping from proc!
113
+ ctrl_proc.rb: process with pid 1659 started.
114
+ ping from proc!
115
+ ping from proc!
116
+ ctrl_proc.rb: process with pid 17561 started.
117
+ ping from proc!
118
+ ping from proc!
119
+ ctrl_proc.rb: process with pid 18362 started.
120
+ ping from proc!
121
+ ping from proc!