resque-workers-lock 1.5 → 1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -24
- data/test/lock_test.rb +71 -60
- metadata +7 -21
- data/test/lock_worker.rb +0 -22
data/Rakefile
CHANGED
@@ -1,30 +1,11 @@
|
|
1
|
-
require 'resque/tasks'
|
2
|
-
|
3
|
-
task "resque:setup" do
|
4
|
-
require_relative 'test/lock_worker'
|
5
|
-
end
|
6
|
-
|
7
1
|
require 'rake/testtask'
|
8
2
|
require 'rdoc/task'
|
9
|
-
|
10
|
-
def command?(command)
|
11
|
-
system("type #{command} > /dev/null")
|
12
|
-
end
|
13
|
-
|
14
|
-
# Tests
|
3
|
+
require 'resque/tasks'
|
15
4
|
|
16
5
|
task :default => :test
|
17
6
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
sh "turn test/*.rb #{suffix}"
|
23
|
-
end
|
24
|
-
else
|
25
|
-
Rake::TestTask.new do |t|
|
26
|
-
t.libs << 'lib'
|
27
|
-
t.pattern = 'test/**/*_test.rb'
|
28
|
-
t.verbose = false
|
29
|
-
end
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.pattern = 'test/**/*_test.rb'
|
10
|
+
t.verbose = false
|
30
11
|
end
|
data/test/lock_test.rb
CHANGED
@@ -1,38 +1,24 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'resque/plugins/workers/lock'
|
3
|
-
|
3
|
+
require 'tempfile'
|
4
4
|
|
5
5
|
class LockTest < Test::Unit::TestCase
|
6
|
-
class SimilarJob
|
7
|
-
extend Resque::Plugins::Workers::Lock
|
8
|
-
@queue = :lock_test
|
9
|
-
|
10
|
-
def self.perform
|
11
|
-
raise "Woah woah! How did this happen?"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
class UniqueJob
|
16
7
|
extend Resque::Plugins::Workers::Lock
|
17
8
|
@queue = :lock_test
|
18
9
|
|
19
|
-
def self.
|
20
|
-
|
10
|
+
def self.lock_workers(*)
|
11
|
+
self.name
|
21
12
|
end
|
22
13
|
|
23
|
-
def self.
|
24
|
-
|
14
|
+
def self.perform params
|
15
|
+
File.open(params['output_file'], 'a') do |output_file|
|
16
|
+
output_file.puts params['job']
|
17
|
+
output_file.flush
|
18
|
+
sleep 1
|
19
|
+
output_file.puts params['job']
|
20
|
+
end
|
25
21
|
end
|
26
|
-
|
27
|
-
def self.perform(id)
|
28
|
-
raise "Woah woah! How did this happen?"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def setup
|
33
|
-
Resque.redis.del('queue:lock_test')
|
34
|
-
Resque.redis.del(SimilarJob.lock_workers)
|
35
|
-
Resque.redis.del(SimilarJob.lock_enqueue)
|
36
22
|
end
|
37
23
|
|
38
24
|
def test_lint
|
@@ -41,53 +27,78 @@ class LockTest < Test::Unit::TestCase
|
|
41
27
|
end
|
42
28
|
end
|
43
29
|
|
44
|
-
def
|
45
|
-
|
30
|
+
def test_workers_dont_work_simultaneously
|
31
|
+
assert_locking_works_with jobs: 2, workers: 2
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
46
35
|
|
47
|
-
|
48
|
-
|
49
|
-
|
36
|
+
def assert_locking_works_with options
|
37
|
+
jobs = (1..options[:jobs]).map{|job| "Job #{job}" }
|
38
|
+
output_file = Tempfile.new 'output_file'
|
50
39
|
|
51
|
-
|
52
|
-
Resque.enqueue
|
53
|
-
assert_equal i.to_s+"e", UniqueJob.lock_enqueue(i)
|
54
|
-
assert_equal i.to_s+"w", UniqueJob.lock_workers(i)
|
40
|
+
jobs.each do |job|
|
41
|
+
Resque.enqueue UniqueJob, job: job, output_file: output_file.path
|
55
42
|
end
|
56
43
|
|
57
|
-
|
44
|
+
process_jobs workers: options[:workers], timeout: 10
|
58
45
|
|
59
|
-
|
60
|
-
|
46
|
+
lines = File.readlines(output_file).map(&:chomp)
|
47
|
+
lines.each_slice(2) do |a,b|
|
48
|
+
assert_equal a,b, "#{a} was interrupted by #{b}"
|
49
|
+
end
|
50
|
+
ensure
|
51
|
+
output_file.close
|
52
|
+
end
|
61
53
|
|
62
|
-
|
63
|
-
|
54
|
+
def process_jobs options
|
55
|
+
with_workers options[:workers] do
|
56
|
+
wait_until(options[:timeout]) do
|
57
|
+
no_busy_workers && no_queued_jobs
|
58
|
+
end
|
59
|
+
end
|
64
60
|
end
|
65
61
|
|
66
|
-
def
|
67
|
-
|
62
|
+
def with_workers n
|
63
|
+
pids = []
|
64
|
+
n.times do
|
65
|
+
if pid = fork
|
66
|
+
pids << pid
|
67
|
+
else
|
68
|
+
pids = [] # Don't kill from child's ensure
|
69
|
+
worker = Resque::Worker.new('*')
|
70
|
+
worker.reconnect
|
71
|
+
worker.work(0.5)
|
72
|
+
exit!
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
yield
|
77
|
+
|
78
|
+
ensure
|
79
|
+
pids.each do |pid|
|
80
|
+
Process.kill("QUIT", pid)
|
81
|
+
end
|
82
|
+
|
83
|
+
pids.each do |pid|
|
84
|
+
Process.waitpid(pid)
|
85
|
+
end
|
86
|
+
end
|
68
87
|
|
69
|
-
|
70
|
-
Resque.
|
88
|
+
def no_busy_workers
|
89
|
+
Resque::Worker.working.size == 0
|
90
|
+
end
|
71
91
|
|
72
|
-
|
92
|
+
def no_queued_jobs
|
93
|
+
Resque.redis.llen("queue:lock_test") == 0
|
73
94
|
end
|
74
95
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
contents = file.read
|
83
|
-
file.close
|
84
|
-
assert_equal '1', contents
|
85
|
-
|
86
|
-
# After 12 seconds the 2 jobs should have been processed (not at the same time because of the lock)
|
87
|
-
sleep(12)
|
88
|
-
file = File.open('test/test.txt', 'rb')
|
89
|
-
contents = file.read
|
90
|
-
file.close
|
91
|
-
assert_equal '11', contents
|
96
|
+
def wait_until(timeout)
|
97
|
+
timeout.times do
|
98
|
+
return if yield
|
99
|
+
sleep 1
|
100
|
+
end
|
101
|
+
|
102
|
+
raise "Timout occured"
|
92
103
|
end
|
93
104
|
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.
|
4
|
+
version: '1.6'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: turn
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: rake
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,9 +43,12 @@ dependencies:
|
|
59
43
|
- - ! '>='
|
60
44
|
- !ruby/object:Gem::Version
|
61
45
|
version: '0'
|
62
|
-
description: !
|
63
|
-
simultaneously by multiple workers.
|
64
|
-
|
46
|
+
description: ! 'A Resque plugin. Two or more jobs with the same lock cannot be processed
|
47
|
+
simultaneously by multiple workers.
|
48
|
+
|
49
|
+
When this situation occurs the second job gets pushed back to the queue.
|
50
|
+
|
51
|
+
'
|
65
52
|
email: bartolsthoorn@gmail.com
|
66
53
|
executables: []
|
67
54
|
extensions: []
|
@@ -72,7 +59,6 @@ files:
|
|
72
59
|
- LICENSE
|
73
60
|
- lib/resque/plugins/workers/lock.rb
|
74
61
|
- test/lock_test.rb
|
75
|
-
- test/lock_worker.rb
|
76
62
|
homepage: http://github.com/bartolsthoorn/resque-workers-lock
|
77
63
|
licenses: []
|
78
64
|
post_install_message:
|
data/test/lock_worker.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'resque'
|
2
|
-
require 'resque/plugins/workers/lock'
|
3
|
-
|
4
|
-
# Sleep job, for testing that two workers are not processing two jobs with
|
5
|
-
# the same lock.
|
6
|
-
class SimilarSleepJob
|
7
|
-
extend Resque::Plugins::Workers::Lock
|
8
|
-
@queue = :lock_test_workers
|
9
|
-
|
10
|
-
def self.lock_enqueue(id)
|
11
|
-
false
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.lock_workers(id)
|
15
|
-
return id.to_s
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.perform(id)
|
19
|
-
File.open('test/test.txt', 'a') {|f| f.write('1') }
|
20
|
-
sleep(5)
|
21
|
-
end
|
22
|
-
end
|