dalliance 0.6.0 → 0.9.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.
@@ -12,6 +12,7 @@ RSpec.describe DallianceModel do
12
12
 
13
13
  before do
14
14
  Resque.remove_queue(:dalliance)
15
+ Resque.remove_queue(:notaqueue)
15
16
  end
16
17
 
17
18
  context "no worker_class" do
@@ -114,6 +115,123 @@ RSpec.describe DallianceModel do
114
115
  end
115
116
  end
116
117
 
118
+ context 'reprocess' do
119
+ before :all do
120
+ DallianceModel.dalliance_options[:dalliance_method] = :dalliance_success_method
121
+ DallianceModel.dalliance_options[:worker_class] = Dalliance::Workers::Resque
122
+ DallianceModel.dalliance_options[:queue] = 'dalliance'
123
+ end
124
+
125
+ before do
126
+ subject.dalliance_process
127
+ subject.reload
128
+ end
129
+
130
+ it 'successfully runs the dalliance_reprocess method' do
131
+ Resque::Stat.clear(:processed)
132
+ Resque::Stat.clear(:failed)
133
+
134
+ subject.dalliance_background_reprocess
135
+ Resque::Worker.new(:dalliance).process
136
+ subject.reload
137
+
138
+ aggregate_failures do
139
+ expect(subject).to be_successful
140
+ expect(Resque.size(:dalliance)).to eq(0)
141
+ expect(Resque::Stat[:processed]).to eq(1)
142
+ expect(Resque::Stat[:failed]).to eq(0)
143
+ expect(subject.reprocessed_count).to eq(1)
144
+ end
145
+ end
146
+
147
+ it 'increases the total processing time counter' do
148
+ original_duration = subject.dalliance_duration
149
+ subject.dalliance_background_reprocess
150
+ Delayed::Worker.new(:queues => [:dalliance]).work_off
151
+ subject.reload
152
+
153
+ expect(subject.dalliance_duration).to be_between(original_duration, Float::INFINITY)
154
+ end
155
+
156
+ it "resets the dalliance_status to 'pending'" do
157
+ subject.update_column(:dalliance_status, 'processing_error')
158
+
159
+ Resque::Stat.clear(:processed)
160
+ Resque::Stat.clear(:failed)
161
+
162
+ expect { subject.dalliance_background_reprocess }
163
+ .to change(subject, :dalliance_status)
164
+ .to('pending')
165
+
166
+ Resque::Worker.new(:dalliance).process
167
+
168
+ expect(subject).to be_successful
169
+ end
170
+ end
171
+
172
+ context 'cancelling' do
173
+ before(:all) do
174
+ DallianceModel.dalliance_options[:dalliance_method] = :dalliance_success_method
175
+ DallianceModel.dalliance_options[:worker_class] = Dalliance::Workers::Resque
176
+ DallianceModel.dalliance_options[:queue] = 'dalliance'
177
+ end
178
+
179
+ it 'can be cancelled after being queued' do
180
+ subject.dalliance_background_process
181
+ expect { subject.request_cancel_dalliance! and subject.cancelled_dalliance! }
182
+ .to change { subject.dalliance_status }
183
+ .from('pending')
184
+ .to('cancelled')
185
+ end
186
+
187
+ it 'dequeues the job' do
188
+ expect { subject.dalliance_background_process }
189
+ .to change { Resque.size('dalliance') }
190
+ .from(0)
191
+ .to(1)
192
+
193
+ expect { subject.cancel_and_dequeue_dalliance! }
194
+ .to change { Resque.size('dalliance') }
195
+ .from(1)
196
+ .to(0)
197
+ .and change { subject.dalliance_status }
198
+ .from('pending')
199
+ .to('cancelled')
200
+ end
201
+
202
+ it 'sets dalliance_status to "cancelled" if cancellation was requested' do
203
+ subject.dalliance_background_process
204
+ subject.request_cancel_dalliance!
205
+
206
+ Resque::Worker.new(:dalliance).process
207
+ subject.reload
208
+
209
+ expect(subject.dalliance_status).to eq 'cancelled'
210
+ end
211
+
212
+ it 'runs normally if the job does not honor the cancellation request' do
213
+ DallianceModel.dalliance_options[:dalliance_method] = :dalliance_ignore_cancellation_method
214
+
215
+ subject.dalliance_background_process
216
+
217
+ Resque::Worker.new(:dalliance).process
218
+ subject.reload
219
+
220
+ expect(subject.successful).to eq true
221
+ expect(subject.dalliance_status).to eq 'completed'
222
+ end
223
+
224
+ it 'does not process' do
225
+ subject.request_cancel_dalliance!
226
+ subject.dalliance_background_process
227
+
228
+ Resque::Worker.new(:dalliance).process
229
+ subject.reload
230
+
231
+ expect(subject.successful).to eq false
232
+ end
233
+ end
234
+
117
235
  context "raise error" do
