acidic_job 0.5.2 → 0.5.3
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/lib/acidic_job/errors.rb +2 -0
- data/lib/acidic_job/key.rb +1 -0
- data/lib/acidic_job/perform_transactionally_extension.rb +16 -14
- data/lib/acidic_job/perform_wrapper.rb +8 -8
- data/lib/acidic_job/staged.rb +3 -2
- data/lib/acidic_job/version.rb +1 -1
- data/lib/acidic_job.rb +31 -22
- data/lib/generators/templates/create_acidic_job_keys_migration.rb.erb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4041a344bd53943f281696cafdd0b0a7fa05969b33ac849475f39efd9333018
|
4
|
+
data.tar.gz: 73a7b1175df2b8e8b7ce6e89cbc13bce208e575b662a31ea03f11ed66fbd0961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb042126f12c372d0d3767d785d095aa68b48bb71faa10866fa7de6216b47ff7e885ef55611b10cd1d65f9d53aff515a7723fc429ee3095dc3b6ac749e0f761d
|
7
|
+
data.tar.gz: 8cbf5b168781b8ea1125382b998492e6f6d38cf1b46dc8c766b432e93f19e786547dc5dfc8f5437b5623d33970d67d090857abdcd58a5eb5e80804fc682efd36
|
data/Gemfile.lock
CHANGED
data/lib/acidic_job/errors.rb
CHANGED
data/lib/acidic_job/key.rb
CHANGED
@@ -7,25 +7,27 @@ module AcidicJob
|
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
9
|
class_methods do
|
10
|
+
# rubocop:disable Metrics/MethodLength
|
10
11
|
def perform_transactionally(*args)
|
11
12
|
attributes = if self < ActiveJob::Base
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
13
|
+
{
|
14
|
+
adapter: "activejob",
|
15
|
+
job_name: name,
|
16
|
+
job_args: job_or_instantiate(*args).serialize
|
17
|
+
}
|
18
|
+
elsif include? Sidekiq::Worker
|
19
|
+
{
|
20
|
+
adapter: "sidekiq",
|
21
|
+
job_name: name,
|
22
|
+
job_args: args
|
23
|
+
}
|
24
|
+
else
|
25
|
+
raise UnknownJobAdapter
|
26
|
+
end
|
26
27
|
|
27
28
|
AcidicJob::Staged.create!(attributes)
|
28
29
|
end
|
30
|
+
# rubocop:enable Metrics/MethodLength
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
@@ -6,14 +6,14 @@ module AcidicJob
|
|
6
6
|
# store arguments passed into `perform` so that we can later persist
|
7
7
|
# them to `AcidicJob::Key#job_args` for both ActiveJob and Sidekiq::Worker
|
8
8
|
@arguments_for_perform = if args.any? && kwargs.any?
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
args + [kwargs]
|
10
|
+
elsif args.any? && kwargs.none?
|
11
|
+
args
|
12
|
+
elsif args.none? && kwargs.any?
|
13
|
+
[kwargs]
|
14
|
+
else
|
15
|
+
[]
|
16
|
+
end
|
17
17
|
|
18
18
|
super
|
19
19
|
end
|
data/lib/acidic_job/staged.rb
CHANGED
@@ -15,10 +15,11 @@ module AcidicJob
|
|
15
15
|
after_create_commit :enqueue_job
|
16
16
|
|
17
17
|
def enqueue_job
|
18
|
-
|
18
|
+
case adapter
|
19
|
+
when "activejob"
|
19
20
|
job = ActiveJob::Base.deserialize(job_args)
|
20
21
|
job.enqueue
|
21
|
-
|
22
|
+
when "sidekiq"
|
22
23
|
Sidekiq::Client.push("class" => job_name, "args" => job_args)
|
23
24
|
else
|
24
25
|
raise UnknownJobAdapter.new(adapter: adapter)
|
data/lib/acidic_job/version.rb
CHANGED
data/lib/acidic_job.rb
CHANGED
@@ -33,6 +33,7 @@ module AcidicJob
|
|
33
33
|
class_methods do
|
34
34
|
def inherited(subclass)
|
35
35
|
AcidicJob.wire_everything_up(subclass)
|
36
|
+
super
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
@@ -44,10 +45,6 @@ module AcidicJob
|
|
44
45
|
|
45
46
|
# takes a block
|
46
47
|
def idempotently(with:)
|
47
|
-
# set accessors for each argument passed in to ensure they are available
|
48
|
-
# to the step methods the job will have written
|
49
|
-
define_accessors_for_passed_arguments(with)
|
50
|
-
|
51
48
|
# execute the block to gather the info on what phases are defined for this job
|
52
49
|
defined_steps = yield
|
53
50
|
# [:create_ride_and_audit_record, :create_stripe_charge, :send_receipt]
|
@@ -68,6 +65,10 @@ module AcidicJob
|
|
68
65
|
# if the key record is already marked as finished, immediately return its result
|
69
66
|
return @key.succeeded? if @key.finished?
|
70
67
|
|
68
|
+
# set accessors for each argument passed in to ensure they are available
|
69
|
+
# to the step methods the job will have written
|
70
|
+
define_accessors_for_passed_arguments(with, @key)
|
71
|
+
|
71
72
|
# otherwise, we will enter a loop to process each required step of the job
|
72
73
|
phases.size.times do
|
73
74
|
# our `phases` hash uses Symbols for keys
|
@@ -115,16 +116,16 @@ module AcidicJob
|
|
115
116
|
rescued_error = e
|
116
117
|
raise e
|
117
118
|
ensure
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
119
|
+
if rescued_error
|
120
|
+
# If we're leaving under an error condition, try to unlock the idempotency
|
121
|
+
# key right away so that another request can try again.3
|
122
|
+
begin
|
123
|
+
key.update_columns(locked_at: nil, error_object: rescued_error)
|
124
|
+
rescue StandardError => e
|
125
|
+
# We're already inside an error condition, so swallow any additional
|
126
|
+
# errors from here and just send them to logs.
|
127
|
+
puts "Failed to unlock key #{key.id} because of #{e}."
|
128
|
+
end
|
128
129
|
end
|
129
130
|
end
|
130
131
|
end
|
@@ -143,15 +144,11 @@ module AcidicJob
|
|
143
144
|
if @key
|
144
145
|
# Programs enqueuing multiple jobs with different parameters but the
|
145
146
|
# same idempotency key is a bug.
|
146
|
-
if @key.job_args != @arguments_for_perform
|
147
|
-
raise MismatchedIdempotencyKeyAndJobArguments
|
148
|
-
end
|
147
|
+
raise MismatchedIdempotencyKeyAndJobArguments if @key.job_args != @arguments_for_perform
|
149
148
|
|
150
149
|
# Only acquire a lock if the key is unlocked or its lock has expired
|
151
150
|
# because the original job was long enough ago.
|
152
|
-
if @key.locked_at && @key.locked_at > Time.current - IDEMPOTENCY_KEY_LOCK_TIMEOUT
|
153
|
-
raise LockedIdempotencyKey
|
154
|
-
end
|
151
|
+
raise LockedIdempotencyKey if @key.locked_at && @key.locked_at > Time.current - IDEMPOTENCY_KEY_LOCK_TIMEOUT
|
155
152
|
|
156
153
|
# Lock the key and update latest run unless the job is already finished.
|
157
154
|
@key.update!(last_run_at: Time.current, locked_at: Time.current) unless @key.finished?
|
@@ -168,12 +165,24 @@ module AcidicJob
|
|
168
165
|
end
|
169
166
|
end
|
170
167
|
|
171
|
-
def define_accessors_for_passed_arguments(passed_arguments)
|
168
|
+
def define_accessors_for_passed_arguments(passed_arguments, key)
|
169
|
+
# first, ensure that `Key#attr_accessors` is populated with initial values
|
170
|
+
key.update_column(:attr_accessors, passed_arguments)
|
171
|
+
|
172
172
|
passed_arguments.each do |accessor, value|
|
173
173
|
# the reader method may already be defined
|
174
174
|
self.class.attr_reader accessor unless respond_to?(accessor)
|
175
175
|
# but we should always update the value to match the current value
|
176
176
|
instance_variable_set("@#{accessor}", value)
|
177
|
+
# and we overwrite the setter to ensure any updates to an accessor update the `Key` stored value
|
178
|
+
# Note: we must define the singleton method on the instance to avoid overwriting setters on other
|
179
|
+
# instances of the same class
|
180
|
+
define_singleton_method("#{accessor}=") do |current_value|
|
181
|
+
instance_variable_set("@#{accessor}", current_value)
|
182
|
+
key.attr_accessors[accessor] = current_value
|
183
|
+
key.save!(validate: false)
|
184
|
+
current_value
|
185
|
+
end
|
177
186
|
end
|
178
187
|
|
179
188
|
true
|
@@ -201,7 +210,7 @@ module AcidicJob
|
|
201
210
|
return job_id if defined?(job_id) && !job_id.nil?
|
202
211
|
return jid if defined?(jid) && !jid.nil?
|
203
212
|
|
204
|
-
require
|
213
|
+
require "securerandom"
|
205
214
|
SecureRandom.hex
|
206
215
|
end
|
207
216
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acidic_job
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fractaledmind
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|