delayed_job_groups_plugin 0.1.3 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Delayed
4
4
  module JobGroups
5
- VERSION = '0.1.3'
5
+ VERSION = '0.4.3'
6
6
  end
7
7
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Delayed
4
+ module JobGroups
5
+ module YamlLoader
6
+ def self.load(yaml)
7
+ return yaml unless yaml.is_a?(String) && /^---/.match(yaml)
8
+ YAML.load_dj(yaml)
9
+ end
10
+
11
+ def self.dump(object)
12
+ return if object.nil?
13
+ YAML.dump(object)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
4
  require 'active_record'
@@ -8,6 +8,7 @@ require 'delayed/job_groups/compatibility'
8
8
  require 'delayed/job_groups/job_extensions'
9
9
  require 'delayed/job_groups/job_group'
10
10
  require 'delayed/job_groups/plugin'
11
+ require 'delayed/job_groups/yaml_loader'
11
12
  require 'delayed/job_groups/version'
12
13
 
13
14
  Delayed::Backend::ActiveRecord::Job.send(:include, Delayed::JobGroups::JobExtensions)
@@ -1,4 +1,4 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'rails/generators'
4
4
  require 'rails/generators/migration'
@@ -8,7 +8,7 @@ module DelayedJobGroupsPlugin
8
8
  class InstallGenerator < Rails::Generators::Base
9
9
  include Rails::Generators::Migration
10
10
 
11
- self.source_paths << File.join(File.dirname(__FILE__), 'templates')
11
+ source_paths << File.join(File.dirname(__FILE__), 'templates')
12
12
 
13
13
  def create_migration_file
14
14
  migration_template('migration.rb', 'db/migrate/create_delayed_job_groups.rb')
@@ -1,4 +1,4 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  class CreateDelayedJobGroups < ActiveRecord::Migration
4
4
 
@@ -1,6 +1,6 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
- ActiveRecord::Schema.define(:version => 0) do
3
+ ActiveRecord::Schema.define(version: 0) do
4
4
 
5
5
  create_table(:delayed_jobs, force: true) do |t|
6
6
  t.integer :priority, default: 0
@@ -1,6 +1,4 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
1
+ # frozen_string_literal: true
4
2
 
5
3
  describe Delayed::JobGroups::JobGroup do
6
4
 
@@ -19,7 +17,7 @@ describe Delayed::JobGroups::JobGroup do
19
17
 
20
18
  before do
21
19
  Timecop.freeze(current_time)
22
- Delayed::Job.stub(:enqueue)
20
+ allow(Delayed::Job).to receive(:enqueue)
23
21
  end
24
22
 
25
23
  after do
@@ -28,21 +26,21 @@ describe Delayed::JobGroups::JobGroup do
28
26
 
29
27
  shared_examples "the job group was completed" do
30
28
  it "queues the completion job" do
31
- Delayed::Job.should have_received(:enqueue).with(on_completion_job, on_completion_job_options)
29
+ expect(Delayed::Job).to have_received(:enqueue).with(on_completion_job, on_completion_job_options)
32
30
  end
33
31
 
34
32
  it "destroys the job group" do
35
- job_group.should have_been_destroyed
33
+ expect(job_group).to have_been_destroyed
36
34
  end
37
35
  end
38
36
 
39
37
  shared_examples "the job group was not completed" do
40
38
  it "does not queue the completion job" do
41
- Delayed::Job.should_not have_received(:enqueue)
39
+ expect(Delayed::Job).not_to have_received(:enqueue)
42
40
  end
43
41
 
44
42
  it "does not destroy the job group" do
45
- job_group.should_not have_been_destroyed
43
+ expect(job_group).not_to have_been_destroyed
46
44
  end
47
45
  end
48
46
 
@@ -51,15 +49,16 @@ describe Delayed::JobGroups::JobGroup do
51
49
  context "when no jobs exist" do
52
50
  before { job_group.mark_queueing_complete }
