acidic_job 0.5.0 → 0.5.4
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/bin/console +2 -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 +51 -29
- data/lib/generators/templates/create_acidic_job_keys_migration.rb.erb +1 -0
- data/lib/generators/templates/create_staged_acidic_jobs_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: 64a311e997f51bcd3ddf92571120c63976f32c35c9da22d3acd1e844ca4b152e
|
4
|
+
data.tar.gz: 68482f3ef456a7e677bfdd3bd9da93ccec5cf3029a9b21a78820cf0d1f210249
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c15a9b5155db86ea3fc43323bcb2ed37e82a2fff199077861a1c7698c998fb4ccd08b7ba70f3f9a190f1d2f5e14430caf880ce0a3d468d83f20941c3d9b41f8e
|
7
|
+
data.tar.gz: 8248faf77812deb4a073c4215d6796ac60932e97c0fc3efe36ce8a11eb79b5e06a41f7a7dab13c9759ffc79d296ad6d82a85340a59f388a7d19b966b5a6d631a
|
data/Gemfile.lock
CHANGED
data/bin/console
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
|
4
4
|
require "bundler/setup"
|
5
5
|
require "acidic_job"
|
6
|
-
require_relative "../test/setup"
|
6
|
+
require_relative "../test/support/setup"
|
7
|
+
require_relative "../test/support/ride_create_job"
|
7
8
|
|
8
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
9
10
|
# with your gem easier. You can also use a different console, if you like.
|
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
@@ -15,15 +15,26 @@ require "active_support/concern"
|
|
15
15
|
module AcidicJob
|
16
16
|
extend ActiveSupport::Concern
|
17
17
|
|
18
|
-
|
19
|
-
attr_reader :key
|
20
|
-
attr_accessor :arguments_for_perform
|
18
|
+
def self.wire_everything_up(klass)
|
19
|
+
klass.attr_reader :key
|
20
|
+
klass.attr_accessor :arguments_for_perform
|
21
21
|
|
22
22
|
# Extend ActiveJob with `perform_transactionally` class method
|
23
|
-
include PerformTransactionallyExtension
|
23
|
+
klass.include PerformTransactionallyExtension
|
24
24
|
|
25
25
|
# Ensure our `perform` method always runs first to gather parameters
|
26
|
-
prepend PerformWrapper
|
26
|
+
klass.prepend PerformWrapper
|
27
|
+
end
|
28
|
+
|
29
|
+
included do
|
30
|
+
AcidicJob.wire_everything_up(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
class_methods do
|
34
|
+
def inherited(subclass)
|
35
|
+
AcidicJob.wire_everything_up(subclass)
|
36
|
+
super
|
37
|
+
end
|
27
38
|
end
|
28
39
|
|
29
40
|
# Number of seconds passed which we consider a held idempotency key lock to be
|
@@ -34,10 +45,6 @@ module AcidicJob
|
|
34
45
|
|
35
46
|
# takes a block
|
36
47
|
def idempotently(with:)
|
37
|
-
# set accessors for each argument passed in to ensure they are available
|
38
|
-
# to the step methods the job will have written
|
39
|
-
define_accessors_for_passed_arguments(with)
|
40
|
-
|
41
48
|
# execute the block to gather the info on what phases are defined for this job
|
42
49
|
defined_steps = yield
|
43
50
|
# [:create_ride_and_audit_record, :create_stripe_charge, :send_receipt]
|
@@ -58,8 +65,12 @@ module AcidicJob
|
|
58
65
|
# if the key record is already marked as finished, immediately return its result
|
59
66
|
return @key.succeeded? if @key.finished?
|
60
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
|
+
|
61
72
|
# otherwise, we will enter a loop to process each required step of the job
|
62
|
-
|
73
|
+
phases.size.times do
|
63
74
|
# our `phases` hash uses Symbols for keys
|
64
75
|
recovery_point = @key.recovery_point.to_sym
|
65
76
|
|
@@ -105,16 +116,16 @@ module AcidicJob
|
|
105
116
|
rescued_error = e
|
106
117
|
raise e
|
107
118
|
ensure
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
118
129
|
end
|
119
130
|
end
|
120
131
|
end
|
@@ -133,15 +144,11 @@ module AcidicJob
|
|
133
144
|
if @key
|
134
145
|
# Programs enqueuing multiple jobs with different parameters but the
|
135
146
|
# same idempotency key is a bug.
|
136
|
-
if @key.job_args != @arguments_for_perform
|
137
|
-
raise MismatchedIdempotencyKeyAndJobArguments
|
138
|
-
end
|
147
|
+
raise MismatchedIdempotencyKeyAndJobArguments if @key.job_args != @arguments_for_perform
|
139
148
|
|
140
149
|
# Only acquire a lock if the key is unlocked or its lock has expired
|
141
150
|
# because the original job was long enough ago.
|
142
|
-
if @key.locked_at && @key.locked_at > Time.current - IDEMPOTENCY_KEY_LOCK_TIMEOUT
|
143
|
-
raise LockedIdempotencyKey
|
144
|
-
end
|
151
|
+
raise LockedIdempotencyKey if @key.locked_at && @key.locked_at > Time.current - IDEMPOTENCY_KEY_LOCK_TIMEOUT
|
145
152
|
|
146
153
|
# Lock the key and update latest run unless the job is already finished.
|
147
154
|
@key.update!(last_run_at: Time.current, locked_at: Time.current) unless @key.finished?
|
@@ -158,12 +165,27 @@ module AcidicJob
|
|
158
165
|
end
|
159
166
|
end
|
160
167
|
|
161
|
-
def define_accessors_for_passed_arguments(passed_arguments)
|
162
|
-
|
168
|
+
def define_accessors_for_passed_arguments(passed_arguments, key)
|
169
|
+
# first, get the current state of all accessors for both previously persisted and initialized values
|
170
|
+
current_accessors = passed_arguments.stringify_keys.merge(key.attr_accessors)
|
171
|
+
|
172
|
+
# next, ensure that `Key#attr_accessors` is populated with initial values
|
173
|
+
key.update_column(:attr_accessors, current_accessors)
|
174
|
+
|
175
|
+
current_accessors.each do |accessor, value|
|
163
176
|
# the reader method may already be defined
|
164
177
|
self.class.attr_reader accessor unless respond_to?(accessor)
|
165
178
|
# but we should always update the value to match the current value
|
166
179
|
instance_variable_set("@#{accessor}", value)
|
180
|
+
# and we overwrite the setter to ensure any updates to an accessor update the `Key` stored value
|
181
|
+
# Note: we must define the singleton method on the instance to avoid overwriting setters on other
|
182
|
+
# instances of the same class
|
183
|
+
define_singleton_method("#{accessor}=") do |current_value|
|
184
|
+
instance_variable_set("@#{accessor}", current_value)
|
185
|
+
key.attr_accessors[accessor] = current_value
|
186
|
+
key.save!(validate: false)
|
187
|
+
current_value
|
188
|
+
end
|
167
189
|
end
|
168
190
|
|
169
191
|
true
|
@@ -191,7 +213,7 @@ module AcidicJob
|
|
191
213
|
return job_id if defined?(job_id) && !job_id.nil?
|
192
214
|
return jid if defined?(jid) && !jid.nil?
|
193
215
|
|
194
|
-
require
|
216
|
+
require "securerandom"
|
195
217
|
SecureRandom.hex
|
196
218
|
end
|
197
219
|
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.4
|
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
|