118
236
  before(:all) do
119
237
  DallianceModel.dalliance_options[:dalliance_method] = :dalliance_error_method
@@ -9,19 +9,21 @@ RSpec.describe 'Dalliance' do
9
9
 
10
10
  context "self#dalliance_status_in_load_select_array" do
11
11
  it "should return [state, human_name]" do
12
- expect(DallianceModel.dalliance_status_in_load_select_array).to eq([
12
+ expect(DallianceModel.dalliance_status_in_load_select_array).to contain_exactly(
13
13
  ["Completed", "completed"],
14
14
  ["Pending", "pending"],
15
15
  ["Processing", "processing"],
16
16
  ["Processing Error", "processing_error"],
17
- ["Validation Error", "validation_error"]
18
- ])
17
+ ["Validation Error", "validation_error"],
18
+ ["Cancellation Requested", "cancel_requested"],
19
+ ['Cancelled', 'cancelled']
20
+ )
19
21
  end
20
22
  end
21
23
 
22
24
  context "human_attribute_name" do
23
25
  it "should display the correct locale" do
24
- expect(DallianceModel.human_attribute_name(:dalliance_status)).to eq ('Status')
26
+ expect(DallianceModel.human_attribute_name(:dalliance_status)).to eq('Status')
25
27
  end
26
28
  end
27
29
 
@@ -44,7 +46,7 @@ RSpec.describe 'Dalliance' do
44
46
  end
45
47
 
46
48
  context "w/ args" do
47
- let(:queue) { Proc.new{ |a,b,c| 'dalliance_2' } }
49
+ let(:queue) { Proc.new{ |_a,_b,_c| 'dalliance_2' } }
48
50
 
49
51
  specify{ expect(subject.processing_queue).to eq(queue.call) }
50
52
  end
@@ -35,6 +35,61 @@ RSpec.describe DallianceModel do
35
35
  end
36
36
  end
37
37
 
38
+ context 'reprocess' do
39
+ context 'without having already processed' do
40
+ it 'raises an error' do
41
+ expect { subject.dalliance_background_reprocess }
42
+ .to raise_error(
43
+ StateMachine::InvalidTransition,
44
+ /^Cannot transition dalliance_status via :reprocess_dalliance from :pending.*/
45
+ )
46
+ end
47
+ end
48
+
49
+ context 'when the model has already processed' do
50
+ before do
51
+ DallianceModel.dalliance_options[:dalliance_method] = :dalliance_success_method
52
+ subject.dalliance_background_process
53
+ subject.reload
54
+ end
55
+
56
+ it 'calls the dalliance_reprocess method' do
57
+ expect { subject.dalliance_background_reprocess }
58
+ .to change(subject, :reprocessed_count)
59
+ .from(0)
60
+ .to(1)
61
+ end
62
+
63
+ it 'can call the dalliance_reprocess method many times in succession' do
64
+ expect { 10.times { subject.dalliance_background_reprocess } }
65
+ .to change(subject, :reprocessed_count)
66
+ .from(0)
67
+ .to(10)
68
+ end
69
+
70
+ it 'sets the dalliance_status to completed' do
71
+ expect { subject.dalliance_background_reprocess }
72
+ .not_to change { subject.reload.dalliance_status }
73
+ .from('completed')
74
+ end
75
+
76
+ it 'sets the dalliance_progress to 100' do
77
+ expect { subject.dalliance_background_reprocess }
78
+ .not_to change { subject.reload.dalliance_progress }
79
+ .from(100)
80
+ end
81
+
82
+ it 'increases the total processing time counter' do
83
+ original_duration = subject.dalliance_duration
84
+ subject.dalliance_background_reprocess
85
+ Delayed::Worker.new(:queues => [:dalliance]).work_off
86
+ subject.reload
87
+
88
+ expect(subject.dalliance_duration).to be_between(original_duration, Float::INFINITY)
89
+ end
90
+ end
91
+ end
92
+
38
93
  context "raise error" do
