resque-uniq 0.0.5 → 0.0.6

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/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source :rubygems
2
2
 
3
+ gemspec
4
+
3
5
  gem "redis"
4
6
  gem "resque"
5
7
  gem 'yajl-ruby', :require => 'yajl/json_gem'
data/README.md CHANGED
@@ -54,6 +54,38 @@ You can tell _resque-uniq_ to auto-expire job locks by setting `@unique_lock_aut
54
54
 
55
55
  Lock autoexpiration was necessary to protect against stale locks. With the new run lock trick you probably don't need it anymore.
56
56
 
57
+ If you want to define a default `unique_lock_autoexpire` in the base class and let other jobs to extend that base class
58
+ you cannot use `@unique_lock_autoexpire` since it's not inherited by subclasses. Define a class method with the same
59
+ name instead
60
+
61
+ class BaseJob
62
+ extend Resque::Plugins::UniqueJob
63
+
64
+ def self.queue
65
+ :default_queue
66
+ end
67
+
68
+ def self.unique_lock_autoexpire
69
+ 600 # TTL = 10 minutes
70
+ end
71
+ end
72
+
73
+ class SmallJob < BaseJob
74
+ def self.perform
75
+ ...
76
+ end
77
+ end
78
+
79
+ class BiglJob < BaseJob
80
+ def self.unique_lock_autoexpire
81
+ 3 * 3600 # TTL = 3 hours
82
+ end
83
+
84
+ def self.perform
85
+ ...
86
+ end
87
+ end
88
+
57
89
  ## Credits
58
90
 
59
91
  There are several similar Resque plugins. We tried them all but for one reason or another they didn't work reliably
@@ -46,8 +46,9 @@ module Resque
46
46
  Resque.redis.del "#{RUN_LOCK_NAME_PREFIX}#{lock_name}"
47
47
  end
48
48
  nx = Resque.redis.setnx(lock_name, Time.now.to_i)
49
- if defined?(@unique_lock_autoexpire) && @unique_lock_autoexpire > 0
50
- Resque.redis.expire(lock_name, @unique_lock_autoexpire)
49
+ ttl = instance_variable_get(:@unique_lock_autoexpire) || respond_to?(:unique_lock_autoexpire) && unique_lock_autoexpire
50
+ if ttl && ttl > 0
51
+ Resque.redis.expire(lock_name, ttl)
51
52
  end
52
53
  nx
53
54
  end
@@ -64,6 +65,10 @@ module Resque
64
65
  end
65
66
  end
66
67
 
68
+ def after_dequeue_lock(*args)
69
+ Resque.redis.del(run_lock(*args))
70
+ Resque.redis.del(lock(*args))
71
+ end
67
72
 
68
73
  private
69
74
 
@@ -1,3 +1,3 @@
1
1
  module ResqueUniq
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -16,6 +16,15 @@ class UniqueJobTest < Test::Unit::TestCase
16
16
  def self.perform(param) ; end
17
17
  end
18
18
 
19
+ class AutoexpireLockJobBase
20
+ extend Resque::Plugins::UniqueJob
21
+ def self.queue ; :job_test ; end
22
+ def self.unique_lock_autoexpire ; 1 ; end
23
+ def self.perform(param) ; end
24
+ end
25
+
26
+ class ExtendedAutoExpireLockJob < AutoexpireLockJobBase ; end
27
+
19
28
  def setup
20
29
  Resque.remove_queue(Resque.queue_from_class(Job))
21
30
  Resque.redis.keys('*:UniqueJobTest::*').each {|k| Resque.redis.del(k) }
@@ -45,6 +54,30 @@ class UniqueJobTest < Test::Unit::TestCase
45
54
  assert_equal 1, Resque.size(queue)
46
55
  end
47
56
 
57
+ def test_lock_is_removed_after_dequeue
58
+ queue = Resque.queue_from_class(Job)
59
+ Resque.enqueue(Job, "hello")
60
+ assert_equal 1, Resque.size(queue)
61
+
62
+ Resque.dequeue(Job, "hello")
63
+ assert_equal 0, Resque.size(queue)
64
+ assert_equal nil, Resque.redis.get(Job.lock("hello"))
65
+ assert_equal nil, Resque.redis.get(Job.run_lock("hello"))
66
+
67
+ Resque.enqueue(Job, "hello")
68
+ assert_equal 1, Resque.size(queue)
69
+ end
70
+
71
+ # XXX Resque doesn't call any job hooks in Resque#remove_queue. We don't get a chance to clean up the locks
72
+ # def test_lock_is_removed_after_remove_queue
73
+ # queue = Resque.queue_from_class(Job)
74
+ # Resque.enqueue(Job, "hello")
75
+ # assert_equal 1, Resque.size(queue)
76
+
77
+ # Resque.remove_queue(queue)
78
+ # assert_equal nil, Resque.redis.get(Job.lock("hello"))
79
+ # assert_equal nil, Resque.redis.get(Job.run_lock("hello"))
80
+ # end
48
81
 
49
82
  def test_autoexpire_lock
50
83
  Resque.enqueue(AutoexpireLockJob, 123)
@@ -53,4 +86,11 @@ class UniqueJobTest < Test::Unit::TestCase
53
86
  assert_equal 2, Resque.size(Resque.queue_from_class(AutoexpireLockJob))
54
87
  end
55
88
 
89
+ def test_extended_autoexpire_lock
90
+ Resque.enqueue(ExtendedAutoExpireLockJob, 123)
91
+ sleep 2
92
+ Resque.enqueue(ExtendedAutoExpireLockJob, 123)
93
+ assert_equal 2, Resque.size(Resque.queue_from_class(ExtendedAutoExpireLockJob))
94
+ end
95
+
56
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-uniq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -43,13 +43,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
43
  version: '0'
44
44
  segments:
45
45
  - 0
46
- hash: 563217245005310137
46
+ hash: -1891980449315157063
47
47
  required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  none: false
49
49
  requirements:
50
50
  - - ! '>='
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
+ segments:
54
+ - 0
55
+ hash: -1891980449315157063
53
56
  requirements: []
54
57
  rubyforge_project:
55
58
  rubygems_version: 1.8.24