acidic_job 1.0.0.pre8 → 1.0.0.pre9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -25
- data/lib/acidic_job/version.rb +1 -1
- data/lib/acidic_job.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3971aa5af368952620c483730c9a8a28efd9e01bffecc38d015d322e165bdffe
|
4
|
+
data.tar.gz: a6c7374d7e0f2121a0fc92b988c28cee29d488f32506dd84e9e84c15b0117eda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a890818e15da5fb4f5990f9753545d2e33c2bfb6d6e2a9793a42efeed34fcb6dbef076b18111980b0bfe32fe58f490f5fad0d70640b8d6624c8ae0210b4dd8a
|
7
|
+
data.tar.gz: b988408714e535184d553982a8220e32c83e7551ea129a7a5c001f1e83462d03d64f86e7df4112fed3af51f1a8a197f38c587fd01a9dc0b4d77ea8051a43aee0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -78,7 +78,7 @@ class RideCreateJob < ActiveJob::Base
|
|
78
78
|
def perform(user_id, ride_params)
|
79
79
|
user = User.find(user_id)
|
80
80
|
|
81
|
-
with_acidity
|
81
|
+
with_acidity providing: { user: user, params: ride_params, ride: nil } do
|
82
82
|
step :create_ride_and_audit_record
|
83
83
|
step :create_stripe_charge
|
84
84
|
step :send_receipt
|
@@ -99,20 +99,20 @@ class RideCreateJob < ActiveJob::Base
|
|
99
99
|
end
|
100
100
|
```
|
101
101
|
|
102
|
-
`with_acidity` takes only the `
|
102
|
+
`with_acidity` takes only the `providing:` named parameter and a block where you define the steps of this operation. `step` simply takes the name of a method available in the job. That's all!
|
103
103
|
|
104
104
|
Now, each execution of this job will find or create an `AcidicJob::Run` record, which we leverage to wrap every step in a database transaction. Moreover, this database record allows `acidic_job` to ensure that if your job fails on step 3, when it retries, it will simply jump right back to trying to execute the method defined for the 3rd step, and won't even execute the first two step methods. This means your step methods only need to be idempotent on failure, not on success, since they will never be run again if they succeed.
|
105
105
|
|
106
106
|
### Persisted Attributes
|
107
107
|
|
108
|
-
Any objects passed to the `
|
108
|
+
Any objects passed to the `providing` option on the `with_acidity` method are not just made available to each of your step methods, they are made available across retries. This means that you can set an attribute in step 1, access it in step 2, have step 2 fail, have the job retry, jump directly back to step 2 on retry, and have that object still accessible. This is done by serializing all objects to a field on the `AcidicJob::Run` and manually providing getters and setters that sync with the database record.
|
109
109
|
|
110
110
|
```ruby
|
111
111
|
class RideCreateJob < ActiveJob::Base
|
112
112
|
include AcidicJob
|
113
113
|
|
114
114
|
def perform(ride_params)
|
115
|
-
with_acidity
|
115
|
+
with_acidity providing: { ride: nil } do
|
116
116
|
step :create_ride_and_audit_record
|
117
117
|
step :create_stripe_charge
|
118
118
|
step :send_receipt
|
@@ -148,7 +148,7 @@ class RideCreateJob < ActiveJob::Base
|
|
148
148
|
def perform(user_id, ride_params)
|
149
149
|
user = User.find(user_id)
|
150
150
|
|
151
|
-
with_acidity
|
151
|
+
with_acidity providing: { user: user, params: ride_params, ride: nil } do
|
152
152
|
step :create_ride_and_audit_record
|
153
153
|
step :create_stripe_charge
|
154
154
|
step :send_receipt
|
@@ -182,7 +182,7 @@ class RideCreateJob < ActiveJob::Base
|
|
182
182
|
def perform(user_id, ride_params)
|
183
183
|
user = User.find(user_id)
|
184
184
|
|
185
|
-
with_acidity
|
185
|
+
with_acidity providing: { user: user, params: ride_params, ride: nil } do
|
186
186
|
step :create_ride_and_audit_record, awaits: [SomeJob]
|
187
187
|
step :create_stripe_charge, args: [1, 2, 3], kwargs: { some: 'thing' }
|
188
188
|
step :send_receipt
|
@@ -203,25 +203,7 @@ This error is thrown because by default RSpec and most MiniTest test suites use
|
|
203
203
|
|
204
204
|
In order to avoid this error, you need to ensure firstly that your tests that run your acidic jobs are not using a database transaction and secondly that they use some different strategy to keep your test database clean. The [DatabaseCleaner](https://github.com/DatabaseCleaner/database_cleaner) gem is a commonly used tool to manage different strategies for keeping your test database clean. As for which strategy to use, `truncation` and `deletion` are both safe, but their speed varies based on our app's table structure (see https://github.com/DatabaseCleaner/database_cleaner#what-strategy-is-fastest). Either is fine; use whichever is faster for your app.
|
205
205
|
|
206
|
-
In order to make this test setup simpler, `AcidicJob` provides a `TestCase` class that your MiniTest jobs tests can inherit from. It is simple; it inherits from `ActiveJob::TestCase`, sets `use_transactional_tests` to `false`, and ensures `DatabaseCleaner` is run for each of your tests
|
207
|
-
|
208
|
-
```ruby
|
209
|
-
class AcidicJob::TestCase < ActiveJob::TestCase
|
210
|
-
self.use_transactional_tests = false
|
211
|
-
|
212
|
-
def before_setup
|
213
|
-
super
|
214
|
-
DatabaseCleaner.start
|
215
|
-
end
|
216
|
-
|
217
|
-
def after_teardown
|
218
|
-
DatabaseCleaner.clean
|
219
|
-
super
|
220
|
-
end
|
221
|
-
|
222
|
-
# ...
|
223
|
-
end
|
224
|
-
```
|
206
|
+
In order to make this test setup simpler, `AcidicJob` provides a `TestCase` class that your MiniTest jobs tests can inherit from. It is simple; it inherits from `ActiveJob::TestCase`, sets `use_transactional_tests` to `false`, and ensures `DatabaseCleaner` is run for each of your tests. Moreover, it ensures that the system's original DatabaseCleaner configuration is maintained, options included, except that any `transaction` strategies for any ORMs are replaced with a `deletion` strategy. It does so by storing whatever the system DatabaseCleaner configuration is at the start of `before_setup` phase in an instance variable and then restores that configuration at the end of `after_teardown` phase. In between, it runs the configuration thru a pipeline that selectively replaces any `transaction` strategies with a corresponding `deletion` strategy, leaving any other configured strategies untouched.
|
225
207
|
|
226
208
|
For those of you using RSpec, you can require the `acidic_job/rspec_configuration` file, which will configure RSpec in the exact same way I have used in my RSpec projects to allow me to test acidic jobs with either the `deletion` strategy but still have all of my other tests use the fast `transaction` strategy:
|
227
209
|
|
data/lib/acidic_job/version.rb
CHANGED
data/lib/acidic_job.rb
CHANGED
@@ -59,7 +59,7 @@ module AcidicJob
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
def with_acidity(
|
62
|
+
def with_acidity(providing: {})
|
63
63
|
# execute the block to gather the info on what steps are defined for this job workflow
|
64
64
|
@__acidic_job_steps = []
|
65
65
|
steps = yield || []
|
@@ -76,7 +76,7 @@ module AcidicJob
|
|
76
76
|
# TODO: allow idempotency to be defined by args OR job id
|
77
77
|
@__acidic_job_idempotency_key ||= IdempotencyKey.value_for(self, @__acidic_job_args, @__acidic_job_kwargs)
|
78
78
|
|
79
|
-
@run = ensure_run_record(@__acidic_job_idempotency_key, workflow,
|
79
|
+
@run = ensure_run_record(@__acidic_job_idempotency_key, workflow, providing)
|
80
80
|
|
81
81
|
# begin the workflow
|
82
82
|
process_run(@run)
|
@@ -84,7 +84,7 @@ module AcidicJob
|
|
84
84
|
|
85
85
|
# DEPRECATED
|
86
86
|
def idempotently(with:, &blk)
|
87
|
-
with_acidity(
|
87
|
+
with_acidity(providing: with, &blk)
|
88
88
|
end
|
89
89
|
|
90
90
|
def safely_finish_acidic_job
|