39
94
  before(:all) do
40
95
  DallianceModel.dalliance_options[:dalliance_method] = :dalliance_error_method
@@ -121,17 +176,19 @@ RSpec.describe DallianceModel do
121
176
  end
122
177
  end
123
178
 
124
- context "destroy" do
179
+ context "destroy" do
125
180
  it "should return false when pending?" do
126
181
  subject.update_column(:dalliance_status, 'pending')
127
182
  expect(subject.destroy).to be_falsey
128
- expect(subject.errors[:dalliance_status]).to eq(['is invalid'])
183
+ expect(subject.errors[:dalliance_status])
184
+ .to eq(["Processing must be finished or cancelled, but status is 'pending'"])
129
185
  end
130
186
 
131
187
  it "should return false when processing?" do
132
188
  subject.update_column(:dalliance_status, 'processing')
133
189
  expect(subject.destroy).to be_falsey
134
- expect(subject.errors[:dalliance_status]).to eq(['is invalid'])
190
+ expect(subject.errors[:dalliance_status])
191
+ .to eq(["Processing must be finished or cancelled, but status is 'processing'"])
135
192
  end
136
193
 
137
194
  it "should return true when validation_error?" do
data/spec/spec_helper.rb CHANGED
@@ -4,10 +4,12 @@ require 'bundler/setup'
4
4
  #Automatically included in a rails app...
5
5
  require 'active_support'
6
6
 
7
+ # rubocop:disable Lint/SuppressedException
7
8
  begin
8
9
  require 'active_job'
9
10
  rescue LoadError
10
11
  end
12
+ # rubocop:enable Lint/SuppressedException
11
13
 
12
14
  require 'state_machine'
13
15
  require 'byebug'
@@ -35,9 +35,10 @@ ActiveRecord::Schema.define do
35
35
  create_table :dalliance_models, :force => true do |t|
36
36
  t.text :dalliance_error_hash
37
37
  t.string :dalliance_status, :string, :null => false, :default => 'pending'
38
- t.integer :dalliance_duration
38
+ t.decimal :dalliance_duration
39
39
 
40
40
  t.boolean :successful, :default => false
41
+ t.integer :reprocessed_count, default: 0
41
42
  end
42
43
  end
43
44
 
@@ -47,12 +48,24 @@ class DallianceModel < ActiveRecord::Base
47
48
  include Dalliance::Glue
48
49
 
49
50
  dalliance :dalliance_success_method,
51
+ reprocess_method: :dalliance_reprocess_method,
50
52
  :logger => nil
51
53
 
52
54
  def dalliance_success_method
53
55
  update_attribute(:successful, true)
54
56
  end
55
57
 
58
+ def dalliance_reprocess_method
59
+ update_attribute(:reprocessed_count, self.reprocessed_count + 1)
60
+ end
61
+
62
+ # Pretends that an external action requested processing to be cancelled, but
63
+ # ignores the request and finishes anyway.
64
+ def dalliance_ignore_cancellation_method
65
+ request_cancel_dalliance!
66
+ update_attribute(:successful, true)
67
+ end
68
+
56
69
  def dalliance_error_method
57
70
  raise RuntimeError
58
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalliance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Sullivan
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-21 00:00:00.000000000 Z
11
+ date: 2021-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.3'
22
+ version: '6.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '4.2'
29
+ version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.3'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: state_machine
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +114,48 @@ dependencies:
114
114
  - - ">="
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: bundler-audit
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rubocop
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '0.78'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '0.78'
145
+ - !ruby/object:Gem::Dependency
146
+ name: rspec_junit_formatter
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
117
159
  description: " Background processing for ActiveRecord using a 'delayable' worker and
118
160
  a state_machine "
119
161
  email:
@@ -122,6 +164,7 @@ executables: []
122
164
  extensions: []
