resque-batched-job 1.1.0 → 1.2.0
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/Rakefile +14 -1
- data/lib/resque/plugins/batched_job/version.rb +1 -1
- data/lib/resque/plugins/batched_job.rb +24 -3
- data/test/test_batched_job.rb +28 -0
- metadata +5 -4
data/Rakefile
CHANGED
|
@@ -11,6 +11,14 @@ Rake::TestTask.new do |task|
|
|
|
11
11
|
task.verbose = true
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
+
begin
|
|
15
|
+
require 'yard'
|
|
16
|
+
YARD::Rake::YardocTask.new do |t|
|
|
17
|
+
t.files = ['lib/**/*.rb', '-', 'README.md']
|
|
18
|
+
end
|
|
19
|
+
rescue LoadError
|
|
20
|
+
end
|
|
21
|
+
|
|
14
22
|
desc "Publish RubyGem and source."
|
|
15
23
|
task :publish => [:build, :tag] do
|
|
16
24
|
sh "git push origin v#{Resque::Plugins::BatchedJob::VERSION}"
|
|
@@ -23,11 +31,16 @@ task :tag do
|
|
|
23
31
|
sh "git tag v#{Resque::Plugins::BatchedJob::VERSION}"
|
|
24
32
|
end
|
|
25
33
|
|
|
26
|
-
desc "Build RubyGem."
|
|
34
|
+
desc "Build resque-batched-job RubyGem."
|
|
27
35
|
task :build do
|
|
28
36
|
sh "gem build resque-batched-job.gemspec"
|
|
29
37
|
end
|
|
30
38
|
|
|
39
|
+
desc "Install current resque-batched-job RubyGem."
|
|
40
|
+
task :install => :build do
|
|
41
|
+
sh "gem install --local resque-batched-job-#{Resque::Plugins::BatchedJob::VERSION}.gem"
|
|
42
|
+
end
|
|
43
|
+
|
|
31
44
|
desc "View changelog"
|
|
32
45
|
task :changelog do
|
|
33
46
|
tags = `git tag`.split("\n").reverse
|
|
@@ -16,11 +16,16 @@ module Resque
|
|
|
16
16
|
include Resque::Helpers
|
|
17
17
|
|
|
18
18
|
# Helper method used to generate the batch key.
|
|
19
|
+
#
|
|
20
|
+
# @param [Object, #to_s] id Batch identifier. Any Object that responds to #to_s
|
|
21
|
+
# @return [String] Used to identify batch Redis List key
|
|
19
22
|
def batch(id)
|
|
20
23
|
"batch:#{id}"
|
|
21
24
|
end
|
|
22
25
|
|
|
23
|
-
#
|
|
26
|
+
# Resque hook that handles batching the job. (closes #2)
|
|
27
|
+
#
|
|
28
|
+
# @param [Object, #to_s] id Batch identifier. Any Object that responds to #to_s
|
|
24
29
|
def after_enqueue_batch(id, *args)
|
|
25
30
|
mutex(id) do |bid|
|
|
26
31
|
redis.rpush(bid, encode(:class => self.name, :args => args))
|
|
@@ -30,6 +35,8 @@ module Resque
|
|
|
30
35
|
# After the job is performed, remove it from the batched job list. If the
|
|
31
36
|
# current job is the last in the batch to be performed, invoke the after_batch
|
|
32
37
|
# hooks.
|
|
38
|
+
#
|
|
39
|
+
# @param id (see Resque::Plugins::BatchedJob#after_enqueue_batch)
|
|
33
40
|
def after_perform_batch(id, *args)
|
|
34
41
|
remove_batched_job(id, *args)
|
|
35
42
|
|
|
@@ -43,31 +50,45 @@ module Resque
|
|
|
43
50
|
|
|
44
51
|
# Checks the size of the batched job list and returns true if the list is
|
|
45
52
|
# empty or if the key does not exist.
|
|
53
|
+
#
|
|
54
|
+
# @param id (see Resque::Plugins::BatchedJob#batch)
|
|
46
55
|
def batch_complete?(id)
|
|
47
56
|
mutex(id) do |bid|
|
|
48
57
|
redis.llen(bid) == 0
|
|
49
58
|
end
|
|
50
59
|
end
|
|
51
60
|
|
|
61
|
+
# Check to see if the Redis key exists.
|
|
62
|
+
#
|
|
63
|
+
# @param id (see Resque::Plugins::BatchedJob#batch)
|
|
52
64
|
def batch_exist?(id)
|
|
53
65
|
mutex(id) do |bid|
|
|
54
66
|
redis.exists(bid)
|
|
55
67
|
end
|
|
56
68
|
end
|
|
57
69
|
|
|
58
|
-
# Remove a job from the batch list.
|
|
70
|
+
# Remove a job from the batch list. (closes #6)
|
|
71
|
+
#
|
|
72
|
+
# @param id (see Resque::Plugins::BatchedJob#after_enqueue_batch)
|
|
59
73
|
def remove_batched_job(id, *args)
|
|
60
74
|
mutex(id) do |bid|
|
|
61
75
|
redis.lrem(bid, 1, encode(:class => self.name, :args => args))
|
|
62
76
|
end
|
|
63
77
|
end
|
|
64
78
|
|
|
79
|
+
# Remove a job from the batch list and run after hooks if necessary.
|
|
80
|
+
#
|
|
81
|
+
# @param id (see Resque::Plugins::BatchedJob#remove_batched_job)
|
|
82
|
+
def remove_batched_job!(id, *args)
|
|
83
|
+
after_perform_batch(id, *args)
|
|
84
|
+
end
|
|
85
|
+
|
|
65
86
|
private
|
|
66
87
|
|
|
67
88
|
# Lock a batch key before executing Redis commands. This will ensure
|
|
68
89
|
# no race conditions occur when modifying batch information. Here is
|
|
69
90
|
# an example of how this works. See http://redis.io/commands/setnx for
|
|
70
|
-
# more information.
|
|
91
|
+
# more information. (fixes #4) (closes #5)
|
|
71
92
|
#
|
|
72
93
|
# * Job2 sends SETNX batch:123:lock in order to aquire a lock.
|
|
73
94
|
# * Job1 still has the key locked, so Job2 continues into the loop.
|
data/test/test_batched_job.rb
CHANGED
|
@@ -20,6 +20,14 @@ class BatchedJobTest < Test::Unit::TestCase
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
def test_encoding
|
|
24
|
+
Resque.enqueue(Job, @batch_id, 123)
|
|
25
|
+
Resque.enqueue(JobWithoutArgs, @batch_id)
|
|
26
|
+
|
|
27
|
+
assert_equal("{\"class\":\"Job\",\"args\":[123]}", redis.lindex(@batch, 0))
|
|
28
|
+
assert_equal("{\"class\":\"JobWithoutArgs\",\"args\":[]}", redis.lindex(@batch, 1))
|
|
29
|
+
end
|
|
30
|
+
|
|
23
31
|
def test_batch_key
|
|
24
32
|
assert_nothing_raised do
|
|
25
33
|
Resque.enqueue(Job, @batch_id, 'foobar')
|
|
@@ -119,6 +127,26 @@ class BatchedJobTest < Test::Unit::TestCase
|
|
|
119
127
|
|
|
120
128
|
assert_equal(x * y, Integer(redis.get(@batch)))
|
|
121
129
|
end
|
|
130
|
+
|
|
131
|
+
def test_remove_batched_job
|
|
132
|
+
Resque.enqueue(JobWithoutArgs, @batch_id)
|
|
133
|
+
|
|
134
|
+
assert_nothing_raised do
|
|
135
|
+
JobWithoutArgs.remove_batched_job(@batch_id)
|
|
136
|
+
end
|
|
137
|
+
assert(Job.batch_complete?(@batch_id))
|
|
138
|
+
assert_equal(false, Job.batch_exist?(@batch_id))
|
|
139
|
+
assert_equal(false, $batch_complete)
|
|
140
|
+
|
|
141
|
+
Resque.enqueue(JobWithoutArgs, @batch_id)
|
|
142
|
+
|
|
143
|
+
assert_nothing_raised do
|
|
144
|
+
JobWithoutArgs.remove_batched_job!(@batch_id)
|
|
145
|
+
end
|
|
146
|
+
assert($batch_complete)
|
|
147
|
+
assert(Job.batch_complete?(@batch_id))
|
|
148
|
+
assert_equal(false, Job.batch_exist?(@batch_id))
|
|
149
|
+
end
|
|
122
150
|
|
|
123
151
|
private
|
|
124
152
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: resque-batched-job
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
12
|
+
date: 2012-01-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: resque
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &70323806805200 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,7 +21,7 @@ dependencies:
|
|
|
21
21
|
version: 1.10.0
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *70323806805200
|
|
25
25
|
description: ! " Resque plugin for batching jobs. When a batch/group of jobs are
|
|
26
26
|
complete, \nadditional work can be performed usings batch hooks.\n"
|
|
27
27
|
email: dan@dj-agiledev.com
|
|
@@ -65,3 +65,4 @@ summary: Resque plugin
|
|
|
65
65
|
test_files:
|
|
66
66
|
- test/test_batched_job.rb
|
|
67
67
|
- test/test_helper.rb
|
|
68
|
+
has_rdoc:
|