rekiq 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/test_app.rb +15 -6
- data/lib/rekiq/exceptions.rb +2 -1
- data/lib/rekiq/middleware/work_overseer.rb +15 -6
- data/lib/rekiq/scheduler.rb +3 -3
- data/lib/rekiq/version.rb +1 -1
- data/lib/rekiq/worker.rb +13 -13
- data/rekiq.gemspec +3 -4
- data/spec/rekiq/middleware/work_overseer_spec.rb +21 -5
- data/spec/rekiq/scheduler_spec.rb +2 -2
- data/spec/rekiq/worker_spec.rb +64 -17
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d548839b06922aae376dd135b36f42129b5e05e
|
4
|
+
data.tar.gz: 878fdd5c73c1943c47727071040194a310c82d83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae761311a5dc7ce345d52e4c357d8cd0585e555fbb07b927873e8552c1955791ff7df0ed529155024bb1b8f448728bb78b2971d11a745decb00191326c7abef0
|
7
|
+
data.tar.gz: c801e761d166e54589d8a5120850b1ba5ce3479e2e2cda5b75283e73469d27b1ee8c4b2201ae9bdd8b46d1c5410cdaf1c3ec7660f22fbbcc8c3eea668b73b0d7
|
data/example/test_app.rb
CHANGED
@@ -4,17 +4,24 @@
|
|
4
4
|
require 'ice_cube'
|
5
5
|
require 'sidekiq'
|
6
6
|
require 'rekiq'
|
7
|
+
require 'pry'
|
7
8
|
|
8
9
|
# define sidekiq worker as you normally would
|
9
10
|
class TestWorker1
|
10
11
|
include Sidekiq::Worker
|
11
12
|
|
12
|
-
sidekiq_options queue: "rekiq_test_worker",
|
13
|
+
sidekiq_options queue: "rekiq_test_worker",
|
14
|
+
retry: 3,
|
15
|
+
rekiq_cancel_method: :cancel
|
13
16
|
|
14
17
|
def perform(arg1, arg2)
|
15
|
-
puts "\
|
18
|
+
puts "\nhello from TestWorker1, arg1 is #{arg1}, arg2 is #{arg2} " \
|
16
19
|
"scheduled work time was #{scheduled_work_time}\n\n"
|
17
20
|
end
|
21
|
+
|
22
|
+
def cancel(arg1)
|
23
|
+
puts "\ncancel method invoked with arg #{arg1}\n\n"
|
24
|
+
end
|
18
25
|
end
|
19
26
|
|
20
27
|
# create ice cube schedule
|
@@ -24,7 +31,9 @@ schedule = IceCube::Schedule.new(Time.now) do |s|
|
|
24
31
|
|
25
32
|
# invoke method
|
26
33
|
TestWorker1.perform_recurringly(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
)
|
34
|
+
schedule,
|
35
|
+
['Rekiq', 'ola', '!!!'],
|
36
|
+
{ 'complex' => { 'hash' => 'woot!' } }
|
37
|
+
) do |config|
|
38
|
+
config.rekiq_cancel_args 1
|
39
|
+
end
|
data/lib/rekiq/exceptions.rb
CHANGED
@@ -2,5 +2,6 @@ module Rekiq
|
|
2
2
|
class StandardError < ::StandardError; end
|
3
3
|
class SidekiqNotLoaded < StandardError; end
|
4
4
|
class InvalidAttributeValue < StandardError; end
|
5
|
-
class
|
5
|
+
class CancelMethodMissing < StandardError; end
|
6
|
+
class CancelMethodInvocationError < StandardError; end
|
6
7
|
end
|
@@ -13,9 +13,8 @@ module Rekiq
|
|
13
13
|
|
14
14
|
setup_vars(worker, msg, queue)
|
15
15
|
|
16
|
-
if
|
17
|
-
|
18
|
-
return logger.info 'worker canceled by recurrence canceller'
|
16
|
+
if cancel_worker?
|
17
|
+
return logger.info 'worker canceled by rekiq cancel method'
|
19
18
|
end
|
20
19
|
|
21
20
|
return yield unless msg.key?('rq:schdlr')
|
@@ -33,8 +32,9 @@ module Rekiq
|
|
33
32
|
protected
|
34
33
|
|
35
34
|
def setup_vars(worker, msg, queue)
|
36
|
-
@
|
37
|
-
@
|
35
|
+
@cancel_method = worker.rekiq_cancel_method
|
36
|
+
@cancel_args = msg['rq:ca']
|
37
|
+
@worker = worker
|
38
38
|
@worker_name = worker.class.name
|
39
39
|
@queue = queue
|
40
40
|
@args = msg['args']
|
@@ -43,10 +43,19 @@ module Rekiq
|
|
43
43
|
@scheduled_work_time = Time.at(msg['rq:at'].to_f)
|
44
44
|
end
|
45
45
|
|
46
|
+
def cancel_worker?
|
47
|
+
!@cancel_method.nil? and @worker.send(@cancel_method, *@cancel_args)
|
48
|
+
rescue StandardError => s
|
49
|
+
raise CancelMethodInvocationError,
|
50
|
+
"error while invoking rekiq_cancel_method with message " \
|
51
|
+
"#{s.message}",
|
52
|
+
s.backtrace
|
53
|
+
end
|
54
|
+
|
46
55
|
def reschedule
|
47
56
|
jid, work_time =
|
48
57
|
Rekiq::Scheduler
|
49
|
-
.new(@worker_name, @queue, @args, @job, @addon, @
|
58
|
+
.new(@worker_name, @queue, @args, @job, @addon, @cancel_args)
|
50
59
|
.schedule_from_work_time(@scheduled_work_time)
|
51
60
|
|
52
61
|
unless jid.nil?
|
data/lib/rekiq/scheduler.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Rekiq
|
2
2
|
class Scheduler
|
3
|
-
def initialize(worker_name, queue, args, job, addon,
|
3
|
+
def initialize(worker_name, queue, args, job, addon, cancel_args)
|
4
4
|
@worker_name = worker_name
|
5
5
|
@queue = queue
|
6
6
|
@args = args
|
7
7
|
@job = job
|
8
8
|
@addon = addon
|
9
|
-
@
|
9
|
+
@cancel_args = cancel_args
|
10
10
|
end
|
11
11
|
|
12
12
|
def schedule(from = Time.now)
|
@@ -34,7 +34,7 @@ module Rekiq
|
|
34
34
|
'rq:schdlr' => nil
|
35
35
|
}.tap do |hash|
|
36
36
|
hash['rq:addon'] = @addon unless @addon.nil?
|
37
|
-
hash['rq:ca'] = @
|
37
|
+
hash['rq:ca'] = @cancel_args unless @cancel_args.nil?
|
38
38
|
end
|
39
39
|
|
40
40
|
Sidekiq::Client.push(client_args)
|
data/lib/rekiq/version.rb
CHANGED
data/lib/rekiq/worker.rb
CHANGED
@@ -6,10 +6,10 @@ module Rekiq
|
|
6
6
|
module Worker
|
7
7
|
class Configuration
|
8
8
|
attr_accessor :shift, :schedule_post_work, :schedule_expired,
|
9
|
-
:expiration_margin, :addon, :
|
9
|
+
:expiration_margin, :addon, :cancel_args
|
10
10
|
|
11
|
-
def
|
12
|
-
|
11
|
+
def rekiq_cancel_args(*args)
|
12
|
+
@cancel_args = args
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -34,7 +34,7 @@ module Rekiq
|
|
34
34
|
|
35
35
|
jid, work_time =
|
36
36
|
Rekiq::Scheduler
|
37
|
-
.new(name, queue, args, job, @config.addon, @config.
|
37
|
+
.new(name, queue, args, job, @config.addon, @config.cancel_args)
|
38
38
|
.schedule
|
39
39
|
|
40
40
|
if jid.nil?
|
@@ -46,18 +46,18 @@ module Rekiq
|
|
46
46
|
jid
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
50
|
-
get_sidekiq_options['
|
49
|
+
def rekiq_cancel_method
|
50
|
+
get_sidekiq_options['rekiq_cancel_method']
|
51
51
|
end
|
52
52
|
|
53
53
|
protected
|
54
54
|
|
55
55
|
def validate!
|
56
|
-
unless
|
57
|
-
self.method_defined?(
|
58
|
-
raise
|
59
|
-
'
|
60
|
-
"#{
|
56
|
+
unless rekiq_cancel_method.nil? or
|
57
|
+
self.method_defined?(rekiq_cancel_method)
|
58
|
+
raise CancelMethodMissing,
|
59
|
+
'rekiq cancel method name defined as ' \
|
60
|
+
"#{rekiq_cancel_method}, but worker does not have " \
|
61
61
|
'a method with that name, either remove definition or define ' \
|
62
62
|
'missing method'
|
63
63
|
end
|
@@ -77,8 +77,8 @@ module Sidekiq
|
|
77
77
|
base.extend(Rekiq::Worker::ClassMethods)
|
78
78
|
end
|
79
79
|
|
80
|
-
def
|
81
|
-
self.class.
|
80
|
+
def rekiq_cancel_method
|
81
|
+
self.class.rekiq_cancel_method
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
data/rekiq.gemspec
CHANGED
@@ -9,9 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["junhanamaki"]
|
10
10
|
spec.email = ["jun.hanamaki@gmail.com"]
|
11
11
|
spec.summary = %q{Recurring worker extension for Sidekiq}
|
12
|
-
spec.description = %q{Rekiq extends Sidekiq
|
13
|
-
recurring workers
|
14
|
-
more details.}
|
12
|
+
spec.description = %q{Rekiq extends Sidekiq to allow the scheduling of
|
13
|
+
recurring workers}
|
15
14
|
spec.homepage = "https://github.com/junhanamaki/rekiq"
|
16
15
|
spec.license = "MIT"
|
17
16
|
|
@@ -25,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
25
24
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
25
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
26
|
spec.add_development_dependency 'simplecov', '~> 0.9'
|
28
|
-
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.
|
27
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
|
29
28
|
spec.add_development_dependency 'factory_girl', '~> 4.4'
|
30
29
|
spec.add_development_dependency 'jazz_hands', '~> 0.5'
|
31
30
|
spec.add_development_dependency 'ice_cube', '~> 0.12'
|
@@ -11,7 +11,7 @@ describe Rekiq::Middleware::WorkOverseer do
|
|
11
11
|
include Sidekiq::Worker
|
12
12
|
|
13
13
|
sidekiq_options queue: 'work_overseer_test_worker',
|
14
|
-
|
14
|
+
rekiq_cancel_method: :cancel
|
15
15
|
|
16
16
|
def cancel(bool)
|
17
17
|
bool
|
@@ -24,7 +24,7 @@ describe Rekiq::Middleware::WorkOverseer do
|
|
24
24
|
let(:job) { build(:job, schedule: schedule) }
|
25
25
|
let(:overseer) { Rekiq::Middleware::WorkOverseer.new }
|
26
26
|
|
27
|
-
context 'worker does not have
|
27
|
+
context 'worker does not have rekiq_cancel_method set' do
|
28
28
|
let(:worker) { WorkOverseerTestWorker.new }
|
29
29
|
let(:queue) { WorkOverseerTestWorker.get_sidekiq_options['queue'] }
|
30
30
|
|
@@ -80,7 +80,7 @@ describe Rekiq::Middleware::WorkOverseer do
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
context 'worker has
|
83
|
+
context 'worker has rekiq_cancel_method method set' do
|
84
84
|
let(:worker) { WorkOverseerCancelTestWorker.new }
|
85
85
|
let(:queue) { WorkOverseerCancelTestWorker.get_sidekiq_options['queue'] }
|
86
86
|
|
@@ -95,7 +95,7 @@ describe Rekiq::Middleware::WorkOverseer do
|
|
95
95
|
end.not_to yield_control
|
96
96
|
end
|
97
97
|
|
98
|
-
it 'does not
|
98
|
+
it 'does not schedule worker' do
|
99
99
|
expect(WorkOverseerCancelTestWorker.jobs.count).to eq(0)
|
100
100
|
end
|
101
101
|
end
|
@@ -105,11 +105,27 @@ describe Rekiq::Middleware::WorkOverseer do
|
|
105
105
|
{ 'rq:job' => job.to_array, 'args' => args, 'rq:ca' => false }
|
106
106
|
end
|
107
107
|
|
108
|
-
it '
|
108
|
+
it 'yields given block' do
|
109
109
|
expect do |b|
|
110
110
|
overseer.call(worker, msg, queue, &b)
|
111
111
|
end.to yield_control.once
|
112
112
|
end
|
113
|
+
|
114
|
+
it 'does not schedule worker' do
|
115
|
+
expect(WorkOverseerCancelTestWorker.jobs.count).to eq(0)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'msg with rq:ca key with different arity from cancel method' do
|
120
|
+
let(:msg) do
|
121
|
+
{ 'rq:job' => job.to_array, 'args' => args, 'rq:ca' => [true, true] }
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'raises error' do
|
125
|
+
expect do |b|
|
126
|
+
overseer.call(worker, msg, queue, &b)
|
127
|
+
end.to raise_error
|
128
|
+
end
|
113
129
|
end
|
114
130
|
end
|
115
131
|
end
|
@@ -46,7 +46,7 @@ describe Rekiq::Scheduler do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
context 'given nil as
|
49
|
+
context 'given nil as rekiq_cancel_args' do
|
50
50
|
it 'creates sidekiq job' do
|
51
51
|
expect(SchedulerTestWorker.jobs.count).to eq(1)
|
52
52
|
end
|
@@ -56,7 +56,7 @@ describe Rekiq::Scheduler do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
context 'given non empty array as
|
59
|
+
context 'given non empty array as rekiq_cancel_args' do
|
60
60
|
let(:c_args) { [1, 2, 3] }
|
61
61
|
|
62
62
|
it 'creates sidekiq job' do
|
data/spec/rekiq/worker_spec.rb
CHANGED
@@ -7,20 +7,20 @@ describe Rekiq::Worker do
|
|
7
7
|
include Sidekiq::Worker
|
8
8
|
end
|
9
9
|
|
10
|
-
class
|
10
|
+
class RekiqCancelMethodUndefinedExampleWorker
|
11
11
|
include Sidekiq::Worker
|
12
|
-
sidekiq_options
|
12
|
+
sidekiq_options rekiq_cancel_method: :cancel
|
13
13
|
end
|
14
14
|
|
15
|
-
class
|
15
|
+
class RekiqCancelMethodDefinedExampleWorker
|
16
16
|
include Sidekiq::Worker
|
17
|
-
sidekiq_options
|
17
|
+
sidekiq_options rekiq_cancel_method: :cancel
|
18
18
|
|
19
19
|
def cancel
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
context '
|
23
|
+
context 'class includes Sidekiq::Worker module' do
|
24
24
|
it 'responds to perform_recurringly' do
|
25
25
|
ExampleWorker.respond_to? :perform_recurringly
|
26
26
|
end
|
@@ -45,36 +45,36 @@ describe Rekiq::Worker do
|
|
45
45
|
let(:time) { Time.now + 3600 }
|
46
46
|
let(:schedule) { IceCube::Schedule.new(time) }
|
47
47
|
|
48
|
-
context 'for worker with
|
48
|
+
context 'for worker with rekiq_cancel_method set with ' \
|
49
49
|
'non defined method' do
|
50
50
|
before do
|
51
51
|
begin
|
52
52
|
@jid =
|
53
|
-
|
53
|
+
RekiqCancelMethodUndefinedExampleWorker.perform_recurringly(schedule)
|
54
54
|
rescue
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'raises error' do
|
59
59
|
expect do
|
60
|
-
|
60
|
+
RekiqCancelMethodUndefinedExampleWorker.perform_recurringly(schedule)
|
61
61
|
end.to raise_error
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'does not schedule worker' do
|
65
|
-
expect(
|
65
|
+
expect(RekiqCancelMethodUndefinedExampleWorker.jobs.count).to eq(0)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
context 'for worker with
|
69
|
+
context 'for worker with rekiq_cancel_method set with ' \
|
70
70
|
'defined method' do
|
71
71
|
before do
|
72
|
-
@jid =
|
72
|
+
@jid = RekiqCancelMethodDefinedExampleWorker.perform_recurringly(schedule)
|
73
73
|
end
|
74
74
|
|
75
75
|
it 'does not raise error' do
|
76
76
|
expect do
|
77
|
-
|
77
|
+
RekiqCancelMethodDefinedExampleWorker.perform_recurringly(schedule)
|
78
78
|
end.not_to raise_error
|
79
79
|
end
|
80
80
|
|
@@ -83,11 +83,11 @@ describe Rekiq::Worker do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'schedules worker' do
|
86
|
-
expect(
|
86
|
+
expect(RekiqCancelMethodDefinedExampleWorker.jobs.count).to eq(1)
|
87
87
|
end
|
88
88
|
|
89
89
|
it 'schedules worker for one hour from now' do
|
90
|
-
expect(
|
90
|
+
expect(RekiqCancelMethodDefinedExampleWorker.jobs[0]['at']).to eq(time.to_f)
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -109,8 +109,55 @@ describe Rekiq::Worker do
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
+
context 'rekiq_cancel_args with 2 arguments' do
|
113
|
+
let(:rekiq_cancel_args) { ['args1', 'args2'] }
|
114
|
+
before do
|
115
|
+
@jid = ExampleWorker.perform_recurringly(schedule) do |config|
|
116
|
+
config.rekiq_cancel_args *rekiq_cancel_args
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'returns created job id' do
|
121
|
+
expect(@jid).not_to be_nil
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'schedules worker' do
|
125
|
+
expect(ExampleWorker.jobs.count).to eq(1)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'yields once if block given' do
|
129
|
+
expect do |b|
|
130
|
+
ExampleWorker.perform_recurringly(schedule, &b)
|
131
|
+
end.to yield_control.once
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'creates key rq:ca in job hash' do
|
135
|
+
expect(ExampleWorker.jobs[0].key?('rq:ca')).to eq(true)
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'value under rq:ca key in job hash' do
|
139
|
+
let(:value) { ExampleWorker.jobs[0]['rq:ca'] }
|
140
|
+
|
141
|
+
it 'is an array' do
|
142
|
+
expect(value.class).to eq(Array)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'has count equal to number of rekiq_cancel_args' do
|
146
|
+
expect(value.count).to eq(rekiq_cancel_args.count)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'has first element equal to first arg in rekiq_cancel_args' do
|
150
|
+
expect(value[0]).to eq(rekiq_cancel_args[0])
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'has second element equal to second arg in rekiq_cancel_args' do
|
154
|
+
expect(value[1]).to eq(rekiq_cancel_args[1])
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
112
159
|
context 'shift set to minus 5 minutes' do
|
113
|
-
let(:shift)
|
160
|
+
let(:shift) { -5 * 60 }
|
114
161
|
before do
|
115
162
|
@jid = ExampleWorker.perform_recurringly(schedule) do |config|
|
116
163
|
config.shift = shift
|
@@ -125,13 +172,13 @@ describe Rekiq::Worker do
|
|
125
172
|
expect(ExampleWorker.jobs.count).to eq(1)
|
126
173
|
end
|
127
174
|
|
128
|
-
it 'yields once' do
|
175
|
+
it 'yields once if block given' do
|
129
176
|
expect do |b|
|
130
177
|
ExampleWorker.perform_recurringly(schedule, &b)
|
131
178
|
end.to yield_control.once
|
132
179
|
end
|
133
180
|
|
134
|
-
it 'sets shift in rq:job' do
|
181
|
+
it 'sets shift in position 1 of array under key rq:job' do
|
135
182
|
array = ExampleWorker.jobs[0]['rq:job']
|
136
183
|
|
137
184
|
expect(array[1]).to eq(shift)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rekiq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- junhanamaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.4'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
68
|
+
version: '0.4'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: factory_girl
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,9 +123,8 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '3.2'
|
125
125
|
description: |-
|
126
|
-
Rekiq extends Sidekiq
|
127
|
-
recurring workers
|
128
|
-
more details.
|
126
|
+
Rekiq extends Sidekiq to allow the scheduling of
|
127
|
+
recurring workers
|
129
128
|
email:
|
130
129
|
- jun.hanamaki@gmail.com
|
131
130
|
executables: []
|