resque-workers-lock 1.3 → 1.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ require 'resque/tasks'
2
+
1
3
  require 'rake/testtask'
2
4
  require 'rdoc/task'
3
5
 
@@ -1,42 +1,44 @@
1
+ require 'resque'
2
+
1
3
  module Resque
2
4
  alias_method :orig_remove_queue, :remove_queue
3
-
5
+
4
6
  def remove_queue(queue)
5
7
  Resque.redis.keys('enqueuelock:*').collect { |x| Resque.redis.del(x) }.count
6
8
  Resque.redis.keys('workerslock:*').collect { |x| Resque.redis.del(x) }.count
7
-
9
+
8
10
  orig_remove_queue(queue)
9
11
  end
10
-
12
+
11
13
  module Plugins
12
14
  module Workers
13
15
  module Lock
14
-
16
+
15
17
  # Override in your job to control the queue lock key
16
18
  def lock_enqueue(*args)
17
19
  "#{name}-#{args.to_s}"
18
20
  end
19
-
21
+
20
22
  def get_lock_enqueue(*args)
21
23
  "enqueuelock:"+lock_enqueue(*args).to_s
22
24
  end
23
-
25
+
24
26
  # Override in your job to control the workers lock key.
25
27
  def lock_workers(*args)
26
28
  "#{name}-#{args.to_s}"
27
29
  end
28
-
30
+
29
31
  def get_lock_workers(*args)
30
32
  "workerslock:"+lock_workers(*args).to_s
31
33
  end
32
-
34
+
33
35
  # Override in your job to change the perform requeue delay
34
36
  def requeue_perform_delay
35
37
  1.0
36
38
  end
37
-
38
-
39
- # Called with the job args before a job is placed on the queue.
39
+
40
+
41
+ # Called with the job args before a job is placed on the queue.
40
42
  # If the hook returns false, the job will not be placed on the queue.
41
43
  def before_enqueue_lock(*args)
42
44
  if lock_enqueue(*args) == false
@@ -45,8 +47,8 @@ module Resque
45
47
  return Resque.redis.setnx(get_lock_enqueue(*args), true)
46
48
  end
47
49
  end
48
-
49
- # Called with the job args before perform.
50
+
51
+ # Called with the job args before perform.
50
52
  # If it raises Resque::Job::DontPerform, the job is aborted.
51
53
  def before_perform_lock(*args)
52
54
  if lock_workers(*args)
@@ -59,12 +61,12 @@ module Resque
59
61
  end
60
62
  end
61
63
  end
62
-
64
+
63
65
  def after_dequeue_lock(*args)
64
66
  # Clear the lock when dequeueed
65
67
  Resque.redis.del(get_lock_enqueue(*args))
66
68
  end
67
-
69
+
68
70
  def around_perform_lock(*args)
69
71
  begin
70
72
  yield
@@ -74,14 +76,14 @@ module Resque
74
76
  Resque.redis.del(get_lock_enqueue(*args))
75
77
  end
76
78
  end
77
-
79
+
78
80
  def on_failure_lock(exception, *args)
79
81
  # Clear the lock on DirtyExit
80
82
  Resque.redis.del(get_lock_workers(*args))
81
83
  Resque.redis.del(get_lock_enqueue(*args))
82
84
  end
83
-
85
+
84
86
  end
85
87
  end
86
88
  end
87
- end
89
+ end
data/test/lock_test.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  require 'test/unit'
2
- require 'resque'
3
2
  require 'resque/plugins/workers/lock'
4
3
 
5
- $counter = 0
6
-
7
4
  class LockTest < Test::Unit::TestCase
8
5
  class SimilarJob
9
6
  extend Resque::Plugins::Workers::Lock
@@ -13,19 +10,19 @@ class LockTest < Test::Unit::TestCase
13
10
  raise "Woah woah! How did this happen?"
14
11
  end
15
12
  end
16
-
13
+
17
14
  class UniqueJob
18
15
  extend Resque::Plugins::Workers::Lock
19
16
  @queue = :lock_test
20
-
17
+
21
18
  def self.lock_enqueue(id)
22
19
  return id.to_s+"e"
23
20
  end