53
51
 
54
- it { should be_queueing_complete }
52
+ it { is_expected.to be_queueing_complete }
55
53
  it_behaves_like "the job group was completed"
56
54
  end
57
55
 
58
56
  context "when no jobs exist but the job group is blocked" do
59
57
  let(:blocked) { true }
58
+
60
59
  before { job_group.mark_queueing_complete }
61
60
 
62
- it { should be_queueing_complete }
61
+ it { is_expected.to be_queueing_complete }
63
62
  it_behaves_like "the job group was not completed"
64
63
  end
65
64
 
@@ -69,7 +68,7 @@ describe Delayed::JobGroups::JobGroup do
69
68
  job_group.mark_queueing_complete
70
69
  end
71
70
 
72
- it { should be_queueing_complete }
71
+ it { is_expected.to be_queueing_complete }
73
72
  it_behaves_like "the job group was not completed"
74
73
  end
75
74
  end
@@ -117,11 +116,11 @@ describe Delayed::JobGroups::JobGroup do
117
116
  include_context "complete job and check job group complete"
118
117
 
119
118
  it "queues the completion job with empty options" do
120
- Delayed::Job.should have_received(:enqueue).with(on_completion_job, {})
119
+ expect(Delayed::Job).to have_received(:enqueue).with(on_completion_job, {})
121
120
  end
122
121
 
123
122
  it "destroys the job group" do
124
- job_group.should have_been_destroyed
123
+ expect(job_group).to have_been_destroyed
125
124
  end
126
125
  end
127
126
 
@@ -131,11 +130,11 @@ describe Delayed::JobGroups::JobGroup do
131
130
  include_context "complete job and check job group complete"
132
131
 
133
132
  it "doesn't queues the non-existent completion job" do
134
- Delayed::Job.should_not have_received(:enqueue)
133
+ expect(Delayed::Job).not_to have_received(:enqueue)
135
134
  end
136
135
 
137
136
  it "destroys the job group" do
138
- job_group.should have_been_destroyed
137
+ expect(job_group).to have_been_destroyed
139
138
  end
140
139
  end
141
140
  end
@@ -149,7 +148,7 @@ describe Delayed::JobGroups::JobGroup do
149
148
 
150
149
  shared_examples "it enqueues the job in the correct blocked state" do
151
150
  it "enqueues the job in the same blocked state as the job group" do
152
- Delayed::Job.should have_received(:enqueue).with(job, job_group_id: job_group.id, blocked: blocked)
151
+ expect(Delayed::Job).to have_received(:enqueue).with(job, job_group_id: job_group.id, blocked: blocked)
153
152
  end
154
153
  end
155
154
 
@@ -169,7 +168,7 @@ describe Delayed::JobGroups::JobGroup do
169
168
  job_group.unblock
170
169
  end
171
170
 
172
- its(:blocked?) { should be_false }
171
+ its(:blocked?) { is_expected.to be(false) }
173
172
  end
174
173
 
175
174
  context "when the JobGroup is blocked" do
@@ -184,10 +183,10 @@ describe Delayed::JobGroups::JobGroup do
184
183
  Timecop.freeze(unblock_time) { job_group.unblock }
185
184
  end
186
185
 
187
- its(:blocked?) { should be_false }
186
+ its(:blocked?) { is_expected.to be(false) }
188
187
 
189
188
  it "sets the job's run_at to the unblocked time" do
190
- job.reload.run_at.should eq unblock_time
189
+ expect(job.reload.run_at).to eq unblock_time
191
190
  end
192
191
 
193
192
  it_behaves_like "the job group was not completed"
@@ -199,7 +198,7 @@ describe Delayed::JobGroups::JobGroup do
199
198
  job_group.unblock
200
199
  end
201
200
 
202
- its(:blocked?) { should be_false }
201
+ its(:blocked?) { is_expected.to be(false) }
203
202
  it_behaves_like "the job group was completed"
204
203
  end
205
204
  end
@@ -214,15 +213,15 @@ describe Delayed::JobGroups::JobGroup do
214
213
  end
215
214
 
216
215
  it "destroys the job group" do
217
- job_group.should have_been_destroyed
216
+ expect(job_group).to have_been_destroyed
218
217
  end
219
218
 
220
219
  it "destroys queued jobs" do
221
- queued_job.should have_been_destroyed
220
+ expect(queued_job).to have_been_destroyed
222
221
  end
223
222
 
224
223
  it "does not destroy running jobs" do
225
- running_job.should_not have_been_destroyed
224
+ expect(running_job).not_to have_been_destroyed
226
225
  end
227
226
  end
228
227
 
@@ -1,77 +1,72 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
1
+ # frozen_string_literal: true
4
2
 
5
3
  describe Delayed::JobGroups::Plugin do
6
-
7
- before(:all) do
4
+ before do
8
5
  @old_max_attempts = Delayed::Worker.max_attempts
9
6
  Delayed::Worker.max_attempts = 2
10
- end
11
7
 
12
- after(:all) do
13
- Delayed::Worker.max_attempts = @old_max_attempts
14
- end
15
-
16
- before(:each) do
17
8
  CompletionJob.invoked = false
18
9
  CancellationJob.invoked = false
19
10
  end
20
11
 
12
+ after do
13
+ Delayed::Worker.max_attempts = @old_max_attempts
14
+ end
15
+
21
16
  let!(:job_group) { Delayed::JobGroups::JobGroup.create!(on_completion_job: CompletionJob.new) }
22
17
 
23
18
  it "runs the completion job after completing other jobs" do
24
19
  job_group.enqueue(NoOpJob.new)
25
20
  job_group.enqueue(NoOpJob.new)
26
21
  job_group.mark_queueing_complete
27
- job_group_count.should eq 1
28
- queued_job_count.should eq 2
22
+ expect(job_group_count).to eq 1
23
+ expect(queued_job_count).to eq 2
29
24
 
30
25
  # Run our first job
31
26
  Delayed::Worker.new.work_off(1)
32
- CompletionJob.invoked.should be_false
33
- job_group_count.should eq 1
34
- queued_job_count.should eq 1
27
+ expect(CompletionJob.invoked).to be(false)
28
+ expect(job_group_count).to eq 1
29
+ expect(queued_job_count).to eq 1
35
30
 
36
31
  # Run our second job which should enqueue the completion job
37
32
  Delayed::Worker.new.work_off(1)
38
- CompletionJob.invoked.should be_false
39
- job_group_count.should eq 0
40
- queued_job_count.should eq 1
33
+ expect(CompletionJob.invoked).to be(false)
34
+ expect(job_group_count).to eq 0
35
+ expect(queued_job_count).to eq 1
41
36
 
42
37
  # Now we should run the completion job
43
38
  Delayed::Worker.new.work_off(1)
44
- CompletionJob.invoked.should be_true
45
- queued_job_count.should eq 0
39
+ expect(CompletionJob.invoked).to be(true)
40
+ expect(queued_job_count).to eq 0
46
41
  end
47
42
 
48
43
  it "only runs the completion job after queueing is completed" do
49
44
  job_group.enqueue(NoOpJob.new)
50
45
  job_group.enqueue(NoOpJob.new)
51
- job_group_count.should eq 1
52
- queued_job_count.should eq 2
46
+ expect(job_group_count).to eq 1
47
+ expect(queued_job_count).to eq 2
53
48
 
54
49
  # Run our first job
55
50
  Delayed::Worker.new.work_off(1)
56
- CompletionJob.invoked.should be_false
57
- job_group_count.should eq 1
58
- queued_job_count.should eq 1
51
+ expect(CompletionJob.invoked).to be(false)
52
+ expect(job_group_count).to eq 1
53
+ expect(queued_job_count).to eq 1
59
54
 
60
55
  # Run our second job
