stalker 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/stalker.rb +12 -1
  3. data/test/stalker_test.rb +9 -0
  4. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.5.0
data/lib/stalker.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'beanstalk-client'
2
2
  require 'json'
3
3
  require 'uri'
4
+ require 'timeout'
4
5
 
5
6
  module Stalker
6
7
  extend self
@@ -53,13 +54,23 @@ module Stalker
53
54
  loop { work_one_job }
54
55
  end
55
56
 
57
+ class JobTimeout < RuntimeError; end
58
+
56
59
  def work_one_job
57
60
  job = beanstalk.reserve
58
61
  name, args = JSON.parse job.body
59
62
  log_job_begin(name, args)
60
63
  handler = @@handlers[name]
61
64
  raise(NoSuchJob, name) unless handler
62
- handler.call(args)
65
+
66
+ begin
67
+ Timeout::timeout(job.ttr - 1) do
68
+ handler.call(args)
69
+ end
70
+ rescue Timeout::Error
71
+ raise JobTimeout, "#{name} hit #{job.ttr-1}s timeout"
72
+ end
73
+
63
74
  job.delete
64
75
  log_job_end(name)
65
76
  rescue Beanstalk::NotConnected => e
data/test/stalker_test.rb CHANGED
@@ -39,4 +39,13 @@ class StalkerTest < Test::Unit::TestCase
39
39
  Stalker.work_one_job
40
40
  assert_equal false, $handled
41
41
  end
42
+
43
+ test "exception raised one second before beanstalk ttr reached" do
44
+ Stalker.error { |e| $handled = e.class }
45
+ Stalker.job('my.job') { sleep(3); $handled = "didn't time out" }
46
+ Stalker.enqueue('my.job', {}, :ttr => 2)
47
+ Stalker.prep
48
+ Stalker.work_one_job
49
+ assert_equal Stalker::JobTimeout, $handled
50
+ end
42
51
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 2
10
- version: 0.4.2
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Wiggins
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-08 00:00:00 -07:00
18
+ date: 2010-08-29 00:00:00 -07:00
19
19
  default_executable: stalk
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency