daemons 1.1.9 → 1.2.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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +1 -1
  3. data/README.md +206 -0
  4. data/Releases +17 -0
  5. data/examples/call/call.rb +13 -16
  6. data/examples/call/call_monitor.rb +13 -17
  7. data/examples/daemonize/daemonize.rb +4 -8
  8. data/examples/run/ctrl_crash.rb +0 -1
  9. data/examples/run/ctrl_custom_logfiles.rb +18 -0
  10. data/examples/run/ctrl_exec.rb +0 -1
  11. data/examples/run/ctrl_exit.rb +0 -1
  12. data/examples/run/ctrl_keep_pid_files.rb +1 -3
  13. data/examples/run/ctrl_monitor.rb +0 -1
  14. data/examples/run/ctrl_monitor_multiple.rb +17 -0
  15. data/examples/run/ctrl_multiple.rb +0 -1
  16. data/examples/run/ctrl_ontop.rb +0 -1
  17. data/examples/run/ctrl_optionparser.rb +4 -6
  18. data/examples/run/ctrl_proc.rb +8 -9
  19. data/examples/run/ctrl_proc_multiple.rb +4 -6
  20. data/examples/run/ctrl_proc_rand.rb +2 -4
  21. data/examples/run/ctrl_proc_simple.rb +0 -1
  22. data/examples/run/myserver.rb +0 -1
  23. data/examples/run/myserver_crashing.rb +5 -5
  24. data/examples/run/myserver_exiting.rb +2 -2
  25. data/examples/run/myserver_hanging.rb +4 -5
  26. data/examples/run/myserver_slowstop.rb +5 -6
  27. data/lib/daemons.rb +66 -68
  28. data/lib/daemons/application.rb +171 -188
  29. data/lib/daemons/application_group.rb +99 -92
  30. data/lib/daemons/change_privilege.rb +3 -3
  31. data/lib/daemons/cmdline.rb +43 -54
  32. data/lib/daemons/controller.rb +36 -53
  33. data/lib/daemons/daemonize.rb +54 -64
  34. data/lib/daemons/etc_extension.rb +3 -2
  35. data/lib/daemons/exceptions.rb +10 -11
  36. data/lib/daemons/monitor.rb +60 -62
  37. data/lib/daemons/pid.rb +24 -56
  38. data/lib/daemons/pidfile.rb +38 -40
  39. data/lib/daemons/pidmem.rb +5 -9
  40. data/lib/daemons/version.rb +3 -0
  41. metadata +45 -45
  42. data/README +0 -214
  43. data/Rakefile +0 -90
  44. data/TODO +0 -2
  45. data/setup.rb +0 -1360
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e59f2e1546ffdb0c31f593a15204e5a027e64392
4
+ data.tar.gz: da329109adb90ff065d7ae0e7dd6b0238d83862d
5
+ SHA512:
6
+ metadata.gz: 9e80ab5277dd3607978f6f5ccb23c95088f4dcaec7ecee4059a7649d071bc0c4451f6ece2f415c6900182a19b2e6abd5c8502b59876faa6cac66ec3eefee9eca
7
+ data.tar.gz: 0ce93e84a5ef74e07593193bcff94fec8d48687d0e9f976a2d289139ab7c74eb75c9e71811f696a5135eecc8836392ec56b95207982449ad8cd2ed8e19c89376
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2005-2012 Thomas Uehlinger
1
+ Copyright (c) 2005-2015 Thomas Uehlinger, 2014-2015 Aaron Stone
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
data/README.md ADDED
@@ -0,0 +1,206 @@
1
+ Ruby Daemons
2
+ ============
3
+
4
+ Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server)
5
+ to be _run as a daemon_ and to be _controlled by simple start/stop/restart commands_.
6
+
7
+ If you want, you can also use daemons to _run blocks of ruby code in a daemon process_ and to control
8
+ these processes from the main application.
9
+
10
+ Besides this basic functionality, daemons offers many advanced features like _exception backtracing_
11
+ and logging (in case your ruby script crashes) and _monitoring_ and automatic restarting of your processes
12
+ if they crash.
13
+
14
+ Basic Usage
15
+ -----------
16
+
17
+ You can use Daemons in four different ways:
18
+
19
+ ### 1. Create wrapper scripts for your server scripts or applications
20
+
21
+ Layout: suppose you have your self-written server `myserver.rb`:
22
+
23
+ ``` ruby
24
+ # this is myserver.rb
25
+ # it does nothing really useful at the moment
26
+
27
+ loop do
28
+ sleep(5)
29
+ end
30
+ ```
31
+
32
+ To use `myserver.rb` in a production environment, you need to be able to
33
+ run `myserver.rb` in the _background_ (this means detach it from the console, fork it
34
+ in the background, release all directories and file descriptors).
35
+
36
+ Just create `myserver_control.rb` like this:
37
+
38
+ ``` ruby
39
+ # this is myserver_control.rb
40
+ require 'daemons'
41
+
42
+ Daemons.run('myserver.rb')
43
+ ```
44
+
45
+ And use it like this from the console:
46
+
47
+ ``` ruby
48
+ $ ruby myserver_control.rb start
49
+ (myserver.rb is now running in the background)
50
+ $ ruby myserver_control.rb restart
51
+ (...)
52
+ $ ruby myserver_control.rb stop
53
+ ```
54
+
55
+ For testing purposes you can even run `myserver.rb` _without forking_ in the background:
56
+
57
+ ``` ruby
58
+ $ ruby myserver_control.rb run
59
+ ```
60
+
61
+ An additional nice feature of Daemons is that you can pass _additional arguments_ to the script that
62
+ should be daemonized by seperating them by two _hyphens_:
63
+
64
+ ``` ruby
65
+ $ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument
66
+ ```
67
+
68
+
69
+ ### 2. Create wrapper scripts that include your server procs
70
+
71
+ Layout: suppose you have some code you want to run in the background and control that background process
72
+ from a script:
73
+
74
+ ``` ruby
75
+ # this is your code
76
+ # it does nothing really useful at the moment
77
+
78
+ loop do
79
+ sleep(5)
80
+ end
81
+ ```
82
+
83
+ To run this code as a daemon create `myproc_control.rb` like this and include your code:
84
+
85
+ ``` ruby
86
+ # this is myproc_control.rb
87
+ require 'daemons'
88
+
89
+ Daemons.run_proc('myproc.rb') do
90
+ loop do
91
+ sleep(5)
92
+ end
93
+ end
94
+ ```
95
+
96
+ And use it like this from the console:
97
+
98
+ ``` ruby
99
+ $ ruby myproc_control.rb start
100
+ (myproc.rb is now running in the background)
101
+ $ ruby myproc_control.rb restart
102
+ (...)
103
+ $ ruby myproc_control.rb stop
104
+ ```
105
+
106
+ For testing purposes you can even run `myproc.rb` _without forking_ in the background:
107
+
108
+ ``` ruby
109
+ $ ruby myproc_control.rb run
110
+ ```
111
+
112
+ ### 3. Control a bunch of daemons from another application
113
+
114
+ Layout: you have an application `my_app.rb` that wants to run a bunch of
115
+ server tasks as daemon processes.
116
+
117
+ ``` ruby
118
+ # this is my_app.rb
119
+ require 'daemons'
120
+
121
+ task1 = Daemons.call(:multiple => true) do
122
+ # first server task
123
+
124
+ loop do
125
+ conn = accept_conn()
126
+ serve(conn)
127
+ end
128
+ end
129
+
130
+ task2 = Daemons.call do
131
+ # second server task
132
+
133
+ loop do
134
+ something_different()
135
+ end
136
+ end
137
+
138
+ # the parent process continues to run
139
+
140
+ # we can even control our tasks, for example stop them
141
+ task1.stop
142
+ task2.stop
143
+
144
+ exit
145
+ ```
146
+
147
+ ### 4. Daemonize the currently running process
148
+
149
+ Layout: you have an application `my_daemon.rb` that wants to run as a daemon
150
+ (but without the ability to be controlled by daemons via start/stop commands)
151
+
152
+ ``` ruby
153
+ # this is my_daemons.rb
154
+ require 'daemons'
155
+
156
+ # Initialize the app while we're not a daemon
157
+ init()
158
+
159
+ # Become a daemon
160
+ Daemons.daemonize
161
+
162
+ # The server loop
163
+ loop do
164
+ conn = accept_conn()
165
+ serve(conn)
166
+ end
167
+ ```
168
+
169
+ For further documentation, refer to the module documentation of Daemons.
170
+
171
+ Displaying daemon status
172
+ ------------------------
173
+
174
+ When daemonizing a process using a wrapper script, as examples 1 and 2 above,
175
+ the status can be shown using
176
+
177
+ ``` ruby
178
+ $ ruby myproc_control.rb status
179
+ ```
180
+
181
+ By default this will display whether or not the daemon is running and, if it
182
+ is, its PID.
183
+
184
+ A custom message can be shown with
185
+
186
+ ``` ruby
187
+ def custom_show_status(app)
188
+ # Display the default status information
189
+ app.default_show_status
190
+
191
+ puts
192
+ puts "PS information"
193
+ system("ps -p #{app.pid.pid.to_s}")
194
+
195
+ puts
196
+ puts "Size of log files"
197
+ system("du -hs /path/to/logs")
198
+ end
199
+
200
+ Daemons.run('myserver.rb', { show_status_callback: :custom_show_status })
201
+ ```
202
+
203
+ Author
204
+ ------
205
+
206
+ Written 2005-2015 by Thomas Uehlinger <thomas.uehlinger@gmail.com>, 2014-2015 by Aaron Stone <aaron@serendipity.cx>.
data/Releases CHANGED
@@ -1,5 +1,22 @@
1
1
  = Daemons Release History