61
56
  Delayed::Worker.new.work_off(1)
62
- CompletionJob.invoked.should be_false
63
- job_group_count.should eq 1
64
- queued_job_count.should eq 0
57
+ expect(CompletionJob.invoked).to be(false)
58
+ expect(job_group_count).to eq 1
59
+ expect(queued_job_count).to eq 0
65
60
 
66
61
  # Mark queueing complete which should queue the completion job
67
62
  job_group.mark_queueing_complete
68
- job_group_count.should eq 0
69
- queued_job_count.should eq 1
63
+ expect(job_group_count).to eq 0
64
+ expect(queued_job_count).to eq 1
70
65
 
71
66
  # Now we should run the completion job
72
67
  Delayed::Worker.new.work_off(1)
73
- CompletionJob.invoked.should be_true
74
- queued_job_count.should eq 0
68
+ expect(CompletionJob.invoked).to be(true)
69
+ expect(queued_job_count).to eq 0
75
70
  end
76
71
 
77
72
  describe "job failures" do
@@ -83,15 +78,16 @@ describe Delayed::JobGroups::Plugin do
83
78
  job_group.enqueue(FailingJob.new)
84
79
  job_group.enqueue(NoOpJob.new)
85
80
  job_group.mark_queueing_complete
86
- queued_job_count.should eq 2
87
- job_group_count.should eq 1
81
+ expect(queued_job_count).to eq 2
82
+ expect(job_group_count).to eq 1
88
83
 
89
84
  # Run the job which should fail and cancel the JobGroup
90
85
  Delayed::Worker.new.work_off(1)
91
- CompletionJob.invoked.should be_false
92
- failed_job_count.should eq 1
93
- queued_job_count.should eq 0
94
- job_group_count.should eq 0
86
+ # Completion job is not invoked
87
+ expect(CompletionJob.invoked).to be(false)
88
+ expect(failed_job_count).to eq 1
89
+ expect(queued_job_count).to eq 0
90
+ expect(job_group_count).to eq 0
95
91
  end
96
92
  end
97
93
 
@@ -105,15 +101,68 @@ describe Delayed::JobGroups::Plugin do
105
101
  job_group.enqueue(FailingJob.new)
106
102
  job_group.enqueue(NoOpJob.new)
107
103
  job_group.mark_queueing_complete
108
- queued_job_count.should eq 2
109
- job_group_count.should eq 1
104
+ expect(queued_job_count).to eq 2
105
+ expect(job_group_count).to eq 1
110
106
 
111
107
  # Run the job which should fail don't cancel the JobGroup
112
108
  Delayed::Worker.new.work_off(1)
113
- CancellationJob.invoked.should be_false
114
- failed_job_count.should eq 1
115
- queued_job_count.should eq 1
116
- job_group_count.should eq 1
109
+ expect(CancellationJob.invoked).to be(false)
110
+ expect(failed_job_count).to eq 1
111
+ expect(queued_job_count).to eq 1
112
+ expect(job_group_count).to eq 1
113
+
114
+ # Run the last job
115
+ Delayed::Worker.new.work_off(1)
116
+ expect(failed_job_count).to eq 1
117
+ expect(queued_job_count).to eq 1
118
+ expect(job_group_count).to eq 0
119
+
120
+ # Run the completion job
121
+ Delayed::Worker.new.work_off(1)
122
+ # Completion job is invoked
123
+ expect(CompletionJob.invoked).to be(true)
124
+ expect(failed_job_count).to eq 1
125
+ expect(queued_job_count).to eq 0
126
+ expect(job_group_count).to eq 0
127
+ end
128
+
129
+ it "runs completion job if last job failed" do
130
+ Delayed::Worker.max_attempts = 2
131
+
132
+ job_group.enqueue(NoOpJob.new)
133
+ job_group.enqueue(FailingJob.new)
134
+ job_group.mark_queueing_complete
135
+ expect(queued_job_count).to eq 2
136
+ expect(job_group_count).to eq 1
137
+
138
+ # Run the non failing job
139
+ Delayed::Worker.new.work_off(1)
140
+ expect(failed_job_count).to eq 0
141
+ expect(queued_job_count).to eq 1
142
+ expect(job_group_count).to eq 1
143
+
144
+ # Run the job which should error
145
+ Delayed::Worker.new.work_off(1)
146
+ # Completion job is not invoked
147
+ expect(CompletionJob.invoked).to be(false)
148
+ expect(failed_job_count).to eq 0
149
+ expect(queued_job_count).to eq 1
150
+ expect(job_group_count).to eq 1
151
+
152
+ # Run the job again which should fail
153
+ Timecop.travel(1.minute.from_now)
154
+ Delayed::Worker.new.work_off(1)
155
+ expect(failed_job_count).to eq 1
156
+ expect(queued_job_count).to eq 1
157
+ expect(job_group_count).to eq 0
158
+
159
+ # Run the completion job
160
+ Delayed::Worker.new.work_off(1)
161
+ # Completion job is invoked
162
+ expect(CompletionJob.invoked).to be(true)
163
+ expect(failed_job_count).to eq 1
164
+ expect(queued_job_count).to eq 0
165
+ expect(job_group_count).to eq 0
117
166
  end
118
167
  end
119
168
  end
@@ -121,12 +170,12 @@ describe Delayed::JobGroups::Plugin do
121
170
  it "doesn't retry failed jobs if the job group has been canceled" do
122
171
  job_group.cancel
123
172
  Delayed::Job.enqueue(FailingJob.new, job_group_id: job_group.id)
124
- queued_job_count.should eq 1
173
+ expect(queued_job_count).to eq 1
125
174
 
126
175
  # Run the job which should fail and should not queue a retry
127
176
  Delayed::Worker.new.work_off(1)
128
- failed_job_count.should eq 1
129
- queued_job_count.should eq 0
177
+ expect(failed_job_count).to eq 1
178
+ expect(queued_job_count).to eq 0
130
179
  end
131
180
 
132
181
  it "doesn't run jobs until they're unblocked" do
@@ -136,32 +185,32 @@ describe Delayed::JobGroups::Plugin do
136
185
  job_group.enqueue(NoOpJob.new)
137
186
  job_group.enqueue(NoOpJob.new)
138
187
  job_group.mark_queueing_complete
139
- Delayed::Job.count.should eq 2
188
+ expect(Delayed::Job.count).to eq 2
140
189
 
141
190
  # No jobs should run because they're blocked
142
191
  (successes, failures) = Delayed::Worker.new.work_off
143
- successes.should eq 0
144
- failures.should eq 0
145
- Delayed::Job.count.should eq 2
192
+ expect(successes).to eq 0
193
+ expect(failures).to eq 0
194
+ expect(Delayed::Job.count).to eq 2
146
195
 
147
196
  job_group.unblock
148
197
 
149
198
  # Run our first job
150
199
  Delayed::Worker.new.work_off(1)
151
- CompletionJob.invoked.should be_false
152
- job_group_count.should eq 1
153
- Delayed::Job.count.should eq 1
200
+ expect(CompletionJob.invoked).to be(false)
201
+ expect(job_group_count).to eq 1
202
+ expect(Delayed::Job.count).to eq 1
154
203
 
155
204
  # Run our second job which should enqueue the completion job
156
205
  Delayed::Worker.new.work_off(1)
157
- CompletionJob.invoked.should be_false
158
- job_group_count.should eq 0
159
- Delayed::Job.count.should eq 1
206
+ expect(CompletionJob.invoked).to be(false)
207
+ expect(job_group_count).to eq 0
208
+ expect(Delayed::Job.count).to eq 1
160
209
 
161
210
  # Now we should run the completion job
162
211
  Delayed::Worker.new.work_off(1)
163
- CompletionJob.invoked.should be_true
164
- Delayed::Job.count.should eq 0
212
+ expect(CompletionJob.invoked).to be(true)
213
+ expect(Delayed::Job.count).to eq 0
165
214
  end
166
215
 
167
216
  context "when a cancellation job is provided" do
@@ -176,23 +225,23 @@ describe Delayed::JobGroups::Plugin do
176
225
  job_group.enqueue(FailingJob.new)
177
226
  job_group.enqueue(NoOpJob.new)
178
227
  job_group.mark_queueing_complete
179
- queued_job_count.should eq 2
180
- job_group_count.should eq 1
228
+ expect(queued_job_count).to eq 2
229
+ expect(job_group_count).to eq 1
181
230
 
182
231
  # Run the job which should fail and cancel the JobGroup
183
232
  Delayed::Worker.new.work_off(1)
184
- CompletionJob.invoked.should be_false
185
- CancellationJob.invoked.should be_false
186
- failed_job_count.should eq 1
233
+ expect(CompletionJob.invoked).to be(false)
234
+ expect(CancellationJob.invoked).to be(false)
235
+ expect(failed_job_count).to eq 1
187
236
 
188
- queued_job_count.should eq 1
189
- job_group_count.should eq 0
237
+ expect(queued_job_count).to eq 1
238
+ expect(job_group_count).to eq 0
190
239
 
191
240
  # Now we should run the cancellation job
192
241
  Delayed::Worker.new.work_off(1)
193
- CompletionJob.invoked.should be_false
194
- CancellationJob.invoked.should be_true
195
- queued_job_count.should eq 0
242
+ expect(CompletionJob.invoked).to be(false)
243
+ expect(CancellationJob.invoked).to be(true)
244
+ expect(queued_job_count).to eq 0
196
245
  end
197
246
 
198
247
  it "runs the cancellation job after the job group is cancelled" do
@@ -201,14 +250,14 @@ describe Delayed::JobGroups::Plugin do
201
250
  job_group.mark_queueing_complete
202
251
  job_group.cancel
203
252
 
204
- #cancellation job should be queued
205
- queued_job_count.should eq 1
206
- CancellationJob.invoked.should be_false
253
+ # cancellation job should be queued
254
+ expect(queued_job_count).to eq 1
255
+ expect(CancellationJob.invoked).to be(false)
207
256
 
208
257
  # Run the cancellation job
209
258
  Delayed::Worker.new.work_off(1)
210
- CancellationJob.invoked.should be_true
211
- queued_job_count.should eq 0
259
+ expect(CancellationJob.invoked).to be(true)
260
+ expect(queued_job_count).to eq 0
212
261
  end
213
262
  end
214
263
 
@@ -219,21 +268,21 @@ describe Delayed::JobGroups::Plugin do
219
268
  job_group.enqueue(NoOpJob.new)
220
269
  job_group.enqueue(NoOpJob.new)
221
270
  job_group.mark_queueing_complete
222
- job_group_count.should eq 1
223
- queued_job_count.should eq 2
224
- failed_job_count.should eq 0
271
+ expect(job_group_count).to eq 1
272
+ expect(queued_job_count).to eq 2
273
+ expect(failed_job_count).to eq 0
225
274
 
226
275
  # Run our first job
227
276
  Delayed::Worker.new.work_off(1)
228
- job_group_count.should eq 1
229
- queued_job_count.should eq 1
230
- failed_job_count.should eq 0
277
+ expect(job_group_count).to eq 1
278
+ expect(queued_job_count).to eq 1
279
+ expect(failed_job_count).to eq 0
231
280
 
232
281
  # Run our second job which should delete the job group
233
282
  Delayed::Worker.new.work_off(1)
234
- job_group_count.should eq 0
235
- queued_job_count.should eq 0
236
- failed_job_count.should eq 0
283
+ expect(job_group_count).to eq 0
284
+ expect(queued_job_count).to eq 0
285
+ expect(failed_job_count).to eq 0
237
286
  end
238
287
  end
239
288