ngauthier-active-listener 0.3.2 → 0.3.3
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.
- data/VERSION.yml +2 -2
- data/lib/active-listener.rb +34 -2
- data/test/active_listener-events.yml +1 -0
- data/test/active_listener_test.rb +14 -22
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/active-listener.rb
CHANGED
@@ -39,6 +39,11 @@ class ActiveListener
|
|
39
39
|
"#{rake_root}",
|
40
40
|
].join(" ")
|
41
41
|
`#{command}`
|
42
|
+
10.times do
|
43
|
+
return true if self.running(opts)
|
44
|
+
sleep(0.1)
|
45
|
+
end
|
46
|
+
return self.running(opts)
|
42
47
|
end
|
43
48
|
|
44
49
|
# Stop an #ActiveListener by specifying a pid file
|
@@ -46,7 +51,32 @@ class ActiveListener
|
|
46
51
|
# ActiveListener.stop(:pid_file => File.join(RAILS_ROOT, 'log', 'active-listener.pid'))
|
47
52
|
def self.stop(opts = {})
|
48
53
|
pid_file = opts[:pid_file]
|
49
|
-
|
54
|
+
10.times do
|
55
|
+
`start-stop-daemon --stop --oknodo --pidfile #{File.expand_path(pid_file)}`
|
56
|
+
break if !self.running(opts)
|
57
|
+
sleep(0.2)
|
58
|
+
end
|
59
|
+
return !self.running(opts)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Check if an #ActiveListener is running
|
63
|
+
#
|
64
|
+
# ActiveListener.running(:pid => 12345)
|
65
|
+
# or
|
66
|
+
# ActiveListener.running(:pid_file => 'active-listener.pid')
|
67
|
+
def self.running(opts = {})
|
68
|
+
unless opts[:pid] or opts[:pid_file]
|
69
|
+
raise "Must pass in pid or pid_file"
|
70
|
+
end
|
71
|
+
if opts[:pid_file]
|
72
|
+
raise "File does not exist" unless File.exists?(opts[:pid_file])
|
73
|
+
f = File.new(opts[:pid_file], 'r')
|
74
|
+
pid = f.read.to_i
|
75
|
+
f.close
|
76
|
+
else
|
77
|
+
pid = opts[:pid].to_i
|
78
|
+
end
|
79
|
+
return `ps -p #{pid.to_s} -o pid=`.size > 0
|
50
80
|
end
|
51
81
|
|
52
82
|
# Create an #ActiveListener in the foreground. This is useful for a non-rails project.
|
@@ -192,6 +222,8 @@ class ActiveListener
|
|
192
222
|
if port
|
193
223
|
self.port = port
|
194
224
|
spawn_listener_thread
|
225
|
+
else
|
226
|
+
# puts "no port in [#{config_file}]:\n#{yml.inspect}"
|
195
227
|
end
|
196
228
|
end
|
197
229
|
|
@@ -220,7 +252,7 @@ class ActiveListener
|
|
220
252
|
|
221
253
|
# Listen on a port for triggers (not implemented)
|
222
254
|
def spawn_listener_thread
|
223
|
-
|
255
|
+
puts "Hey a port! #{port}"
|
224
256
|
end
|
225
257
|
|
226
258
|
end
|
@@ -91,20 +91,20 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
91
91
|
context "An autostarted listener" do
|
92
92
|
|
93
93
|
setup do
|
94
|
+
@sample_file = File.join(File.dirname(__FILE__),'sample.txt')
|
95
|
+
FileUtils.rm_f @sample_file
|
94
96
|
assert ActiveListener.autostart(
|
95
97
|
:config => File.join(File.dirname(__FILE__), 'active_listener.yml'),
|
96
98
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid'),
|
97
99
|
:log_file => File.join(File.dirname(__FILE__), 'active_listener.log'),
|
98
100
|
:rake_root => File.join(File.dirname(__FILE__), '..')
|
99
101
|
)
|
100
|
-
@sample_file = File.join(File.dirname(__FILE__),'sample.txt')
|
101
102
|
end
|
102
103
|
|
103
104
|
teardown do
|
104
|
-
ActiveListener.stop(
|
105
|
+
assert ActiveListener.stop(
|
105
106
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid')
|
106
107
|
)
|
107
|
-
sleep(1)
|
108
108
|
FileUtils.rm_f(File.join(File.dirname(__FILE__),'sample.txt'))
|
109
109
|
end
|
110
110
|
|
@@ -113,7 +113,7 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
113
113
|
assert File.exists?(@sample_file)
|
114
114
|
FileUtils.rm_f(@sample_file)
|
115
115
|
assert !File.exists?(@sample_file)
|
116
|
-
sleep(
|
116
|
+
sleep(2)
|
117
117
|
assert File.exists?(@sample_file)
|
118
118
|
end
|
119
119
|
|
@@ -121,7 +121,7 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
121
121
|
pf = File.new(File.join(File.dirname(__FILE__), 'active_listener.pid'))
|
122
122
|
pid = pf.read
|
123
123
|
pf.close
|
124
|
-
assert(
|
124
|
+
assert(ActiveListener.running(:pid => pid))
|
125
125
|
ActiveListener.autostart(
|
126
126
|
:config => File.join(File.dirname(__FILE__), 'active_listener.yml'),
|
127
127
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid'),
|
@@ -132,19 +132,19 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
132
132
|
new_pid = pf.read
|
133
133
|
pf.close
|
134
134
|
assert_equal pid, new_pid
|
135
|
-
assert(
|
135
|
+
assert(ActiveListener.running(:pid => pid))
|
136
136
|
end
|
137
137
|
|
138
138
|
should "be able to be stopped and a new one can be run" do
|
139
139
|
pf = File.new(File.join(File.dirname(__FILE__), 'active_listener.pid'))
|
140
140
|
pid = pf.read
|
141
141
|
pf.close
|
142
|
-
assert(
|
143
|
-
ActiveListener.stop(
|
142
|
+
assert ActiveListener.running(:pid => pid)
|
143
|
+
assert ActiveListener.stop(
|
144
144
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid')
|
145
145
|
)
|
146
|
-
sleep(
|
147
|
-
assert(!
|
146
|
+
sleep(0.2)
|
147
|
+
assert(!ActiveListener.running(:pid => pid))
|
148
148
|
ActiveListener.autostart(
|
149
149
|
:config => File.join(File.dirname(__FILE__), 'active_listener.yml'),
|
150
150
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid'),
|
@@ -155,8 +155,8 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
155
155
|
new_pid = pf.read
|
156
156
|
pf.close
|
157
157
|
assert_not_equal pid, new_pid
|
158
|
-
assert(
|
159
|
-
assert
|
158
|
+
assert ActiveListener.running(:pid => new_pid)
|
159
|
+
assert !ActiveListener.running(:pid => pid)
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
@@ -186,10 +186,9 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
186
186
|
|
187
187
|
context "An auto-started event based listener" do
|
188
188
|
setup do
|
189
|
-
ActiveListener.stop(
|
189
|
+
assert ActiveListener.stop(
|
190
190
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid')
|
191
191
|
)
|
192
|
-
sleep(0.5)
|
193
192
|
ActiveListener.autostart(
|
194
193
|
:config => File.join(File.dirname(__FILE__), 'active_listener-events.yml'),
|
195
194
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid'),
|
@@ -200,10 +199,9 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
200
199
|
end
|
201
200
|
|
202
201
|
teardown do
|
203
|
-
ActiveListener.stop(
|
202
|
+
assert ActiveListener.stop(
|
204
203
|
:pid_file => File.join(File.dirname(__FILE__), 'active_listener.pid')
|
205
204
|
)
|
206
|
-
sleep(1)
|
207
205
|
FileUtils.rm_f(File.join(File.dirname(__FILE__),'sample.txt'))
|
208
206
|
end
|
209
207
|
|
@@ -213,10 +211,4 @@ class ActiveListenerTest < Test::Unit::TestCase
|
|
213
211
|
#assert File.exists?(@sample_file)
|
214
212
|
end
|
215
213
|
end
|
216
|
-
|
217
|
-
private
|
218
|
-
|
219
|
-
def pid_running(pid)
|
220
|
-
`ps -p #{pid.to_i.to_s} -o pid=`.size > 0
|
221
|
-
end
|
222
214
|
end
|