acidic_job 0.5.1 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84a76328fcff18be9e2302440dc2bc7284764ba94737fbf5044c9d7111902fa9
4
- data.tar.gz: 49f793646313f1d7373913dfbb745ec94e644472031b77eda41b49d622a4290f
3
+ metadata.gz: 79d8997dd3c0922319cbfc2408804a935e302a74c6fc0664811dce1a59bb272d
4
+ data.tar.gz: bfce5d294e9831fff9e40fc8b6aa1cda59eb31262640f2530906b30b21ceea79
5
5
  SHA512:
6
- metadata.gz: f61806e50dcae6c26a22e2cea13205f62f28a768c6642b20417cc1f12a5a6a04880c366cf0530bea68a8d51707caef54be9aefb7ef630e7b41dcff3e252d6369
7
- data.tar.gz: a83e16a9bd2838df358e78d1d6a5eaebd94829e9f56241ec4b99763f92b319918954960a285f9a1475fc5321f6b8cf1fc8c2f38215245563eecd3ef391992c2c
6
+ metadata.gz: c97fed527770260bfd63f1ef3702419c81168e9b2f08da603ed22e328d22f85dc0a745bdc523725317ac3ac77fa4a3005581cd82a7bc85cd573552ba158e473f
7
+ data.tar.gz: 1e5a63b40b7e6b98e10bf9c98db06686b0468fd559ccdd0834309b2a2e5e40c4d9366632806c845e72cb43924fb34059155d67b2fa5bd5717ed5f6c523589082
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acidic_job (0.5.1)
4
+ acidic_job (0.5.5)
5
5
  activerecord (>= 4.0.0)
6
6
  activesupport
7
7
 
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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AcidicJob
2
4
  class Error < StandardError; end
3
5
 
@@ -10,6 +10,7 @@ module AcidicJob
10
10
 
11
11
  serialize :error_object
12
12
  serialize :job_args
13
+ store :attr_accessors
13
14
 
14
15
  validates :idempotency_key, presence: true, uniqueness: { scope: %i[job_name job_args] }
15
16
  validates :job_name, presence: true
@@ -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
- adapter: "activejob",
14
- job_name: self.name,
15
- job_args: job_or_instantiate(*args).serialize
16
- }
17
- elsif self.include? Sidekiq::Worker
18
- {
19
- adapter: "sidekiq",
20
- job_name: self.name,
21
- job_args: args
22
- }
23
- else
24
- raise UnknownJobAdapter
25
- end
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
- args + [kwargs]
10
- elsif args.any? && kwargs.none?
11
- args
12
- elsif args.none? && kwargs.any?
13
- [kwargs]
14
- else
15
- []
16
- end
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
@@ -15,10 +15,11 @@ module AcidicJob
15
15
  after_create_commit :enqueue_job
16
16
 
17
17
  def enqueue_job
18
- if adapter == "activejob"
18
+ case adapter
19
+ when "activejob"
19
20
  job = ActiveJob::Base.deserialize(job_args)
20
21
  job.enqueue
21
- elsif adapter == "sidekiq"
22
+ when "sidekiq"
22
23
  Sidekiq::Client.push("class" => job_name, "args" => job_args)
23
24
  else
24
25
  raise UnknownJobAdapter.new(adapter: adapter)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AcidicJob
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.5"
5
5
  end
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,8 +65,12 @@ 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
- 100.times do
73
+ phases.size.times do
73
74
  # our `phases` hash uses Symbols for keys
74
75
  recovery_point = @key.recovery_point.to_sym
75
76
 
@@ -115,16 +116,16 @@ module AcidicJob
115
116
  rescued_error = e
116
117
  raise e
117
118
  ensure
118
- return unless rescued_error
119
-
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}."
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,27 @@ module AcidicJob
168
165
  end
169
166
  end
170
167
 
171
- def define_accessors_for_passed_arguments(passed_arguments)
172
- passed_arguments.each do |accessor, value|
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|
173
176
  # the reader method may already be defined
174
177
  self.class.attr_reader accessor unless respond_to?(accessor)
175
178
  # but we should always update the value to match the current value
176
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
177
189
  end
178
190
 
179
191
  true
@@ -185,9 +197,11 @@ module AcidicJob
185
197
  {}.tap do |phases|
186
198
  defined_steps.each_cons(2).map do |enter_method, exit_method|
187
199
  phases[enter_method] = lambda do
188
- method(enter_method).call
200
+ result = method(enter_method).call
189
201
 
190
- if exit_method.to_s == Key::RECOVERY_POINT_FINISHED
202
+ if result.is_a?(Response)
203
+ result
204
+ elsif exit_method.to_s == Key::RECOVERY_POINT_FINISHED
191
205
  Response.new
192
206
  else
193
207
  RecoveryPoint.new(exit_method)
@@ -201,7 +215,7 @@ module AcidicJob
201
215
  return job_id if defined?(job_id) && !job_id.nil?
202
216
  return jid if defined?(jid) && !jid.nil?
203
217
 
204
- require 'securerandom'
218
+ require "securerandom"
205
219
  SecureRandom.hex
206
220
  end
207
221
  end
@@ -8,6 +8,7 @@ class CreateAcidicJobKeys < <%= migration_class %>
8
8
  t.datetime :locked_at, null: true
9
9
  t.string :recovery_point, null: false
10
10
  t.text :error_object
11
+ t.text :attr_accessors
11
12
  t.timestamps
12
13
 
13
14
  t.index %i[idempotency_key job_name job_args],
@@ -4,6 +4,7 @@ class CreateStagedAcidicJobs < <%= migration_class %>
4
4
  t.string :adapter, null: false
5
5
  t.string :job_name, null: false
6
6
  t.text :job_args, null: true
7
+ t.timestamps
7
8
  end
8
9
  end
9
10
  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.1
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - fractaledmind
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-29 00:00:00.000000000 Z
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord