stalking 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -177,6 +177,11 @@ Stalking::Consumer.new :logger => ...
177
177
  You can hand out any kind of logging object, which responds to an `error`
178
178
  method and takes a string error message.
179
179
 
180
+ # Graceful termination
181
+
182
+ To stop your workers gracefully, send a USR1 signal to them. The worker will
183
+ finish its current job and terminate afterwards.
184
+
180
185
  ## Contributing
181
186
 
182
187
  1. Fork it
@@ -30,28 +30,30 @@ module Stalking
30
30
  private
31
31
 
32
32
  def work
33
- loop do
33
+ looping do
34
34
  job = connection.reserve
35
35
 
36
- name, args = JSON.parse(job.body)
36
+ lock do
37
+ name, args = JSON.parse(job.body)
37
38
 
38
- if handler = @handlers.job_handlers[name]
39
- begin
40
- Timeout::timeout(job.ttr - 1) do
41
- handle @handlers.before_handlers, name, args
39
+ if handler = @handlers.job_handlers[name]
40
+ begin
41
+ Timeout::timeout(job.ttr - 1) do
42
+ handle @handlers.before_handlers, name, args
42
43
 
43
- handler.call args
44
+ handler.call args
44
45
 
45
- handle @handlers.after_handlers, name, args
46
+ handle @handlers.after_handlers, name, args
47
+ end
48
+ rescue Beanstalk::NotConnected => e
49
+ raise e # Re-raise
50
+ rescue Timeout::Error, StandardError => e
51
+ handle_error e, name, args
46
52
  end
47
- rescue Beanstalk::NotConnected => e
48
- raise e # Re-raise
49
- rescue Timeout::Error, StandardError => e
50
- handle_error e, name, args
51
53
  end
52
- end
53
54
 
54
- job.delete
55
+ job.delete
56
+ end
55
57
  end
56
58
  rescue Beanstalk::NotConnected => e
57
59
  log_error e
@@ -63,6 +65,28 @@ module Stalking
63
65
  retry
64
66
  end
65
67
 
68
+ def looping
69
+ continue = true
70
+
71
+ trap "USR1" do
72
+ exit unless @locked
73
+
74
+ continue = false
75
+ end
76
+
77
+ while continue
78
+ yield
79
+ end
80
+ end
81
+
82
+ def lock
83
+ @locked = true
84
+
85
+ yield
86
+ ensure
87
+ @locked = false
88
+ end
89
+
66
90
  def connect
67
91
  @connection ||= Beanstalk::Pool.new(@servers)
68
92
 
@@ -1,3 +1,3 @@
1
1
  module Stalking
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -3,6 +3,7 @@ $:.unshift File.expand_path("../../lib", __FILE__)
3
3
 
4
4
  require "stalking"
5
5
  require "test/unit"
6
+ require "timeout"
6
7
 
7
8
  require File.expand_path("../beanstalk", __FILE__)
8
9
  require File.expand_path("../test_logger", __FILE__)
@@ -179,7 +180,7 @@ class Stalking::ConsumerTest < Test::Unit::TestCase
179
180
  res = []
180
181
 
181
182
  exception = assert_raises(Exception) do
182
- Stalking::Consumer.new(:logger => logger) do
183
+ Stalking::Consumer.new :logger => logger do
183
184
  job "test" do |args|
184
185
  if res.empty?
185
186
  res.push ["down"]
@@ -204,5 +205,19 @@ class Stalking::ConsumerTest < Test::Unit::TestCase
204
205
  def test_logger
205
206
  # Already tested
206
207
  end
208
+
209
+ def test_termination
210
+ assert_nothing_raised do
211
+ Timeout::timeout 3 do
212
+ consumer = Stalking::Consumer.new
213
+
214
+ consumer.send :looping do
215
+ consumer.send :lock do # Don't invoke exit
216
+ Process.kill "USR1", Process.pid
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
207
222
  end
208
223
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stalking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-28 00:00:00.000000000 Z
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake