delayed_cron_job 0.7.0 → 0.7.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a6855db526a23fa48933504b568fded11df2722
4
- data.tar.gz: c28537792117c9937fb5deb5c18f287ebf90596f
3
+ metadata.gz: ef66952c36796b2dfa66c343a31631d061d09bd1
4
+ data.tar.gz: fba9ad110c34d1e475bc8057d818b9d0c0fa02d4
5
5
  SHA512:
6
- metadata.gz: 5abd5f9a6d32b08f7f92c274d355daaddebf32d9e93ec9197982c044c6a9928fc15a79a3412fa6c6589f98356d0183c768d20c6b8ad3cbbabd56bf256c2fa35c
7
- data.tar.gz: 8abb91541f341a7e620ba2c602370e384b3093a0e6c4a7fd87ab0d6510c4610dc5be7dee8cdb562140320074805f3f1a0ff7c6d05969ca1ffe4c305087e60f09
6
+ metadata.gz: da230b6a7fef72f96129ff81f32f8e1345b025a6f0b54cc6bcc482d44a11e823480a2f3a0715c46687a98226d8f9c7e210ccf3f6892b08723ce6f78fd884e498
7
+ data.tar.gz: 150cd81fe161e5cd030a99924345902e47788877c861db22975ad6db80aa9eeb593e09abbd11a67c35054eb13603eddcb8313b85f4698368e78e879e0e1229ec
@@ -3,6 +3,7 @@ require 'English'
3
3
  require 'delayed_cron_job/cronline'
4
4
  require 'delayed_cron_job/plugin'
5
5
  require 'delayed_cron_job/version'
6
+ require 'delayed_cron_job/backend/updatable_cron'
6
7
 
7
8
  module DelayedCronJob
8
9
 
@@ -11,10 +12,14 @@ end
11
12
  if defined?(Delayed::Backend::Mongoid)
12
13
  Delayed::Backend::Mongoid::Job.field :cron, :type => String
13
14
  Delayed::Backend::Mongoid::Job.attr_accessible(:cron) if Delayed::Backend::Mongoid::Job.respond_to?(:attr_accessible)
15
+ Delayed::Backend::Mongoid::Job.send(:include, DelayedCronJob::Backend::UpdatableCron)
14
16
  end
15
17
 
16
- if defined?(Delayed::Backend::ActiveRecord) && Delayed::Backend::ActiveRecord::Job.respond_to?(:attr_accessible)
17
- Delayed::Backend::ActiveRecord::Job.attr_accessible(:cron)
18
+ if defined?(Delayed::Backend::ActiveRecord)
19
+ Delayed::Backend::ActiveRecord::Job.send(:include, DelayedCronJob::Backend::UpdatableCron)
20
+ if Delayed::Backend::ActiveRecord::Job.respond_to?(:attr_accessible)
21
+ Delayed::Backend::ActiveRecord::Job.attr_accessible(:cron)
22
+ end
18
23
  end
19
24
 
20
25
  Delayed::Worker.plugins << DelayedCronJob::Plugin
@@ -0,0 +1,17 @@
1
+ module DelayedCronJob
2
+ module Backend
3
+ module UpdatableCron
4
+
5
+ def self.included(klass)
6
+ klass.send(:before_save, :set_next_run_at, :if => :cron_changed?)
7
+ end
8
+
9
+ def set_next_run_at
10
+ if cron.present?
11
+ self.run_at = Cronline.new(cron).next_time(Delayed::Job.db_time_now)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -2,20 +2,12 @@ module DelayedCronJob
2
2
  class Plugin < Delayed::Plugin
3
3
 
4
4
  class << self
5
- def next_run_at(job)
6
- job.run_at = Cronline.new(job.cron).next_time(Delayed::Job.db_time_now)
7
- end
8
-
9
5
  def cron?(job)
10
6
  job.cron.present?
11
7
  end
12
8
  end
13
9
 
14
10
  callbacks do |lifecycle|
15
- # Calculate the next run_at based on the cron attribute before enqueue.
16
- lifecycle.before(:enqueue) do |job|
17
- next_run_at(job) if cron?(job)
18
- end
19
11
 
20
12
  # Prevent rescheduling of failed jobs as this is already done
21
13
  # after perform.
@@ -39,6 +31,13 @@ module DelayedCronJob
39
31
  end
40
32
  end
41
33
 
34
+ # Update the cron expression from the database in case it was updated.
35
+ lifecycle.after(:invoke_job) do |job|
36
+ if cron?(job)
37
+ job.cron = job.class.where(:id => job.id).pluck(:cron).first
38
+ end
39
+ end
40
+
42
41
  # Schedule the next run based on the cron attribute.
43
42
  lifecycle.after(:perform) do |worker, job|
44
43
  if cron?(job)
@@ -48,7 +47,6 @@ module DelayedCronJob
48
47
  next_job.locked_at = nil
49
48
  next_job.locked_by = nil
50
49
  next_job.attempts += 1
51
- next_run_at(next_job)
52
50
  next_job.save!
53
51
  end
54
52
  end
@@ -1,3 +1,3 @@
1
1
  module DelayedCronJob
2
- VERSION = '0.7.0'
2
+ VERSION = '0.7.1'
3
3
  end
@@ -31,7 +31,7 @@ describe DelayedCronJob do
31
31
 
32
32
  it 'schedules a new job after success' do
33
33
  job.update_column(:run_at, now)
34
- job.reload
34
+ job.reload # adjusts granularity of run_at datetime
35
35
 
36
36
  worker.work_off
37
37
 
@@ -48,7 +48,7 @@ describe DelayedCronJob do
48
48
  it 'schedules a new job after failure' do
49
49
  allow_any_instance_of(TestJob).to receive(:perform).and_raise('Fail!')
50
50
  job.update(run_at: now)
51
- job.reload
51
+ job.reload # adjusts granularity of run_at datetime
52
52
 
53
53
  worker.work_off
54
54
 
@@ -98,7 +98,7 @@ describe DelayedCronJob do
98
98
  expect(j.last_error).to eq(nil)
99
99
  end
100
100
 
101
- it 'has correct last_error after success' do
101
+ it 'has updated last_error after failure' do
102
102
  allow_any_instance_of(TestJob).to receive(:perform).and_raise('Fail!')
103
103
  job.update(run_at: now, last_error: 'Last error')
104
104
 
@@ -138,6 +138,36 @@ describe DelayedCronJob do
138
138
  j = Delayed::Job.first
139
139
  expect(j.attempts).to eq(job.attempts + 1)
140
140
  end
141
+
142
+ it 'updates run_at if cron is changed' do
143
+ job.update!(cron: '1 10 * * *')
144
+ expect(job.run_at.min).to eq(1)
145
+ end
146
+
147
+ it 'uses new cron when this is updated while job is running' do
148
+ job.update_column(:run_at, now)
149
+ allow_any_instance_of(TestJob).to receive(:perform) { job.update!(cron: '1 10 * * *') }
150
+
151
+ worker.work_off
152
+
153
+ j = Delayed::Job.first
154
+ expect(j.run_at.min).to eq(1)
155
+ end
156
+
157
+ it 'does not reschedule job if cron is cleared while job is running' do
158
+ job.update_column(:run_at, now)
159
+ allow_any_instance_of(TestJob).to receive(:perform) { job.update!(cron: '') }
160
+
161
+ expect { worker.work_off }.to change { Delayed::Job.count }.by(-1)
162
+ end
163
+
164
+ it 'does not reschedule job if model is deleted while job is running' do
165
+ job.update_column(:run_at, now)
166
+ allow_any_instance_of(TestJob).to receive(:perform) { job.destroy! }
167
+
168
+ expect { worker.work_off }.to change { Delayed::Job.count }.by(-1)
169
+ end
170
+
141
171
  end
142
172
 
143
173
  context 'without cron' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed_cron_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pascal Zumkehr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-29 00:00:00.000000000 Z
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: delayed_job
@@ -129,6 +129,7 @@ files:
129
129
  - lib/delayed_cron_job.rb
130
130
  - lib/delayed_cron_job/active_job/enqueuing.rb
131
131
  - lib/delayed_cron_job/active_job/queue_adapter.rb
132
+ - lib/delayed_cron_job/backend/updatable_cron.rb
132
133
  - lib/delayed_cron_job/cronline.rb
133
134
  - lib/delayed_cron_job/plugin.rb
134
135
  - lib/delayed_cron_job/version.rb
@@ -157,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
158
  version: '0'
158
159
  requirements: []
159
160
  rubyforge_project:
160
- rubygems_version: 2.4.5.1
161
+ rubygems_version: 2.4.8
161
162
  signing_key:
162
163
  specification_version: 4
163
164
  summary: An extension to Delayed::Job that allows you to set cron expressions for