123
165
  extra_rdoc_files: []
124
166
  files:
167
+ - ".circleci/config.yml"
125
168
  - ".gitignore"
126
169
  - ".rspec"
127
170
  - Appraisals
@@ -131,14 +174,16 @@ files:
131
174
  - Rakefile
132
175
  - config/locales/en.yml
133
176
  - dalliance.gemspec
134
- - gemfiles/rails_4.2.gemfile
135
- - gemfiles/rails_4.2.gemfile.lock
136
177
  - gemfiles/rails_5.0.gemfile
137
178
  - gemfiles/rails_5.0.gemfile.lock
138
179
  - gemfiles/rails_5.1.gemfile
139
180
  - gemfiles/rails_5.1.gemfile.lock
140
181
  - gemfiles/rails_5.2.gemfile
141
182
  - gemfiles/rails_5.2.gemfile.lock
183
+ - gemfiles/rails_6.0.gemfile
184
+ - gemfiles/rails_6.0.gemfile.lock
185
+ - gemfiles/rails_6.1.gemfile
186
+ - gemfiles/rails_6.1.gemfile.lock
142
187
  - lib/dalliance.rb
143
188
  - lib/dalliance/engine.rb
144
189
  - lib/dalliance/progress_meter.rb
@@ -161,7 +206,7 @@ files:
161
206
  homepage: https://github.com/annkissam/dalliance
162
207
  licenses: []
163
208
  metadata: {}
164
- post_install_message:
209
+ post_install_message:
165
210
  rdoc_options: []
166
211
  require_paths:
167
212
  - lib
@@ -176,9 +221,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
221
  - !ruby/object:Gem::Version
177
222
  version: '0'
178
223
  requirements: []
179
- rubyforge_project: dalliance
224
+ rubyforge_project:
180
225
  rubygems_version: 2.7.6
181
- signing_key:
226
+ signing_key:
182
227
  specification_version: 4
183
228
  summary: Wrapper for an ActiveRecord model with a single ascynhronous method
184
229
  test_files:
@@ -1,165 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- dalliance (0.5.1)
5
- rails (>= 3.2, < 5.3)
6
- state_machine
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- actionmailer (4.2.9)
12
- actionpack (= 4.2.9)
13
- actionview (= 4.2.9)
14
- activejob (= 4.2.9)
15
- mail (~> 2.5, >= 2.5.4)
16
- rails-dom-testing (~> 1.0, >= 1.0.5)
17
- actionpack (4.2.9)
18
- actionview (= 4.2.9)
19
- activesupport (= 4.2.9)
20
- rack (~> 1.6)
21
- rack-test (~> 0.6.2)
22
- rails-dom-testing (~> 1.0, >= 1.0.5)
23
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
- actionview (4.2.9)
25
- activesupport (= 4.2.9)
26
- builder (~> 3.1)
27
- erubis (~> 2.7.0)
28
- rails-dom-testing (~> 1.0, >= 1.0.5)
29
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
30
- activejob (4.2.9)
31
- activesupport (= 4.2.9)
32
- globalid (>= 0.3.0)
33
- activemodel (4.2.9)
34
- activesupport (= 4.2.9)
35
- builder (~> 3.1)
36
- activerecord (4.2.9)
37
- activemodel (= 4.2.9)
38
- activesupport (= 4.2.9)
39
- arel (~> 6.0)
40
- activesupport (4.2.9)
41
- i18n (~> 0.7)
42
- minitest (~> 5.1)
43
- thread_safe (~> 0.3, >= 0.3.4)
44
- tzinfo (~> 1.1)
45
- appraisal (2.2.0)
46
- bundler
47
- rake
48
- thor (>= 0.14.0)
49
- arel (6.0.4)
50
- builder (3.2.3)
51
- byebug (10.0.2)
52
- concurrent-ruby (1.1.5)
53
- crass (1.0.4)
54
- delayed_job (4.1.5)
55
- activesupport (>= 3.0, < 5.3)
56
- delayed_job_active_record (4.1.3)
57
- activerecord (>= 3.0, < 5.3)
58
- delayed_job (>= 3.0, < 5)
59
- diff-lcs (1.3)
60
- erubis (2.7.0)
61
- globalid (0.4.2)
62
- activesupport (>= 4.2.0)
63
- i18n (0.9.5)
64
- concurrent-ruby (~> 1.0)
65
- loofah (2.2.3)
66
- crass (~> 1.0.2)
67
- nokogiri (>= 1.5.9)
68
- mail (2.7.1)
69
- mini_mime (>= 0.1.1)
70
- mini_mime (1.0.1)
71
- mini_portile2 (2.4.0)
72
- minitest (5.11.3)
73
- mono_logger (1.1.0)
74
- multi_json (1.13.1)
75
- nokogiri (1.10.3)
76
- mini_portile2 (~> 2.4.0)
77
- rack (1.6.8)
78
- rack-protection (1.5.3)
79
- rack
80
- rack-test (0.6.3)
81
- rack (>= 1.0)
82
- rails (4.2.9)
83
- actionmailer (= 4.2.9)
84
- actionpack (= 4.2.9)
85
- actionview (= 4.2.9)
86
- activejob (= 4.2.9)
87
- activemodel (= 4.2.9)
88
- activerecord (= 4.2.9)
89
- activesupport (= 4.2.9)
90
- bundler (>= 1.3.0, < 2.0)
91
- railties (= 4.2.9)
92
- sprockets-rails
93
- rails-deprecated_sanitizer (1.0.3)
94
- activesupport (>= 4.2.0.alpha)
95
- rails-dom-testing (1.0.9)
96
- activesupport (>= 4.2.0, < 5.0)
97
- nokogiri (~> 1.6)
98
- rails-deprecated_sanitizer (>= 1.0.1)
99
- rails-html-sanitizer (1.0.4)
100
- loofah (~> 2.2, >= 2.2.2)
101
- railties (4.2.9)
102
- actionpack (= 4.2.9)
103
- activesupport (= 4.2.9)
104
- rake (>= 0.8.7)
105
- thor (>= 0.18.1, < 2.0)
106
- rake (12.3.2)
107
- redis (4.1.0)
108
- redis-namespace (1.6.0)
109
- redis (>= 3.0.4)
110
- resque (2.0.0)
111
- mono_logger (~> 1.0)
112
- multi_json (~> 1.0)
113
- redis-namespace (~> 1.6)
114
- sinatra (>= 0.9.2)
115
- vegas (~> 0.1.2)
116
- rspec (3.8.0)
117
- rspec-core (~> 3.8.0)
118
- rspec-expectations (~> 3.8.0)
119
- rspec-mocks (~> 3.8.0)
120
- rspec-core (3.8.0)
121
- rspec-support (~> 3.8.0)
122
- rspec-expectations (3.8.2)
123
- diff-lcs (>= 1.2.0, < 2.0)
124
- rspec-support (~> 3.8.0)
125
- rspec-mocks (3.8.0)
126
- diff-lcs (>= 1.2.0, < 2.0)
127
- rspec-support (~> 3.8.0)
128
- rspec-support (3.8.0)
129
- sinatra (1.4.8)
130
- rack (~> 1.5)
131
- rack-protection (~> 1.4)
132
- tilt (>= 1.3, < 3)
133
- sprockets (3.7.2)
134
- concurrent-ruby (~> 1.0)
135
- rack (> 1, < 3)
136
- sprockets-rails (3.2.1)
137
- actionpack (>= 4.0)
138
- activesupport (>= 4.0)
139
- sprockets (>= 3.0.0)
140
- sqlite3 (1.3.13)
141
- state_machine (1.2.0)
142
- thor (0.20.3)
143
- thread_safe (0.3.6)
144
- tilt (2.0.9)
145
- tzinfo (1.2.5)
146
- thread_safe (~> 0.1)
147
- vegas (0.1.11)
148
- rack (>= 1.0.0)
149
-
150
- PLATFORMS
151
- ruby
152
-
153
- DEPENDENCIES
154
- appraisal
155
- byebug
156
- dalliance!
157
- delayed_job (>= 3.0.0)
158
- delayed_job_active_record
159
- rails (~> 4.2.0)
160
- resque
161
- rspec (>= 3.0.0)
162
- sqlite3
163
-
164
- BUNDLED WITH
165
- 1.17.3