rekiq 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 512f24c5f3a752e5df2fec6362c7f4b527303eee
4
- data.tar.gz: 8cd79188800b644ecfa72b9d9a1a7e37b4e39f57
3
+ metadata.gz: 8c02fc37de00489fe1195f4cb22ed4bf1700ab13
4
+ data.tar.gz: 073c844c1ee5f5c671ce71b15d6ef9fe8fd5aa3c
5
5
  SHA512:
6
- metadata.gz: a864ed822e261ac6cbcdfdc32bcfd8a187d9845d922911e882e57e876249120c403a383c880b464355b4f04624042a8ffbf8cb31f53ecd2bb6f0b10803ed21ef
7
- data.tar.gz: 22d22ab155bd60846184e9b60d9dba60f483ffd3529e391cb609a5e83724b5a75c5b607079b35eeee215713788a95538ba4511732fc54a3767d016453676dd9d
6
+ metadata.gz: 4e7aa8af8e713b349c90d0f1519c8532e5fd22cb7d6ec6eb16ec844e912af72d698a735ae0e07739191de5864486f0aaa313e392e51b0444286fbdcda2331ed3
7
+ data.tar.gz: 19fb11a43f48c45e730b361c439d0a5df6f0bf281c61906051c72f01168110886740f99bfdf17f894e5dc80a5f1664efdebdb0dfa26332369655723f0e2b5d6c
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Rekiq
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/rekiq.svg)](http://badge.fury.io/rb/rekiq)
3
4
  [![Build Status](https://travis-ci.org/junhanamaki/rekiq.svg?branch=master)](https://travis-ci.org/junhanamaki/rekiq)
4
5
  [![Code Climate](https://codeclimate.com/github/junhanamaki/rekiq.png)](https://codeclimate.com/github/junhanamaki/rekiq)
5
6
  [![Test Coverage](https://codeclimate.com/github/junhanamaki/rekiq/coverage.png)](https://codeclimate.com/github/junhanamaki/rekiq)
@@ -10,9 +11,9 @@
10
11
 
11
12
  Rekiq extends Sidekiq and adds functionality to schedule recurring workers.
12
13
 
13
- Sidekiq is an amazing gem that allows us delegate time consuming work to a
14
- worker, or even to schedule a time for the worker to start. Now wouldn't it be
15
- nice if it also allowed us to schedule a worker to do work recurringly? That's
14
+ Sidekiq is an amazing gem that allows us to execute work asynchronous, or
15
+ even schedule that work to be run at a given time. Now, wouldn't it be
16
+ nice if it also allowed us to schedule a worker to do recurring work? That's
16
17
  what rekiq purposes to do.
17
18
 
18
19
  For example, rekiq allows you to schedule a worker to repeat the same
@@ -23,6 +24,7 @@ work every friday at 23:00.
23
24
  Tested with:
24
25
 
25
26
  * ruby 2.1.1, 2.0.0 and 1.9.3
27
+
26
28
  * sidekiq 3.2.1
27
29
 
28
30
  ## Installation
@@ -69,8 +71,10 @@ For our example we'll be using the gem [ice_cube](https://github.com/seejohnrun/
69
71
  # now just start your worker
70
72
  ExampleWorker.perform_recurringly(schedule, 'argument_1', 'argument_2')
71
73
 
72
- You can use your own schedule object, configure worker to reschedule before or
73
- after work is done, set margin, and much more! So please check
74
+ And that's it! Now the worker will run every friday as 2am.
75
+
76
+ You can use your own schedule object, configure worker to schedule before or
77
+ after work is done, set a margin, and much more! So please feel free to check
74
78
  [wiki](https://github.com/junhanamaki/rekiq/wiki) for more details.
75
79
 
76
80
  ## Contributing
@@ -6,14 +6,14 @@ unless defined?(Sidekiq)
6
6
  'sidekiq must be required before requiring rekiq'
7
7
  end
8
8
 
9
- require 'rekiq/middleware/work_overseer'
10
- require 'rekiq/middleware/utils'
11
- require 'rekiq/worker'
12
-
13
9
  module Rekiq
14
10
 
15
11
  end
16
12
 
13
+ require 'rekiq/worker'
14
+ require 'rekiq/middleware/work_overseer'
15
+ require 'rekiq/middleware/utils'
16
+
17
17
  Sidekiq.configure_server do |config|
18
18
  config.server_middleware do |chain|
19
19
  chain.add Rekiq::Middleware::Utils
@@ -2,4 +2,5 @@ module Rekiq
2
2
  class StandardError < ::StandardError; end
3
3
  class SidekiqNotLoaded < StandardError; end
4
4
  class InvalidAttributeValue < StandardError; end
5
+ class CancellerMethodMissing < StandardError; end
5
6
  end
@@ -1,4 +1,6 @@
1
1
  require 'yaml'
2
+ require 'rekiq/validator'
3
+ require 'rekiq/configuration'
2
4
 
3
5
  module Rekiq
4
6
  class Job
@@ -1,6 +1,5 @@
1
1
  require 'sidekiq'
2
2
  require 'sidekiq/util'
3
- require 'rekiq/configuration'
4
3
  require 'rekiq/job'
5
4
  require 'rekiq/scheduler'
6
5
 
@@ -9,35 +8,45 @@ module Rekiq
9
8
  class WorkOverseer
10
9
  include ::Sidekiq::Util
11
10
 
12
- attr_accessor :worker_name, :queue, :args, :job, :addon,
13
- :scheduled_work_time
14
-
15
11
  def call(worker, msg, queue)
16
12
  return yield unless msg['rq:job'] and msg['retry_count'].nil?
17
13
 
18
- self.worker_name = worker.class.name
19
- self.queue = queue
20
- self.args = msg['args']
21
- self.job = Job.from_array(msg['rq:job'])
22
- self.addon = msg['rq:addon']
23
- self.scheduled_work_time = Time.at(msg['rq:at'].to_f)
14
+ setup_vars(worker, msg, queue)
15
+
16
+ if !@canceller_name.nil? and
17
+ worker.send(@canceller_name, *@canceller_args)
18
+ return logger.info 'worker canceled by rekiq_canceller'
19
+ end
24
20
 
25
21
  begin
26
- reschedule unless job.schedule_post_work?
22
+ reschedule unless @job.schedule_post_work?
27
23
  yield
28
24
  ensure
29
- reschedule if job.schedule_post_work?
25
+ reschedule if @job.schedule_post_work?
30
26
  end
31
27
  end
32
28
 
29
+ protected
30
+
31
+ def setup_vars(worker, msg, queue)
32
+ @canceller_name = worker.rekiq_canceller_name
33
+ @canceller_args = msg['rq:ca']
34
+ @worker_name = worker.class.name
35
+ @queue = queue
36
+ @args = msg['args']
37
+ @job = Job.from_array(msg['rq:job'])
38
+ @addon = msg['rq:addon']
39
+ @scheduled_work_time = Time.at(msg['rq:at'].to_f)
40
+ end
41
+
33
42
  def reschedule
34
43
  jid, work_time =
35
44
  Rekiq::Scheduler
36
- .new(worker_name, queue, args, job, addon)
37
- .schedule_from_work_time(scheduled_work_time)
45
+ .new(@worker_name, @queue, @args, @job, @addon, @canceller_args)
46
+ .schedule_from_work_time(@scheduled_work_time)
38
47
 
39
48
  unless jid.nil?
40
- logger.info "recurring work for #{worker_name} scheduled for " \
49
+ logger.info "recurring work for #{@worker_name} scheduled for " \
41
50
  "#{work_time} with jid #{jid}"
42
51
  else
43
52
  logger.info 'recurrence terminated, job terminated'
@@ -1,39 +1,39 @@
1
1
  module Rekiq
2
2
  class Scheduler
3
- attr_accessor :worker_name, :queue, :args, :job, :addon, :work_time
4
-
5
- def initialize(worker_name, queue, args, job, addon)
6
- self.worker_name = worker_name
7
- self.queue = queue
8
- self.args = args
9
- self.job = job
10
- self.addon = addon
3
+ def initialize(worker_name, queue, args, job, addon, canceller_args)
4
+ @worker_name = worker_name
5
+ @queue = queue
6
+ @args = args
7
+ @job = job
8
+ @addon = addon
9
+ @canceller_args = canceller_args
11
10
  end
12
11
 
13
12
  def schedule(from = Time.now)
14
- self.work_time = job.next_work_time(from)
13
+ @work_time = @job.next_work_time(from)
15
14
 
16
- work_time.nil? ? nil : [schedule_work, work_time]
15
+ @work_time.nil? ? nil : [schedule_work, @work_time]
17
16
  end
18
17
 
19
18
  def schedule_from_work_time(from)
20
- self.work_time = job.next_work_time_from_work_time(from)
19
+ @work_time = @job.next_work_time_from_work_time(from)
21
20
 
22
- work_time.nil? ? nil : [schedule_work, work_time]
21
+ @work_time.nil? ? nil : [schedule_work, @work_time]
23
22
  end
24
23
 
25
24
  private
26
25
 
27
26
  def schedule_work
28
27
  client_args = {
29
- 'at' => work_time.to_f,
30
- 'queue' => queue,
31
- 'class' => worker_name,
32
- 'args' => args,
33
- 'rq:job' => job.to_array,
34
- 'rq:at' => work_time.to_f
28
+ 'at' => @work_time.to_f,
29
+ 'queue' => @queue,
30
+ 'class' => @worker_name,
31
+ 'args' => @args,
32
+ 'rq:job' => @job.to_array,
33
+ 'rq:at' => @work_time.to_f,
35
34
  }.tap do |hash|
36
- hash['rq:addon'] = addon unless addon.nil?
35
+ hash['rq:addon'] = @addon unless @addon.nil?
36
+ hash['rq:ca'] = @canceller_args unless @canceller_args.nil?
37
37
  end
38
38
 
39
39
  Sidekiq::Client.push(client_args)
@@ -1,3 +1,5 @@
1
+ require 'rekiq/exceptions'
2
+
1
3
  module Rekiq
2
4
  module Validator
3
5
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module Rekiq
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -6,21 +6,33 @@ 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, :canceller_args
10
+
11
+ def rekiq_canceller_args(*args)
12
+ self.canceller_args = args
13
+ end
10
14
  end
11
15
 
12
16
  module ClassMethods
17
+ attr_reader :canceller_name
18
+
19
+ def rekiq_canceller(method_name)
20
+ @canceller_name = method_name
21
+ end
22
+
13
23
  def perform_recurringly(schedule, *args)
14
- config = Configuration.new
15
- yield config if block_given?
24
+ @config = Configuration.new
25
+ yield @config if block_given?
26
+
27
+ validate!
16
28
 
17
29
  job =
18
30
  Rekiq::Job
19
31
  .new 'schedule' => schedule,
20
- 'shift' => config.shift,
21
- 'schedule_post_work' => config.schedule_post_work,
22
- 'schedule_expired' => config.schedule_expired,
23
- 'expiration_margin' => config.expiration_margin
32
+ 'shift' => @config.shift,
33
+ 'schedule_post_work' => @config.schedule_post_work,
34
+ 'schedule_expired' => @config.schedule_expired,
35
+ 'expiration_margin' => @config.expiration_margin
24
36
 
25
37
  job.validate!
26
38
 
@@ -28,16 +40,29 @@ module Rekiq
28
40
 
29
41
  jid, work_time =
30
42
  Rekiq::Scheduler
31
- .new(name, queue, args, job, config.addon)
43
+ .new(name, queue, args, job, @config.addon, @config.canceller_args)
32
44
  .schedule
33
45
 
34
- return if jid.nil?
35
-
36
- ::Sidekiq.logger.info "recurring work for #{name} scheduled for " \
37
- "#{work_time} with jid #{jid}"
46
+ if jid.nil?
47
+ return ::Sidekiq.logger.info \
48
+ "recurring work for #{name} scheduled for " \
49
+ "#{work_time} with jid #{jid}"
50
+ end
38
51
 
39
52
  jid
40
53
  end
54
+
55
+ protected
56
+
57
+ def validate!
58
+ unless canceller_name.nil? or
59
+ self.method_defined?(canceller_name)
60
+ raise CancellerMethodMissing,
61
+ "rekiq_canceller method defined as #{canceller_name} " \
62
+ 'but worker does not have a method with that name, either ' \
63
+ 'remove rekiq_canceller or define missing method'
64
+ end
65
+ end
41
66
  end
42
67
  end
43
68
  end
@@ -52,5 +77,9 @@ module Sidekiq
52
77
  original_included_method.call(base)
53
78
  base.extend(Rekiq::Worker::ClassMethods)
54
79
  end
80
+
81
+ def rekiq_canceller_name
82
+ self.class.canceller_name
83
+ end
55
84
  end
56
85
  end
@@ -7,59 +7,101 @@ describe Rekiq::Middleware::WorkOverseer do
7
7
  sidekiq_options queue: 'work_overseer_test_worker'
8
8
  end
9
9
 
10
+ class WorkOverseerCancelTestWorker
11
+ include Sidekiq::Worker
12
+
13
+ sidekiq_options queue: 'work_overseer_test_worker'
14
+ rekiq_canceller :cancel
15
+
16
+ def cancel(bool)
17
+ bool
18
+ end
19
+ end
20
+
10
21
  describe '#call' do
11
- let(:worker) { WorkOverseerTestWorker.new }
12
- let(:queue) { WorkOverseerTestWorker.get_sidekiq_options['queue'] }
13
22
  let(:args) { [] }
14
23
  let(:schedule) { IceCube::Schedule.new(Time.new + 3600) }
24
+ let(:job) { build(:job, schedule: schedule) }
25
+ let(:overseer) { Rekiq::Middleware::WorkOverseer.new }
26
+
27
+ context 'worker does not have rekiq_canceller set' do
28
+ let(:worker) { WorkOverseerTestWorker.new }
29
+ let(:queue) { WorkOverseerTestWorker.get_sidekiq_options['queue'] }
15
30
 
16
- context 'msg with rq:job key (existing job)' do
17
- let(:job) { build(:job, schedule: schedule) }
18
- let(:msg) { { 'rq:job' => job.to_array, 'args' => args } }
31
+ context 'msg with rq:job key (existing job)' do
32
+ let(:msg) { { 'rq:job' => job.to_array, 'args' => args } }
19
33
 
20
- it 'yields once' do
21
- expect do |b|
22
- Rekiq::Middleware::WorkOverseer.new
23
- .call(worker, msg, queue, &b)
24
- end.to yield_control.once
34
+ it 'yields once' do
35
+ expect do |b|
36
+ overseer.call(worker, msg, queue, &b)
37
+ end.to yield_control.once
38
+ end
39
+
40
+ it 'schedules job' do
41
+ overseer.call(worker, msg, queue) {}
42
+
43
+ expect(WorkOverseerTestWorker.jobs.count).to eq(1)
44
+ end
25
45
  end
26
46
 
27
- it 'schedules job' do
28
- Rekiq::Middleware::WorkOverseer.new
29
- .call(worker, msg, queue) {}
47
+ context 'msg without rq:job key' do
48
+ let(:msg) { {} }
30
49
 
31
- expect(WorkOverseerTestWorker.jobs.count).to eq(1)
50
+ it 'yields once' do
51
+ expect do |b|
52
+ overseer.call(worker, msg, queue, &b)
53
+ end.to yield_control.once
54
+ end
32
55
  end
33
- end
34
56
 
35
- context 'msg without rq:job key' do
36
- let(:msg) { {} }
57
+ context 'msg with job retry info and rq:job (existing job)' do
58
+ let(:msg) { { 'rq:job' => job.to_array, 'retry_count' => 0,
59
+ 'args' => args } }
60
+
61
+ it 'yields once' do
62
+ expect do |b|
63
+ overseer.call(worker, msg, queue, &b)
64
+ end.to yield_control.once
65
+ end
37
66
 
38
- it 'yields once' do
39
- expect do |b|
40
- Rekiq::Middleware::WorkOverseer.new
41
- .call(worker, msg, queue, &b)
42
- end.to yield_control.once
67
+ it 'does not schedule work' do
68
+ overseer.call(worker, msg, queue) {}
69
+
70
+ expect(WorkOverseerTestWorker.jobs.count).to eq(0)
71
+ end
43
72
  end
44
73
  end
45
74
 
46
- context 'msg with job retry info and rq:job (existing job)' do
47
- let(:job) { build(:job, schedule: schedule) }
48
- let(:msg) { { 'rq:job' => job.to_array, 'retry_count' => 0,
49
- 'args' => args } }
75
+ context 'worker has rekiq_canceller method set' do
76
+ let(:worker) { WorkOverseerCancelTestWorker.new }
77
+ let(:queue) { WorkOverseerCancelTestWorker.get_sidekiq_options['queue'] }
78
+
79
+ context 'msg with rq:ca key with value to cancel worker' do
80
+ let(:msg) do
81
+ { 'rq:job' => job.to_array, 'args' => args, 'rq:ca' => true }
82
+ end
83
+
84
+ it 'does not yield' do
85
+ expect do |b|
86
+ overseer.call(worker, msg, queue, &b)
87
+ end.not_to yield_control
88
+ end
50
89
 
51
- it 'yields once' do
52
- expect do |b|
53
- Rekiq::Middleware::WorkOverseer.new
54
- .call(worker, msg, queue, &b)
55
- end.to yield_control.once
90
+ it 'does not scheduler worker' do
91
+ expect(WorkOverseerCancelTestWorker.jobs.count).to eq(0)
92
+ end
56
93
  end
57
94
 
58
- it 'does not schedule work' do
59
- Rekiq::Middleware::WorkOverseer.new
60
- .call(worker, msg, queue) {}
95
+ context 'msg with rq:ca key with value that does not cancel worker' do
96
+ let(:msg) do
97
+ { 'rq:job' => job.to_array, 'args' => args, 'rq:ca' => false }
98
+ end
61
99
 
62
- expect(WorkOverseerTestWorker.jobs.count).to eq(0)
100
+ it 'does yield' do
101
+ expect do |b|
102
+ overseer.call(worker, msg, queue, &b)
103
+ end.to yield_control.once
104
+ end
63
105
  end
64
106
  end
65
107
  end
@@ -10,49 +10,65 @@ describe Rekiq::Scheduler do
10
10
  let(:worker) { SchedulerTestWorker.name }
11
11
  let(:queue) { 'test_queue' }
12
12
  let(:args) { [] }
13
+ let(:addon) { nil }
14
+ let(:c_args) { nil }
15
+ let(:scheduler) do
16
+ Rekiq::Scheduler.new(worker, queue, args, job, addon, c_args)
17
+ end
18
+ before { @jid, @work_time = scheduler.schedule }
13
19
 
14
20
  context 'given valid job' do
15
21
  let(:job) { build(:job) }
16
22
 
23
+ context 'give nil as addon argument' do
24
+ it 'creates sidekiq job' do
25
+ expect(SchedulerTestWorker.jobs.count).to eq(1)
26
+ end
27
+
28
+ it 'does not set key rq:addon in msg' do
29
+ expect(SchedulerTestWorker.jobs[0].key?('rq:addon')).to eq(false)
30
+ end
31
+ end
32
+
17
33
  context 'given not nil string as addon argument' do
18
34
  let(:addon) { { 'random_key' => Time.now.to_f } }
19
35
 
20
- context 'given initialized scheduler instance' do
21
- let(:scheduler) do
22
- Rekiq::Scheduler.new(worker, queue, args, job, addon)
23
- end
24
- before { @jid, @work_time = scheduler.schedule }
36
+ it 'creates sidekiq job' do
37
+ expect(SchedulerTestWorker.jobs.count).to eq(1)
38
+ end
39
+
40
+ it 'add key rq:addon in msg' do
41
+ expect(SchedulerTestWorker.jobs[0].key?('rq:addon')).to eq(true)
42
+ end
25
43
 
26
- it 'creates sidekiq job' do
27
- expect(SchedulerTestWorker.jobs.count).to eq(1)
28
- end
44
+ it 'sets addon value in key rq:addon' do
45
+ expect(SchedulerTestWorker.jobs[0]['rq:addon']).to eq(addon)
46
+ end
47
+ end
29
48
 
30
- it 'add key rq:addon in msg' do
31
- expect(SchedulerTestWorker.jobs[0].key?('rq:addon')).to eq(true)
32
- end
49
+ context 'given nil as canceller_args' do
50
+ it 'creates sidekiq job' do
51
+ expect(SchedulerTestWorker.jobs.count).to eq(1)
52
+ end
33
53
 
34
- it 'sets addon value in key rq:addon' do
35
- expect(SchedulerTestWorker.jobs[0]['rq:addon']).to eq(addon)
36
- end
54
+ it 'does not set key rq:ca in msg' do
55
+ expect(SchedulerTestWorker.jobs[0].key?('rq:ca')).to eq(false)
37
56
  end
38
57
  end
39
58
 
40
- context 'give nil as addon argument' do
41
- let(:addon) { nil }
59
+ context 'given non empty array as canceller_args' do
60
+ let(:c_args) { [1, 2, 3] }
42
61
 
43
- context 'given initialized scheduler instance' do
44
- let(:scheduler) do
45
- Rekiq::Scheduler.new(worker, queue, args, job, addon)
46
- end
47
- before { @jid, @work_time = scheduler.schedule }
62
+ it 'creates sidekiq job' do
63
+ expect(SchedulerTestWorker.jobs.count).to eq(1)
64
+ end
48
65
 
49
- it 'creates sidekiq job' do
50
- expect(SchedulerTestWorker.jobs.count).to eq(1)
51
- end
66
+ it 'sets key rq:ca in msg' do
67
+ expect(SchedulerTestWorker.jobs[0].key?('rq:ca')).to eq(true)
68
+ end
52
69
 
53
- it 'does not set key addon in msg' do
54
- expect(SchedulerTestWorker.jobs[0].key?('addon')).to eq(false)
55
- end
70
+ it 'sets key rq:ca in msg with passed value' do
71
+ expect(SchedulerTestWorker.jobs[0]['rq:ca']).to eq(c_args)
56
72
  end
57
73
  end
58
74
  end
@@ -7,6 +7,19 @@ describe Rekiq::Worker do
7
7
  include Sidekiq::Worker
8
8
  end
9
9
 
10
+ class CancellerUndefinedExampleWorker
11
+ include Sidekiq::Worker
12
+ rekiq_canceller :cancel
13
+ end
14
+
15
+ class CancellerDefinedExampleWorker
16
+ include Sidekiq::Worker
17
+ rekiq_canceller :cancel
18
+
19
+ def cancel
20
+ end
21
+ end
22
+
10
23
  context 'Class includes Sidekiq::Worker module' do
11
24
  it 'responds to perform_recurringly' do
12
25
  ExampleWorker.respond_to? :perform_recurringly
@@ -16,56 +29,100 @@ describe Rekiq::Worker do
16
29
  context 'scheduled one hour from now' do
17
30
  let(:time) { Time.now + 3600 }
18
31
  let(:schedule) { IceCube::Schedule.new(time) }
19
- before do
20
- @jid = ExampleWorker.perform_recurringly(schedule)
21
- end
22
32
 
23
- it 'returns created jid' do
24
- expect(@jid).not_to be_nil
25
- end
33
+ context 'for worker with rekiq_canceller set with non defined method' do
34
+ before do
35
+ begin
36
+ @jid =
37
+ CancellerUndefinedExampleWorker.perform_recurringly(schedule)
38
+ rescue
39
+ end
40
+ end
26
41
 
27
- it 'schedules worker' do
28
- expect(ExampleWorker.jobs.count).to eq(1)
29
- end
42
+ it 'raises error' do
43
+ expect do
44
+ CancellerUndefinedExampleWorker.perform_recurringly(schedule)
45
+ end.to raise_error
46
+ end
30
47
 
31
- it 'schedules worker for one hour from now' do
32
- expect(ExampleWorker.jobs[0]['at']).to eq(time.to_f)
48
+ it 'does not schedule worker' do
49
+ expect(CancellerUndefinedExampleWorker.jobs.count).to eq(0)
50
+ end
33
51
  end
34
- end
35
52
 
36
- context 'scheduled one hour from now ' \
37
- 'shift set to minus 5 minutes' do
38
- let(:time) { Time.now + 3600 }
39
- let(:schedule) { IceCube::Schedule.new(time) }
40
- let(:shift) { -5 * 60 }
41
- before do
42
- @jid = ExampleWorker.perform_recurringly(schedule) do |options|
43
- options.shift = shift
44
- end
45
- end
53
+ context 'for worker with rekiq_canceller set with defined method' do
54
+ before do
55
+ @jid = CancellerDefinedExampleWorker.perform_recurringly(schedule)
56
+ end
46
57
 
47
- it 'returns created job id' do
48
- expect(@jid).not_to be_nil
49
- end
58
+ it 'does not raise error' do
59
+ expect do
60
+ CancellerDefinedExampleWorker.perform_recurringly(schedule)
61
+ end.not_to raise_error
62
+ end
50
63
 
51
- it 'schedules worker' do
52
- expect(ExampleWorker.jobs.count).to eq(1)
53
- end
64
+ it 'returns created jid' do
65
+ expect(@jid).not_to be_nil
66
+ end
67
+
68
+ it 'schedules worker' do
69
+ expect(CancellerDefinedExampleWorker.jobs.count).to eq(1)
70
+ end
54
71
 
55
- it 'yields once' do
56
- expect do |b|
57
- ExampleWorker.perform_recurringly(schedule, &b)
58
- end.to yield_control.once
72
+ it 'schedules worker for one hour from now' do
73
+ expect(CancellerDefinedExampleWorker.jobs[0]['at']).to eq(time.to_f)
74
+ end
59
75
  end
60
76
 
61
- it 'sets shift in rq:job' do
62
- array = ExampleWorker.jobs[0]['rq:job']
77
+ context 'invoked without config' do
78
+ before do
79
+ @jid = ExampleWorker.perform_recurringly(schedule)
80
+ end
81
+
82
+ it 'returns created jid' do
83
+ expect(@jid).not_to be_nil
84
+ end
63
85
 
64
- expect(array[1]).to eq(shift)
86
+ it 'schedules worker' do
87
+ expect(ExampleWorker.jobs.count).to eq(1)
88
+ end
89
+
90
+ it 'schedules worker for one hour from now' do
91
+ expect(ExampleWorker.jobs[0]['at']).to eq(time.to_f)
92
+ end
65
93
  end
66
94
 
67
- it 'schedules worker for one hour minus 5 minutes from now' do
68
- expect(ExampleWorker.jobs[0]['at']).to eq(time.to_f + shift)
95
+ context 'shift set to minus 5 minutes' do
96
+ let(:shift) { -5 * 60 }
97
+ before do
98
+ @jid = ExampleWorker.perform_recurringly(schedule) do |config|
99
+ config.shift = shift
100
+ end
101
+ end
102
+
103
+ it 'returns created job id' do
104
+ expect(@jid).not_to be_nil
105
+ end
106
+
107
+ it 'schedules worker' do
108
+ expect(ExampleWorker.jobs.count).to eq(1)
109
+ end
110
+
111
+ it 'yields once' do
112
+ expect do |b|
113
+ ExampleWorker.perform_recurringly(schedule, &b)
114
+ end.to yield_control.once
115
+ end
116
+
117
+ it 'sets shift in rq:job' do
118
+ array = ExampleWorker.jobs[0]['rq:job']
119
+
120
+ expect(array[1]).to eq(shift)
121
+ end
122
+
123
+ it 'schedules worker for one hour minus 5 minutes from now' do
124
+ expect(ExampleWorker.jobs[0]['at']).to eq(time.to_f + shift)
125
+ end
69
126
  end
70
127
  end
71
128
  end
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.5.0
4
+ version: 0.6.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-07-20 00:00:00.000000000 Z
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler