daemons 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = Daemons Version 0.4.3
1
+ = Daemons Version 0.4.4
2
2
 
3
3
  (See Releases for release-specific information)
4
4
 
@@ -19,7 +19,7 @@ process.
19
19
 
20
20
  == Basic Usage
21
21
 
22
- You can use Daemons in three differet ways:
22
+ You can use Daemons in four differet ways:
23
23
 
24
24
  === 1. Create wrapper scripts for your server scripts or applications
25
25
 
@@ -62,8 +62,45 @@ should be daemonized by seperating them by two _hyphens_:
62
62
 
63
63
  $ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument
64
64
 
65
+
66
+ === 2. Create wrapper scripts that include your server procs
67
+
68
+ Layout: suppose you have some code you want to run in the background and control that background process
69
+ from a script:
70
+
71
+ # this is your code
72
+ # it does nothing really useful at the moment
73
+
74
+ loop do
75
+ sleep(5)
76
+ end
77
+
78
+ To run this code as a daemon create <tt>myproc_control.rb</tt> like this and include your code:
79
+
80
+ # this is myproc_control.rb
81
+
82
+ require 'rubygems' # if you use RubyGems
83
+ require 'daemons'
84
+
85
+ Daemons.run_proc('myproc.rb') do
86
+ loop do
87
+ sleep(5)
88
+ end
89
+ end
90
+
91
+ And use it like this from the console:
92
+
93
+ $ ruby myproc_control.rb start
94
+ (myproc.rb is now running in the background)
95
+ $ ruby myproc_control.rb restart
96
+ (...)
97
+ $ ruby myproc_control.rb stop
98
+
99
+ For testing purposes you can even run <tt>myproc.rb</tt> <i>without forking</i> in the background:
100
+
101
+ $ ruby myproc_control.rb run
65
102
 
66
- === 2. Control a bunch of daemons from another application
103
+ === 3. Control a bunch of daemons from another application
67
104
 
68
105
  Layout: you have an application <tt>my_app.rb</tt> that wants to run a bunch of
69
106
  server tasks as daemon processes.
@@ -98,7 +135,7 @@ server tasks as daemon processes.
98
135
 
99
136
  exit
100
137
 
101
- === 3. Daemonize the currently running process
138
+ === 4. Daemonize the currently running process
102
139
 
103
140
  Layout: you have an application <tt>my_daemon.rb</tt> that wants to run as a daemon
104
141
  (but without the ability to be controlled by daemons via start/stop commands)
data/Releases CHANGED
@@ -1,5 +1,13 @@
1
1
  = Daemons Release History
2
2
 
3
+ == Release 0.4.4: February 14, 2006
4
+
5
+ * Several fixes that allow us to use the Daemons::Controller
6
+ with a proc instead of wrapping a script file. This gives us all the
7
+ PID file management, monitoring, command line options, etc. without having
8
+ to specify a path to our script which can be tricky, especially when using
9
+ RubyGems. (thanks to John-Mason Shackelford)
10
+
3
11
  == Release 0.4.3: November 29, 2005
4
12
 
5
13
  * New Option: You can specify the name of the application with :app_name
File without changes
@@ -0,0 +1,25 @@
1
+ lib_dir = File.expand_path(File.join(File.split(__FILE__)[0], '../../lib'))
2
+
3
+ if File.exists?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ require 'rubygems' rescue nil
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
File without changes
data/lib/daemons.rb CHANGED
@@ -15,19 +15,25 @@ require 'daemons/controller'
15
15
 
16
16
  # All functions and classes that Daemons provides reside in this module.
17
17
  #
18
- # Daemons is normally invoked by on of the following three ways:
18
+ # Daemons is normally invoked by one of the following four ways:
19
19
  #
20
20
  # 1. <tt>Daemons.run(script, options)</tt>:
21
21
  # This is used in wrapper-scripts that are supposed to control other ruby scripts or
22
22
  # external applications. Control is completely passed to the daemons library.
23
- # Such wrapper script should be invoked with command line options like 'start' or 'stop'
23
+ # Such wrapper script need to be invoked with command line options like 'start' or 'stop'
24
24
  # to do anything useful.
25
25
  #
26
- # 2. <tt>Daemons.call(options) { block }</tt>:
26
+ # 2. <tt>Daemons.run_proc(app_name, options) { (...) }</tt>:
27
+ # This is used in wrapper-scripts that are supposed to control a proc.
28
+ # Control is completely passed to the daemons library.
29
+ # Such wrapper script need to be invoked with command line options like 'start' or 'stop'
30
+ # to do anything useful.
31
+ #
32
+ # 3. <tt>Daemons.call(options) { block }</tt>:
27
33
  # Execute the block in a new daemon. <tt>Daemons.call</tt> will return immediately
28
34
  # after spawning the daemon with the new Application object as a return value.
29
35
  #
30
- # 3. <tt>Daemons.daemonize(options)</tt>:
36
+ # 4. <tt>Daemons.daemonize(options)</tt>:
31
37
  # Daemonize the currently runnig process, i.e. the calling process will become a daemon.
32
38
  #
33
39
  # == What does daemons internally do with my daemons?