24
-
21
+
25
22
  def self.lock_workers(id)
26
23
  return id.to_s+"w"
27
24
  end
28
-
25
+
29
26
  def self.perform(id)
30
27
  raise "Woah woah! How did this happen?"
31
28
  end
@@ -43,46 +40,74 @@ class LockTest < Test::Unit::TestCase
43
40
  end
44
41
  end
45
42
 
46
- def test_version
47
- major, minor, patch = Resque::Version.split('.')
48
- assert_equal 1, major.to_i
49
- assert minor.to_i >= 17
50
- assert Resque::Plugin.respond_to?(:before_enqueue_hooks)
51
- end
52
-
53
43
  def test_enqueue
54
44
  3.times { Resque.enqueue(SimilarJob) }
55
45
 
56
46
  assert_equal "LockTest::SimilarJob-[]", SimilarJob.lock_workers
57
47
  assert_equal "LockTest::SimilarJob-[]", SimilarJob.lock_enqueue
58
48
  assert_equal 1, Resque.redis.llen('queue:lock_test')
59
-
49
+
60
50
  3.times do |i|
61
51
  Resque.enqueue(UniqueJob, i+100)
62
52
  assert_equal i.to_s+"e", UniqueJob.lock_enqueue(i)
63
53
  assert_equal i.to_s+"w", UniqueJob.lock_workers(i)
64
54
  end
65
-
55
+
66
56
  assert_equal 4, Resque.redis.llen('queue:lock_test')
67
-
57
+
68
58
  # Test for complete queue wipe
69
59
  Resque.remove_queue(:lock_test)
70
-
60
+
71
61
  Resque.enqueue(SimilarJob)
72
62
  assert_equal 1, Resque.redis.llen('queue:lock_test')
73
63
  end
74
-
64
+
75
65
  def test_zcleanup
76
66
  Resque.remove_queue(:lock_test)
77
-
78
- Resque.redis.keys('enqueuelock:*').collect { |x| Resque.redis.del(x) }.count
79
- Resque.redis.keys('workerslock:*').collect { |x| Resque.redis.del(x) }.count
80
-
67
+
68
+ Resque.redis.keys('enqueuelock:*').collect { |x| Resque.redis.del(x) }
69
+ Resque.redis.keys('workerslock:*').collect { |x| Resque.redis.del(x) }
70
+
81
71
  assert_equal 0, Resque.redis.llen('queue:lock_test')
82
72
  end
83
-
73
+
74
+ # Sleep job, for testing that two workers are not processing two jobs with
75
+ # the same lock.
76
+ class SimilarSleepJob
77
+ extend Resque::Plugins::Workers::Lock
78
+ @queue = :lock_test_workers
79
+
80
+ def self.lock_enqueue(id)
81
+ false
82
+ end
83
+
84
+ def self.lock_workers(id)
85
+ return id.to_s
86
+ end
87
+
88
+ def self.perform(id)
89
+ File.open('test/test.txt', 'a') {|f| f.write('1') }
90
+ sleep(5)
91
+ end
92
+ end
93
+
94
+ # To test this, make sure to run `TERM_CHILD=1 COUNT=2 VVERBOSE=1 QUEUES=* rake resque:work`
84
95
  def test_lock
85
- # TODO: test that two workers are not processing two jobs with same locks
86
- # This is pretty hard to do, contributors are welcome!
96
+ 2.times { Resque.enqueue(SimilarSleepJob, 'writing_and_sleeping') }
97
+ SimilarSleepJob.perform('abc')
98
+
99
+ # After 3 seconds only 1 job had the change of running
100
+ sleep(3)
101
+ file = File.open('test/test.txt', 'rb')
102
+ contents = file.read
103
+ file.close
104
+ assert_equal '1', contents
105
+
106
+ # After 12 seconds the 2 jobs should have been processed (not at the same time because of the lock)
107
+ sleep(12)
108
+ file = File.open('test/test.txt', 'rb')
109
+ contents = file.read
110
+ file.close
111
+ assert_equal '11', contents
87
112
  end
88
- end
113
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-workers-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.3'
4
+ version: '1.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: 2012-08-21 00:00:00.000000000 Z
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: resque