spoon_daemon 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -4
- data/lib/spoon_daemon.rb +21 -9
- data/lib/spoon_daemon/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -24,10 +24,9 @@ Or install it yourself as:
|
|
24
24
|
SpoonDaemon::Runner.new('path/to/my/script.rb', cmd, options)
|
25
25
|
```
|
26
26
|
|
27
|
-
Where cmd is
|
28
|
-
|
29
|
-
|
30
|
-
* `pid_dir`: where to create the PID file
|
27
|
+
Where cmd is one of `start`, `stop` or `restart`, and options are:
|
28
|
+
* `:pid_dir`: where to create the PID file
|
29
|
+
* `:name`: process name, used for the PID filename
|
31
30
|
|
32
31
|
|
33
32
|
## Contributing
|
data/lib/spoon_daemon.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "spoon"
|
2
|
+
require "timeout"
|
2
3
|
|
3
4
|
require "spoon_daemon/version"
|
4
5
|
|
@@ -9,8 +10,10 @@ module SpoonDaemon
|
|
9
10
|
|
10
11
|
@pid_dir = options[:pid_dir] || '.'
|
11
12
|
@name = options[:name] || File.basename(script)
|
13
|
+
@timeout = options[:timeout] || 10
|
12
14
|
|
13
15
|
@pid_path = File.join(@pid_dir, "#{@name}.pid")
|
16
|
+
@pid = get_pid
|
14
17
|
|
15
18
|
case cmd
|
16
19
|
when 'start'
|
@@ -51,29 +54,38 @@ module SpoonDaemon
|
|
51
54
|
def remove_pidfile
|
52
55
|
File.unlink(@pid_path)
|
53
56
|
rescue => e
|
54
|
-
STDERR.puts "ERROR: Unable to unlink #{
|
57
|
+
STDERR.puts "ERROR: Unable to unlink #{@pid_path}:\n\t" +
|
55
58
|
"(#{e.class}) #{e.message}"
|
56
59
|
exit
|
57
60
|
end
|
58
61
|
|
59
62
|
def process_exists?
|
60
|
-
|
61
|
-
|
62
|
-
Process.kill(0, pid)
|
63
|
+
return false unless @pid
|
64
|
+
Process.kill(0, @pid)
|
63
65
|
true
|
64
66
|
rescue Errno::ESRCH, TypeError # "PID is NOT running or is zombied
|
65
67
|
false
|
66
68
|
rescue Errno::EPERM
|
67
|
-
STDERR.puts "No permission to query #{pid}!";
|
69
|
+
STDERR.puts "No permission to query #{@pid}!";
|
68
70
|
false
|
69
71
|
end
|
70
72
|
|
71
73
|
def stop
|
72
|
-
|
73
|
-
|
74
|
+
abort "No process running, nothing to stop" unless process_exists?
|
75
|
+
|
76
|
+
begin
|
77
|
+
STDOUT.puts "Stopping process #{@pid}..."
|
78
|
+
Timeout.timeout(@timeout) do
|
79
|
+
Process.kill("TERM", @pid)
|
80
|
+
sleep 1 while process_exists?
|
81
|
+
end
|
82
|
+
rescue Timeout::Error
|
83
|
+
STDERR.puts "Graceful shutdown timeout, sending KILL signal"
|
84
|
+
Process.kill("KILL", @pid)
|
85
|
+
end
|
86
|
+
|
74
87
|
remove_pidfile
|
75
|
-
|
76
|
-
abort "No process to kill"
|
88
|
+
STDOUT.puts "Process stopped."
|
77
89
|
end
|
78
90
|
|
79
91
|
def start
|
data/lib/spoon_daemon/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: spoon_daemon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Emilien Taque
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-12-
|
13
|
+
date: 2012-12-28 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: spoon
|