2
2
 
3
+ == Release 1.2.1: March 10, 2015
4
+
5
+ * increase version number to be able to re-push to rubygems
6
+
7
+ == Release 1.2.0: March 8, 2015
8
+
9
+ * add options for custum log file names.
10
+ * change pid file name scheme to "#progname_num#{number}.pid" for multiple instances.
11
+ * fix call_as_daemon not saving the PID (thanks Roberto Plancarte)
12
+ * allow for custom statis messages (thanks to Joseph Haig)
13
+ * fix Pid.running? rescuing timeout exceptions (thanks to Geraud Boyer)
14
+ * monitor.rb/application.rb/application_group.rb: handle :monitor and :multiple in combination correctly
15
+ (thanks to Prakash Murthy).
16
+ * pidfile.rb: Handle invalid or empty pid files instead of returning pid 0 (thanks to Aaron Stone)
17
+ * run the whole gem through Rubocop (thanks to Aaron Stone)
18
+ * gem cleanup (thanks to Aaron Stone)
19
+
3
20
  == Release 1.1.9: August 10, 2012
4
21
 
5
22
  * daemonize.rb: do srand in the forked child process both in daemonize and call_as_daemon
@@ -6,12 +6,10 @@ else
6
6
  begin; require 'rubygems'; rescue ::Exception; end
7
7
  end
8
8
 
9
-
10
9
  require 'daemons'
11
10
 
12
11
  testfile = File.expand_path(__FILE__) + '.log'
13
12
 
14
-
15
13
  # On the first call to <tt<call</tt>, an application group (accessible by <tt>Daemons.group</tt>)
16
14
  # will be created an the options will be kept within, so you only have to specify
17
15
  # <tt>:multiple</tt> once.
@@ -23,35 +21,34 @@ options = {
23
21
  :multiple => true
24
22
  }
25
23
 
26
-
27
24
  Daemons.call(options) do
28
- File.open(testfile, 'w') {|f|
29
- f.puts "test"
30
- }
25
+ File.open(testfile, 'w') do |f|
26
+ f.puts 'test'
27
+ end
31
28
 
32
- loop { puts "1"; sleep 5 }
29
+ loop { puts '1'; sleep 5 }
33
30
  end
34
- puts "first task started"
31
+ puts 'first task started'
35
32
 
36
33
  Daemons.call do
37
- loop { puts "2"; sleep 4 }
34
+ loop { puts '2'; sleep 4 }
38
35
  end
39
- puts "second task started"
36
+ puts 'second task started'
40
37
 
41
38
  # NOTE: this process will exit after 5 seconds
42
39
  Daemons.call do
43
- puts "3"
40
+ puts '3'
44
41
  sleep 5
45
42
  end
46
- puts "third task started"
43
+ puts 'third task started'
47
44
 
48
- puts "waiting 20 seconds..."
45
+ puts 'waiting 20 seconds...'
49
46
  sleep(20)
50
47
 
51
- # This call would result in an exception as it will try to kill the third process
48
+ # This call would result in an exception as it will try to kill the third process
52
49
  # which has already terminated by that time; but using the 'true' parameter forces the
53
50
  # stop_all procedure.
54
- puts "trying to stop all tasks..."
51
+ puts 'trying to stop all tasks...'
55
52
  Daemons.group.stop_all(true)
56
53
 
57
- puts "done"
54
+ puts 'done'
@@ -6,12 +6,10 @@ else
6
6
  begin; require 'rubygems'; rescue ::Exception; end
7
7
  end
8
8
 
9
-
10
9
  require 'daemons'
11
10
 
12
11
  testfile = File.expand_path(__FILE__) + '.log'
13
12
 
14
-
15
13
  # On the first call to <tt<call</tt>, an application group (accessible by <tt>Daemons.group</tt>)
16
14
  # will be created an the options will be kept within, so you only have to specify
17
15
  # <tt>:multiple</tt> once.
@@ -23,33 +21,31 @@ options = {
23
21
  :monitor => true
24
22
  }
25
23
 
26
-
27
24
  Daemons.call(options) do
28
- loop { puts "1"; sleep 20 }
25
+ loop { puts '1'; sleep 20 }
29
26
  end
30
- puts "first task started"
31
-
27
+ puts 'first task started'
32
28
 
33
29
  # NOTE: this process will exit after 5 seconds
34
30
  Daemons.call do
35
- File.open(testfile, 'a') {|f|
36
- f.puts "started..."
37
- puts "2"
38
-
31
+ File.open(testfile, 'a') do |f|
32
+ f.puts 'started...'
33
+ puts '2'
34
+
39
35
  sleep 5
40
36
 
41
- f.puts "...exit"
42
- }
37
+ f.puts '...exit'
38
+ end
43
39
  end
44
- puts "second task started"
40
+ puts 'second task started'
45
41
 
46
- puts "waiting 100 seconds..."
42
+ puts 'waiting 100 seconds...'
47
43
  sleep(100)
48
44
 
49
- # This call would result in an exception as it will try to kill the third process
45
+ # This call would result in an exception as it will try to kill the third process
50
46
  # which has already terminated by that time; but using the 'true' parameter forces the
51
47
  # stop_all procedure.
52
- puts "trying to stop all tasks..."
48
+ puts 'trying to stop all tasks...'
53
49
  Daemons.group.stop_all(true)
54
50
 
55
- puts "done"
51
+ puts 'done'
@@ -6,22 +6,18 @@ else
6
6
  begin; require 'rubygems'; rescue ::Exception; end
7
7
  end
8
8
 
9
-
10
-
11
9
  require 'daemons'
12
10
 
13
-
14
11
  options = {
15
12
  :log_output => true
16
13
  }
17
14
 
18
-
19
15
  testfile = File.expand_path(__FILE__) + '.txt'
20
16
 
21
17
  Daemons.daemonize(options)
22
18
 
23
- puts "some output..."
19
+ puts 'some output...'
24
20
 
25
- File.open(testfile, 'w') {|f|
26
- f.write("test")
27
- }
21
+ File.open(testfile, 'w') do |f|
22
+ f.write('test')
23
+ end
@@ -8,7 +8,6 @@ end
8
8
 
9
9
  require 'daemons'
10
10
 
11
-
12
11
  options = {
13
12
  :log_output => true,
14
13
  :backtrace => true
@@ -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
+ options = {
12
+ :log_output => true,
13
+ :backtrace => true,
14
+ :output_logfilename => "custom_output.txt",
15
+ :logfilename => "custom_log.log"
16
+ }
17
+
18
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options)
@@ -8,7 +8,6 @@ end
8
8
 
9
9
  require 'daemons'
10
10
 
11
-
12
11
  options = {
13
12
  :mode => :exec
14
13
  }
@@ -8,7 +8,6 @@ end
8
8
 
9
9
  require 'daemons'
10
10
 
11
-
12
11
  options = {
13
12
  }
14
13
 
@@ -8,10 +8,8 @@ end
8
8
 
9
9
  require 'daemons'
10
10
 
11
-
12
11
  options = {
13
12
  :keep_pid_files => true
14
13
  }
15
14
 
16
-
17
- Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)
15
+ Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options)