pidly 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.rdoc +6 -1
- data/README.md +24 -24
- data/lib/pidly/callbacks.rb +37 -36
- data/lib/pidly/control.rb +62 -43
- data/lib/pidly/version.rb +1 -1
- data/spec/control_spec.rb +14 -12
- metadata +3 -3
data/ChangeLog.rdoc
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# pidly
|
2
2
|
|
3
|
-
* [Homepage]
|
4
|
-
* [Documentation]
|
3
|
+
* [Homepage](https://github.com/mephux/pidly)
|
4
|
+
* [Documentation](http://rubydoc.info/github/mephux/pidly/master/frames)
|
5
5
|
|
6
6
|
## Description
|
7
7
|
|
@@ -10,34 +10,34 @@ daemon without getting in the way with forced verbose output and usage messages.
|
|
10
10
|
|
11
11
|
## Examples
|
12
12
|
|
13
|
-
|
13
|
+
require 'pidly'
|
14
14
|
|
15
|
-
|
15
|
+
class Test < Pidly::Control
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
before_start do
|
18
|
+
puts "BEFORE START #{@pid}"
|
19
|
+
end
|
20
20
|
|
21
|
-
|
21
|
+
start :when_daemon_starts
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
stop do
|
24
|
+
puts "Attempting to kill process: #{@pid}"
|
25
|
+
end
|
26
26
|
|
27
|
-
|
27
|
+
after_stop :test_after_daemon_stops
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
error do
|
30
|
+
puts "SENDING EMAIL | Error Count: #{@error_count}"
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
def when_daemon_starts
|
34
|
+
loop do
|
35
|
+
print "TEST FROM #{@pid}"
|
36
|
+
sleep 2
|
37
|
+
end
|
38
|
+
end
|
39
39
|
|
40
|
-
|
40
|
+
end
|
41
41
|
|
42
42
|
@daemon = Test.spawn(
|
43
43
|
:name => 'Test Daemon',
|
@@ -46,11 +46,11 @@ daemon without getting in the way with forced verbose output and usage messages.
|
|
46
46
|
)
|
47
47
|
|
48
48
|
# @daemon.send ARGV.first
|
49
|
-
@daemon.start # stop, status, restart, and kill.
|
49
|
+
@daemon.start # stop, status, restart, clean!, and kill.
|
50
50
|
|
51
51
|
## Install
|
52
52
|
|
53
|
-
|
53
|
+
$ gem install pidly
|
54
54
|
|
55
55
|
## Copyright
|
56
56
|
|
data/lib/pidly/callbacks.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
module Pidly
|
2
2
|
#
|
3
3
|
# Pidly before/after callbacks
|
4
|
-
#
|
4
|
+
#
|
5
5
|
module Callbacks
|
6
|
-
|
6
|
+
|
7
7
|
#
|
8
8
|
# Before start
|
9
|
-
#
|
10
|
-
# Right before the daemon is instructed to start the
|
9
|
+
#
|
10
|
+
# Right before the daemon is instructed to start the
|
11
11
|
# following callback will be invoked and executed.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# @param [Symbol] callback Method name
|
14
14
|
# @yield [] Code to be executed upon callback invocation
|
15
15
|
#
|
16
|
-
# @example
|
16
|
+
# @example
|
17
17
|
# before_start :method_name
|
18
18
|
# # OR
|
19
19
|
# before_start { puts "#{@pid} is about to start!" }
|
@@ -21,53 +21,53 @@ module Pidly
|
|
21
21
|
def before_start(callback=nil, &block)
|
22
22
|
add_callback(:before_start, (callback || block))
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
#
|
26
26
|
# Start
|
27
|
-
#
|
28
|
-
# When the daemon is instructed to start the
|
27
|
+
#
|
28
|
+
# When the daemon is instructed to start the
|
29
29
|
# following callback will be invoked and executed.
|
30
|
-
#
|
30
|
+
#
|
31
31
|
# @param [Symbol] callback Method name
|
32
32
|
# @yield [] Code to be executed upon callback invocation
|
33
33
|
#
|
34
|
-
# @example
|
34
|
+
# @example
|
35
35
|
# start :method_name
|
36
36
|
# # OR
|
37
37
|
# start { puts "Daemon Started!" }
|
38
|
-
#
|
38
|
+
#
|
39
39
|
def start(callback=nil, &block)
|
40
40
|
add_callback(:start, (callback || block))
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
#
|
44
44
|
# Stop
|
45
|
-
#
|
46
|
-
# When the daemon is instructed to stop the
|
45
|
+
#
|
46
|
+
# When the daemon is instructed to stop the
|
47
47
|
# following callback will be invoked and executed.
|
48
|
-
#
|
48
|
+
#
|
49
49
|
# @param [Symbol] callback Method name
|
50
50
|
# @yield [] Code to be executed upon callback invocation
|
51
|
-
#
|
52
|
-
# @example
|
51
|
+
#
|
52
|
+
# @example
|
53
53
|
# stop :method_name
|
54
54
|
# # OR
|
55
55
|
# stop { puts "Attempting to stop #{@name} with pid #{@pid}!" }
|
56
|
-
#
|
56
|
+
#
|
57
57
|
def stop(callback=nil, &block)
|
58
58
|
add_callback(:stop, (callback || block))
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
#
|
62
62
|
# After stop
|
63
|
-
#
|
64
|
-
# Right after the daemon is instructed to stop the
|
63
|
+
#
|
64
|
+
# Right after the daemon is instructed to stop the
|
65
65
|
# following callback will be invoked and executed.
|
66
|
-
#
|
66
|
+
#
|
67
67
|
# @param [Symbol] callback Method name
|
68
68
|
# @yield [] Code to be executed upon callback invocation
|
69
69
|
#
|
70
|
-
# @example
|
70
|
+
# @example
|
71
71
|
# after_start :method_name
|
72
72
|
# # OR
|
73
73
|
# after_start { puts "#{@pid} was just killed!" }
|
@@ -75,17 +75,17 @@ module Pidly
|
|
75
75
|
def after_stop(callback=nil, &block)
|
76
76
|
add_callback(:after_stop, (callback || block))
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
#
|
80
80
|
# Error
|
81
|
-
#
|
81
|
+
#
|
82
82
|
# If the daemon encounters an error or an exception is raised
|
83
83
|
# the following callback will be invoked and executed.
|
84
|
-
#
|
84
|
+
#
|
85
85
|
# @param [Symbol] callback Method name
|
86
86
|
# @yield [] Code to be executed upon callback invocation
|
87
87
|
#
|
88
|
-
# @example
|
88
|
+
# @example
|
89
89
|
# error :send_error_email
|
90
90
|
# # OR
|
91
91
|
# error { puts "ZOMG! #{@name} failed!" }
|
@@ -96,25 +96,26 @@ module Pidly
|
|
96
96
|
|
97
97
|
#
|
98
98
|
# Add callback
|
99
|
-
#
|
99
|
+
#
|
100
100
|
# @param [Symbol] callback Callback method name
|
101
101
|
# @param [Symbol, nil] invoke Method to call
|
102
102
|
# @yield [] Code to be executed upon callback invocation
|
103
|
-
#
|
103
|
+
#
|
104
104
|
def add_callback(callback, invoke)
|
105
|
-
Control.
|
105
|
+
Control.instance_eval do
|
106
|
+
class_variable_set(:"@@#{callback}", invoke)
|
107
|
+
end
|
106
108
|
end
|
107
109
|
|
108
110
|
#
|
109
111
|
# Extend and include callback methods
|
110
|
-
#
|
112
|
+
#
|
111
113
|
# @param [Class] receiver The calling class
|
112
|
-
#
|
114
|
+
#
|
113
115
|
def self.included(receiver)
|
114
|
-
puts receiver.class
|
115
116
|
receiver.extend self
|
116
117
|
end
|
117
118
|
|
118
119
|
end # modle Callbacks
|
119
|
-
|
120
|
-
end # module Pidly
|
120
|
+
|
121
|
+
end # module Pidly
|
data/lib/pidly/control.rb
CHANGED
@@ -16,7 +16,7 @@ module Pidly
|
|
16
16
|
|
17
17
|
# Include callbacks
|
18
18
|
include Pidly::Callbacks
|
19
|
-
|
19
|
+
|
20
20
|
# Include logging helpers
|
21
21
|
include Pidly::Logger
|
22
22
|
|
@@ -28,13 +28,13 @@ module Pidly
|
|
28
28
|
# Initialize control object
|
29
29
|
#
|
30
30
|
# @param [Hash] options The options to create a controller with.
|
31
|
-
#
|
32
|
-
# @raise [RuntimeError]
|
31
|
+
#
|
32
|
+
# @raise [RuntimeError]
|
33
33
|
# Raise exception if path does not exist
|
34
|
-
#
|
35
|
-
# @raise [RuntimeError]
|
34
|
+
#
|
35
|
+
# @raise [RuntimeError]
|
36
36
|
# Raise exception if path is not readable or writable.
|
37
|
-
#
|
37
|
+
#
|
38
38
|
def initialize(options={})
|
39
39
|
|
40
40
|
@messages = []
|
@@ -88,28 +88,28 @@ module Pidly
|
|
88
88
|
# Spawn
|
89
89
|
#
|
90
90
|
# @param [Hash] options The options to create a controller with.
|
91
|
-
#
|
91
|
+
#
|
92
92
|
# @option options [String] :name Daemon name
|
93
|
-
#
|
93
|
+
#
|
94
94
|
# @option options [String] :path Path to create the log/pids directory
|
95
|
-
#
|
95
|
+
#
|
96
96
|
# @option options [String] :pid_file Pid file path
|
97
|
-
#
|
97
|
+
#
|
98
98
|
# @option options [String] :log_file Log file path
|
99
|
-
#
|
99
|
+
#
|
100
100
|
# @option options [true, false] :sync_log Synchronize log files
|
101
|
-
#
|
102
|
-
# @option options [true, false] :allow_multiple
|
101
|
+
#
|
102
|
+
# @option options [true, false] :allow_multiple
|
103
103
|
# Allow multiple daemons of the same type
|
104
|
-
#
|
104
|
+
#
|
105
105
|
# @option options [true, false] :sync_log Synchronize log files
|
106
|
-
#
|
106
|
+
#
|
107
107
|
# @option options [String] :signal Trap signal
|
108
|
-
#
|
108
|
+
#
|
109
109
|
# @option options [Integer] :timeout Timeout for Process#wait
|
110
|
-
#
|
110
|
+
#
|
111
111
|
# @option options [true, false] :verbose Display daemon messages
|
112
|
-
#
|
112
|
+
#
|
113
113
|
# @option options [true, false] :logger Enable daemon logging
|
114
114
|
#
|
115
115
|
# @return [Control] Control object
|
@@ -117,12 +117,12 @@ module Pidly
|
|
117
117
|
def self.spawn(options={})
|
118
118
|
@daemon = new(options)
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
#
|
122
122
|
# Start
|
123
|
-
#
|
123
|
+
#
|
124
124
|
# Validate callbacks and start daemon
|
125
|
-
#
|
125
|
+
#
|
126
126
|
def start
|
127
127
|
validate_files_and_paths!
|
128
128
|
validate_callbacks!
|
@@ -156,7 +156,7 @@ module Pidly
|
|
156
156
|
STDOUT.reopen log
|
157
157
|
STDERR.reopen STDOUT
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
trap("TERM") do
|
161
161
|
stop
|
162
162
|
end
|
@@ -181,12 +181,12 @@ module Pidly
|
|
181
181
|
STDERR.puts message.backtrace
|
182
182
|
execute_callback(:error)
|
183
183
|
end
|
184
|
-
|
184
|
+
|
185
185
|
#
|
186
186
|
# Stop
|
187
|
-
#
|
187
|
+
#
|
188
188
|
# Stop daemon and remove pid file
|
189
|
-
#
|
189
|
+
#
|
190
190
|
def stop
|
191
191
|
|
192
192
|
if running?
|
@@ -211,19 +211,19 @@ module Pidly
|
|
211
211
|
|
212
212
|
else
|
213
213
|
FileUtils.rm(@pid_file) if File.exists?(@pid_file)
|
214
|
-
log(:info, "PID file not found.
|
214
|
+
log(:info, "PID file not found.")
|
215
215
|
end
|
216
216
|
|
217
217
|
rescue Errno::ENOENT
|
218
218
|
end
|
219
|
-
|
219
|
+
|
220
220
|
#
|
221
221
|
# Status
|
222
|
-
#
|
222
|
+
#
|
223
223
|
# Return current daemon status and pid
|
224
|
-
#
|
224
|
+
#
|
225
225
|
# @return [String] Status
|
226
|
-
#
|
226
|
+
#
|
227
227
|
def status
|
228
228
|
if running?
|
229
229
|
log(:info, "#{@name} is running (PID #{@pid})")
|
@@ -231,21 +231,21 @@ module Pidly
|
|
231
231
|
log(:info, "#{@name} is NOT running")
|
232
232
|
end
|
233
233
|
end
|
234
|
-
|
234
|
+
|
235
235
|
#
|
236
236
|
# Restart
|
237
|
-
#
|
237
|
+
#
|
238
238
|
# Restart the daemon
|
239
|
-
#
|
239
|
+
#
|
240
240
|
def restart
|
241
241
|
stop; sleep 1 while running?; start
|
242
242
|
end
|
243
|
-
|
243
|
+
|
244
244
|
#
|
245
245
|
# Kill
|
246
|
-
#
|
246
|
+
#
|
247
247
|
# @param [String] remove_pid_file Remove the daemon pid file
|
248
|
-
#
|
248
|
+
#
|
249
249
|
def kill(remove_pid_file=true)
|
250
250
|
if running?
|
251
251
|
log(:info, "Killing #{@name} (PID #{@pid})")
|
@@ -255,12 +255,12 @@ module Pidly
|
|
255
255
|
FileUtils.rm(@pid_file) if remove_pid_file
|
256
256
|
rescue Errno::ENOENT
|
257
257
|
end
|
258
|
-
|
258
|
+
|
259
259
|
#
|
260
260
|
# Running?
|
261
|
-
#
|
261
|
+
#
|
262
262
|
# @return [true, false] Return the running status of the daemon.
|
263
|
-
#
|
263
|
+
#
|
264
264
|
def running?
|
265
265
|
Process.kill 0, @pid
|
266
266
|
true
|
@@ -271,7 +271,18 @@ module Pidly
|
|
271
271
|
rescue
|
272
272
|
false
|
273
273
|
end
|
274
|
-
|
274
|
+
|
275
|
+
#
|
276
|
+
# Clean
|
277
|
+
#
|
278
|
+
# Remove all files created by the daemon.
|
279
|
+
#
|
280
|
+
def clean!
|
281
|
+
FileUtils.rm(@log_file)
|
282
|
+
FileUtils.rm(@pid_file)
|
283
|
+
rescue Errno::ENOENT
|
284
|
+
end
|
285
|
+
|
275
286
|
def validate_files_and_paths!
|
276
287
|
log = Pathname.new(@log_file).dirname
|
277
288
|
pid = Pathname.new(@pid_file).dirname
|
@@ -294,8 +305,7 @@ module Pidly
|
|
294
305
|
def execute_callback(callback_name)
|
295
306
|
@error_count += 1 if callback_name == :error
|
296
307
|
|
297
|
-
if
|
298
|
-
callback = Control.class_variable_get(:"@@#{callback_name}")
|
308
|
+
if (callback = fetch_class_var(callback_name))
|
299
309
|
|
300
310
|
if callback.kind_of?(Symbol)
|
301
311
|
|
@@ -323,8 +333,17 @@ module Pidly
|
|
323
333
|
nil
|
324
334
|
end
|
325
335
|
|
336
|
+
def fetch_class_var(name)
|
337
|
+
if Control.class_variable_defined?(:"@@#{name}")
|
338
|
+
Control.instance_eval do
|
339
|
+
return class_variable_get(:"@@#{name}")
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
326
344
|
private :validate_callbacks!, :fetch_pid,
|
327
|
-
:validate_files_and_paths!, :execute_callback
|
345
|
+
:validate_files_and_paths!, :execute_callback,
|
346
|
+
:fetch_class_var
|
328
347
|
|
329
348
|
end # class Control
|
330
349
|
|
data/lib/pidly/version.rb
CHANGED
data/spec/control_spec.rb
CHANGED
@@ -17,49 +17,51 @@ describe Control do
|
|
17
17
|
it "should be running" do
|
18
18
|
@daemon.running?.should == true
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it "should have an active status, name and pid" do
|
22
22
|
@daemon.status
|
23
23
|
reply = "#{@daemon.name} is running (PID #{@daemon.pid})"
|
24
24
|
@daemon.messages.last.should == reply
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
it "should have the correct pid path" do
|
28
28
|
@daemon.pid_file.should == "/tmp/pids/#{@daemon.name}.pid"
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "should have a pid file that exists" do
|
32
32
|
File.exists?("/tmp/pids/#{@daemon.name}.pid").should == true
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should have the correct log file" do
|
36
36
|
@daemon.log_file.should == "/tmp/logs/#{@daemon.name}.log"
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "should have a log file that exists" do
|
40
40
|
File.exists?("/tmp/logs/#{@daemon.name}.log").should == true
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it "should fail when trying to start another daemon" do
|
44
44
|
@daemon.start
|
45
45
|
reply = "#{@daemon.name} is already running (PID #{@daemon.pid})"
|
46
|
-
|
46
|
+
|
47
47
|
@daemon.messages.last.should == reply
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it "should write to the log file" do
|
51
51
|
file = File.open(@daemon.log_file, 'r')
|
52
52
|
file.read.should =~ /TEST FROM #{@daemon.pid}/
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
it "should write to the pid file" do
|
56
56
|
file = File.open(@daemon.pid_file, 'r')
|
57
57
|
file.read.should =~ /#{@daemon.pid}/
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
after(:all) do
|
61
|
-
|
62
|
-
|
61
|
+
if @daemon
|
62
|
+
@daemon.kill
|
63
|
+
@daemon.clean!
|
64
|
+
end
|
63
65
|
end
|
64
66
|
|
65
67
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: pidly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Dustin Willis Webber
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-07 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
99
|
requirements: []
|
100
100
|
|
101
101
|
rubyforge_project: pidly
|
102
|
-
rubygems_version: 1.
|
102
|
+
rubygems_version: 1.5.0
|
103
103
|
signing_key:
|
104
104
|
specification_version: 3
|
105
105
|
summary: A super simple daemon library.
|