insidious 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +64 -2
- data/lib/insidious.rb +47 -93
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8485070f7631adf42bf17e80f53a3f0d19a4f174
|
4
|
+
data.tar.gz: 7f3b4f5e89e271c78288c53a862ff55a22d1bc8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9afb4f6f5fa50d56fcd7a43d70b4e3493dbe60feff439f40b9135e9b43052dcf5813b5bcc4458a8d0f11baffa2a66ff81115fe14301c68c33d9f4912f05c1424
|
7
|
+
data.tar.gz: e70ee2281b526eb160b3dcc1a4c94fa80321a44163b43fe0505bd73702733cb746f1f2f7ef1bccff90e38ee8f202b1d3fb94478dbd692f5bd0bf8213e524fb7d
|
data/README.md
CHANGED
@@ -3,9 +3,71 @@ Insidious [![Gem Version](http://img.shields.io/gem/v/insidious.svg)](https://ru
|
|
3
3
|
|
4
4
|
A simple and flexible ruby gem for managing daemons.
|
5
5
|
|
6
|
-
###
|
6
|
+
### Configuration
|
7
7
|
|
8
|
-
|
8
|
+
````ruby
|
9
|
+
insidious = new Insidious(
|
10
|
+
:pid_file => '/path/where/the/pid/will/be/saved',
|
11
|
+
:daemonize => true, # can be true or false but defaults to being true
|
12
|
+
)
|
13
|
+
````
|
14
|
+
|
15
|
+
### Start a daemon
|
16
|
+
|
17
|
+
````ruby
|
18
|
+
insidious = Insidious.new(:pid_file => '/tmp/insidious.pid')
|
19
|
+
|
20
|
+
insidious.start! do
|
21
|
+
while true
|
22
|
+
puts Time.now.utc
|
23
|
+
sleep 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
````
|
27
|
+
|
28
|
+
### Stop a daemon
|
29
|
+
|
30
|
+
````ruby
|
31
|
+
insidious = Insidious.new(:pid_file => '/tmp/insidious.pid')
|
32
|
+
|
33
|
+
insidious.start! { your_app }
|
34
|
+
|
35
|
+
insidious.stop!
|
36
|
+
````
|
37
|
+
|
38
|
+
### Restart a daemon
|
39
|
+
|
40
|
+
````ruby
|
41
|
+
insidious = Insidious.new(:pid_file => '/tmp/insidious.pid')
|
42
|
+
|
43
|
+
insidious.start! { your_app }
|
44
|
+
|
45
|
+
insidious.restart! { your_app }
|
46
|
+
````
|
47
|
+
|
48
|
+
### Check the status of a daemon
|
49
|
+
|
50
|
+
````ruby
|
51
|
+
insidious = Insidious.new(:pid_file => '/tmp/insidious.pid')
|
52
|
+
|
53
|
+
insidious.start! { your_app }
|
54
|
+
|
55
|
+
insidious.running? # => true
|
56
|
+
|
57
|
+
insidious.stop!
|
58
|
+
|
59
|
+
insidious.running? # => false
|
60
|
+
````
|
61
|
+
|
62
|
+
### Get the process id of a daemon
|
63
|
+
|
64
|
+
````ruby
|
65
|
+
insidious = Insidious.new(:pid_file => '/tmp/insidious.pid')
|
66
|
+
|
67
|
+
insidious.start! { your_app }
|
68
|
+
|
69
|
+
insidious.pid # This will read from /tmp/insidious.pid
|
70
|
+
````
|
9
71
|
|
10
72
|
### Credit
|
11
73
|
|
data/lib/insidious.rb
CHANGED
@@ -1,43 +1,12 @@
|
|
1
1
|
require 'error'
|
2
2
|
|
3
3
|
class Insidious
|
4
|
-
|
5
|
-
attr_accessor :pid
|
6
|
-
attr_accessor :stdin
|
7
|
-
attr_accessor :stdout
|
8
|
-
attr_accessor :stderr
|
4
|
+
attr_reader :pid_file
|
9
5
|
|
6
|
+
# Intiailise Insidious, note the correct spelling of initialise.
|
10
7
|
def initialize(options = {})
|
11
8
|
@daemonize = options[:daemonize].nil? ? true : options[:daemonize]
|
12
|
-
@pid_file = options[:pid_file]
|
13
|
-
@stdin = options[:stdin]
|
14
|
-
@stdout = options[:stdout]
|
15
|
-
@stderr = options[:stderr]
|
16
|
-
end
|
17
|
-
|
18
|
-
# Runs the daemon
|
19
|
-
#
|
20
|
-
# This will set up `INT` & `TERM` signal handlers to stop execution
|
21
|
-
# properly. When this signal handlers are called it will also call
|
22
|
-
# the #interrupt method and delete the pid file
|
23
|
-
def run!(&block)
|
24
|
-
begin
|
25
|
-
if @daemonize
|
26
|
-
Process.daemon(true, (stdin || stdout || stderr))
|
27
|
-
end
|
28
|
-
|
29
|
-
save_pid_file
|
30
|
-
|
31
|
-
block.call
|
32
|
-
rescue Interrupt, SignalException
|
33
|
-
interrupt
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Handles an interrupt (`SIGINT` or `SIGTERM`) properly as it
|
38
|
-
# deletes the pid file and calles the `stop` method.
|
39
|
-
def interrupt
|
40
|
-
File.delete(pid_file) if pid_file && File.exists?(pid_file)
|
9
|
+
@pid_file = options[:pid_file].nil? ? nil : File.absolute_path(options[:pid_file])
|
41
10
|
end
|
42
11
|
|
43
12
|
# Starts the daemon
|
@@ -51,12 +20,12 @@ class Insidious
|
|
51
20
|
fail InsidiousError.new("Process is already running with PID #{pid}")
|
52
21
|
exit 2
|
53
22
|
else
|
54
|
-
if pid_file.nil? &&
|
23
|
+
if @pid_file.nil? && daemon?
|
55
24
|
fail InsidiousError.new('No PID file is set but daemonize is set to true')
|
56
25
|
exit 1
|
57
26
|
end
|
58
27
|
|
59
|
-
|
28
|
+
run_daemon!(&block)
|
60
29
|
end
|
61
30
|
end
|
62
31
|
|
@@ -65,41 +34,35 @@ class Insidious
|
|
65
34
|
# This method only works when a PID file is given, otherwise it will
|
66
35
|
# exit with an error.
|
67
36
|
def stop!
|
68
|
-
if pid_file && File.exists?(pid_file)
|
37
|
+
if @pid_file && File.exists?(@pid_file)
|
69
38
|
begin
|
70
39
|
Process.kill(:INT, pid)
|
71
|
-
File.delete(pid_file)
|
40
|
+
File.delete(@pid_file)
|
72
41
|
rescue Errno::ESRCH
|
73
42
|
fail InsidiousError.new("No process is running with PID #{pid}")
|
74
43
|
exit 3
|
75
44
|
end
|
76
45
|
else
|
77
|
-
fail InsidiousError.new("Couldn't find the PID file: '#{pid_file}'")
|
46
|
+
fail InsidiousError.new("Couldn't find the PID file: '#{@pid_file}'")
|
78
47
|
exit 1
|
79
48
|
end
|
80
49
|
end
|
81
50
|
|
82
|
-
# Restarts the daemon
|
51
|
+
# Restarts the daemon, just a convenience method really
|
83
52
|
def restart!(&block)
|
84
|
-
if running?
|
85
|
-
stop!
|
86
|
-
end
|
87
|
-
|
53
|
+
stop! if running?
|
88
54
|
start!(&block)
|
89
55
|
end
|
90
56
|
|
91
|
-
#
|
92
|
-
def pid
|
93
|
-
File.read(@pid_file).strip.to_i
|
94
|
-
end
|
95
|
-
|
96
|
-
# Returns `true` if the daemon is running
|
57
|
+
# Returns true if the daemon is running
|
97
58
|
def running?
|
98
59
|
# First check if we have a pid file and if it exists
|
99
|
-
if pid_file.nil? || !File.exists?(pid_file)
|
100
|
-
|
101
|
-
|
60
|
+
return false if @pid_file.nil? || !File.exists?(@pid_file)
|
61
|
+
|
62
|
+
# Then make sure we have a pid
|
63
|
+
return false if pid.nil?
|
102
64
|
|
65
|
+
# If we can get the process id then we assume it is running
|
103
66
|
begin
|
104
67
|
Process.getpgid(pid)
|
105
68
|
true
|
@@ -113,54 +76,45 @@ class Insidious
|
|
113
76
|
@daemonize
|
114
77
|
end
|
115
78
|
|
116
|
-
#
|
117
|
-
#
|
118
|
-
|
119
|
-
|
120
|
-
#
|
121
|
-
# @param [String] path of the new workng directory
|
122
|
-
def chdir!(path)
|
123
|
-
Dir.chdir(File.absolute_path(path))
|
79
|
+
# Get the pid from the pid_file
|
80
|
+
# TODO: should this be 'cached'?
|
81
|
+
def pid
|
82
|
+
File.read(@pid_file).strip.to_i
|
124
83
|
end
|
125
84
|
|
126
|
-
#
|
127
|
-
def
|
128
|
-
@pid_file
|
85
|
+
# Save the PID to the PID file specified in @pid_file
|
86
|
+
def pid=(pid)
|
87
|
+
File.open(@pid_file, 'w') do |file|
|
88
|
+
file.write(pid)
|
89
|
+
end if @pid_file
|
129
90
|
end
|
130
91
|
|
131
|
-
|
132
|
-
#
|
133
|
-
# This path is relative to the working directory unless an absolute
|
134
|
-
# path is given.
|
135
|
-
def stdin=(path)
|
136
|
-
@stdin = File.absolute_path(path)
|
137
|
-
STDIN.reopen(@stdin)
|
138
|
-
end
|
92
|
+
private
|
139
93
|
|
140
|
-
#
|
94
|
+
# Runs the daemon
|
141
95
|
#
|
142
|
-
# This
|
143
|
-
#
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
96
|
+
# This will set up `INT` & `TERM` signal handlers to stop execution
|
97
|
+
# properly. When this signal handlers are called it will also call
|
98
|
+
# the #interrupt method and delete the pid file
|
99
|
+
def run_daemon!(&block)
|
100
|
+
begin
|
101
|
+
# Only start the process as a daemon if requested
|
102
|
+
Process.daemon(true) if daemon?
|
148
103
|
|
149
|
-
|
150
|
-
|
151
|
-
# This path is relative to the working directory unless an absolute
|
152
|
-
# path is given.
|
153
|
-
def stderr=(path)
|
154
|
-
@stderr = File.absolute_path(path)
|
155
|
-
STDERR.reopen(stderr, 'a')
|
156
|
-
end
|
104
|
+
# Set the process id, this will save it to @pid_file
|
105
|
+
self.pid = Process.pid
|
157
106
|
|
158
|
-
|
107
|
+
# Call the block of code passed to us
|
108
|
+
block.call
|
109
|
+
# Handle interruptipns such as someone ctrl-c or killing the process
|
110
|
+
rescue Interrupt, SignalException
|
111
|
+
interrupt!
|
112
|
+
end
|
113
|
+
end
|
159
114
|
|
160
|
-
#
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
end if pid_file
|
115
|
+
# Handle an interrupt (`SIGINT` or `SIGTERM`) by deleting the
|
116
|
+
# pid file if it exists
|
117
|
+
def interrupt!
|
118
|
+
File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
|
165
119
|
end
|
166
120
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: insidious
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James White
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -32,12 +32,12 @@ require_paths:
|
|
32
32
|
- lib
|
33
33
|
required_ruby_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - '>='
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: 1.9.3
|
38
38
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
|
-
- -
|
40
|
+
- - '>='
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
43
|
requirements: []
|