@@ -59,7 +65,7 @@ require 'daemons/controller'
59
65
  #
60
66
  module Daemons
61
67
 
62
- VERSION = "0.4.3"
68
+ VERSION = "0.4.4"
63
69
 
64
70
  require 'daemons/daemonize'
65
71
 
@@ -120,8 +126,6 @@ module Daemons
120
126
  options[:script] = script
121
127
  @controller = Controller.new(options, ARGV)
122
128
 
123
- #pp @controller
124
-
125
129
  @controller.catch_exceptions {
126
130
  @controller.run
127
131
  }
@@ -132,6 +136,54 @@ module Daemons
132
136
  module_function :run
133
137
 
134
138
 
139
+ # Passes control to Daemons.
140
+ # This function does the same as Daemons.run except that not a script but a proc
141
+ # will be run as a daemon while this script provides command line options like 'start' or 'stop'
142
+ # and the whole pid-file management to control the proc.
143
+ #
144
+ # +app_name+:: The name of the application. This will be
145
+ # used to contruct the name of the pid files
146
+ # and log files. Defaults to the basename of
147
+ # the script.
148
+ #
149
+ # +options+:: A hash that may contain one or more of the options listed in the documentation for Daemons.run
150
+ #
151
+ # A block must be given to this function. The block will be used as the :proc entry in the options hash.
152
+ # -----
153
+ #
154
+ # === Example:
155
+ #
156
+ # Daemons.run_proc('myproc.rb') do
157
+ # loop do
158
+ # accept_connection()
159
+ # read_request()
160
+ # send_response()
161
+ # close_connection()
162
+ # end
163
+ # end
164
+ #
165
+ def run_proc(app_name, options = {}, &block)
166
+ options[:app_name] = app_name
167
+ options[:mode] = :proc
168
+ options[:proc] = block
169
+
170
+ if [nil, :script].include? options[:dir_mode]
171
+ options[:dir_mode] = :normal
172
+ options[:dir] = File.split(__FILE__)[0]
173
+ end
174
+
175
+ @controller = Controller.new(options, ARGV)
176
+
177
+ @controller.catch_exceptions {
178
+ @controller.run
179
+ }
180
+
181
+ # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions
182
+ @group = @controller.group
183
+ end
184
+ module_function :run_proc
185
+
186
+
135
187
  # Execute the block in a new daemon. <tt>Daemons.call</tt> will return immediately
136
188
  # after spawning the daemon with the new Application object as a return value.
137
189
  #
@@ -205,8 +257,6 @@ module Daemons
205
257
  # }
206
258
  #
207
259
  def daemonize(options = {})
208
- #Daemonize::daemonize
209
-
210
260
  @group ||= ApplicationGroup.new('self', options)
211
261
 
212
262
  @group.new_application(:mode => :none).start
@@ -50,9 +50,7 @@ module Daemons
50
50
  # all running instances of the application and populates the application array.
51
51
  #
52
52
  def setup
53
- if script
54
- @applications = find_applications(pidfile_dir())
55
- end
53
+ @applications = find_applications(pidfile_dir())
56
54
  end
57
55
 
58
56
  def pidfile_dir
@@ -32,6 +32,8 @@ module Daemons
32
32
  @app_name ||= File.split(@script)[1]
33
33
  end
34
34
 
35
+ @app_name = options[:app_name] if options[:app_name]
36
+
35
37
  @command, @controller_part, @app_part = Controller.split_argv(argv)
36
38
 
37
39
  #@options[:dir_mode] ||= :script
data/lib/daemons/pid.rb CHANGED
@@ -18,7 +18,8 @@ module Daemons
18
18
  # If no valid directory is found, returns nil.
19
19
  #
20
20
  def Pid.dir(dir_mode, dir, script)
21
- return nil unless script
21
+ # nil script parameter is allowed so long as dir_mode is not :script
22
+ return nil if dir_mode == :script && script.nil?
22
23
 
23
24
  case dir_mode
24
25
  when :normal
metadata CHANGED
@@ -1,10 +1,10 @@
1
- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: daemons
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.3
7
- date: 2005-11-29 00:00:00 +01:00
6
+ version: 0.4.4
7
+ date: 2006-04-07 00:00:00 +02:00
8
8
  summary: A toolkit to create and control daemons in different ways
9
9
  require_paths:
10
10
  - lib
@@ -56,10 +56,13 @@ files:
56
56
  - examples/run/ctrl_exit.rb
57
57
  - examples/run/ctrl_multiple.rb
58
58
  - examples/run/myserver_crashing.rb.output
59
+ - examples/run/ctrl_proc.output
59
60
  - examples/run/ctrl_normal.rb
60
61
  - examples/run/ctrl_monitor.rb
61
62
  - examples/run/myserver.rb
62
63
  - examples/run/myserver_crashing.rb
64
+ - examples/run/ctrl_proc.rb
65
+ - examples/run/ctrl_proc.rb.output
63
66
  - examples/run/ctrl_ontop.rb
64
67
  - examples/run/myserver_exiting.rb
65
68
  - examples/run/ctrl_crash.rb