acidic_job 0.5.2 → 0.6.0

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: 7b6e76d892b0953452781a38155801969acf50d81897fb78645b561280620ddc
4
- data.tar.gz: 6640c17792135307d21d20f05b49136512368ff84ed748fb2b1ad98d552fc8fb
3
+ metadata.gz: 739d9a393b9dfcf91c6e941e188cab965a2800cf7a729e70a5d30542c76b1237
4
+ data.tar.gz: 6fb81e32e31e6db7001a11d607f33d3bc7f1cb5ddc147d48f8ead84ae15fc7d1
5
5
  SHA512:
6
- metadata.gz: c489d047845c6fcd27482dfe1a3c0cae89a326cd3c81d4816ca9758c7db9e0edd09d954e719eedcc0d8b2a689137e21486482571bd05b7518af795152fc4bc61
7
- data.tar.gz: ddc07f0514be3ad3f8fdec4ffd4bf350a039a4ad95f6b28984f1cb73f29d51ed3511df94eb628f8f336b81d4edae3d9b1c9af3a8b55ab5ac08efc015775c9249
6
+ metadata.gz: bc2d1796950ffb4640d49dc61e05771b2a72f6b6776d96dda4fa35f93720e0687102e08bc8ad39a176d44be828528048df03c4492cbd0b1d121865ef929e5b8f
7
+ data.tar.gz: 32db6d94ca8a8a70d48a7afa863f85f50ebdf05320c9461b1f8c647d4681e697d7d5bc814864fb8846b516482db7057e8659598387f661e43764115f1525e840
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acidic_job (0.5.2)
4
+ acidic_job (0.6.0)
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
- 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
12
+ attributes = if defined?(ActiveJob) && self < ActiveJob::Base
13
+ {
14
+ adapter: "activejob",
15
+ job_name: name,
16
+ job_args: job_or_instantiate(*args).serialize
17
+ }
18
+ elsif defined?(Sidekiq) && 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.2"
4
+ VERSION = "0.6.0"
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,6 +65,11 @@ 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
+ # THIS HAPPENS OUTSIDE OF ANY TRANSACTION
71
+ define_accessors_for_passed_arguments(with, @key)
72
+
71
73
  # otherwise, we will enter a loop to process each required step of the job
72
74
  phases.size.times do
73
75
  # our `phases` hash uses Symbols for keys
@@ -115,16 +117,16 @@ module AcidicJob
115
117
  rescued_error = e
116
118
  raise e
117
119
  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}."
120
+ if rescued_error
121
+ # If we're leaving under an error condition, try to unlock the idempotency
122
+ # key right away so that another request can try again.3
123
+ begin
124
+ key.update_columns(locked_at: nil, error_object: rescued_error)
125
+ rescue StandardError => e
126
+ # We're already inside an error condition, so swallow any additional
127
+ # errors from here and just send them to logs.
128
+ puts "Failed to unlock key #{key.id} because of #{e}."
129
+ end
128
130
  end
129
131
  end
130
132
  end
@@ -140,18 +142,14 @@ module AcidicJob
140
142
  ActiveRecord::Base.transaction(isolation: isolation_level) do
141
143
  @key = Key.find_by(idempotency_key: key_val)
142
144
 
143
- if @key
145
+ if @key.present?
144
146
  # Programs enqueuing multiple jobs with different parameters but the
145
147
  # same idempotency key is a bug.
146
- if @key.job_args != @arguments_for_perform
147
- raise MismatchedIdempotencyKeyAndJobArguments
148
- end
148
+ raise MismatchedIdempotencyKeyAndJobArguments if @key.job_args != @arguments_for_perform
149
149
 
150
150
  # Only acquire a lock if the key is unlocked or its lock has expired
151
151
  # 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
152
+ raise LockedIdempotencyKey if @key.locked_at && @key.locked_at > Time.current - IDEMPOTENCY_KEY_LOCK_TIMEOUT
155
153
 
156
154
  # Lock the key and update latest run unless the job is already finished.
157
155
  @key.update!(last_run_at: Time.current, locked_at: Time.current) unless @key.finished?
@@ -168,12 +166,27 @@ module AcidicJob
168
166
  end
169
167
  end
170
168
 
171
- def define_accessors_for_passed_arguments(passed_arguments)
172
- passed_arguments.each do |accessor, value|
169
+ def define_accessors_for_passed_arguments(passed_arguments, key)
170
+ # first, get the current state of all accessors for both previously persisted and initialized values
171
+ current_accessors = passed_arguments.stringify_keys.merge(key.attr_accessors)
172
+
173
+ # next, ensure that `Key#attr_accessors` is populated with initial values
174
+ key.update_column(:attr_accessors, current_accessors)
175
+
176
+ current_accessors.each do |accessor, value|
173
177
  # the reader method may already be defined
174
178
  self.class.attr_reader accessor unless respond_to?(accessor)
175
179
  # but we should always update the value to match the current value
176
180
  instance_variable_set("@#{accessor}", value)
181
+ # and we overwrite the setter to ensure any updates to an accessor update the `Key` stored value
182
+ # Note: we must define the singleton method on the instance to avoid overwriting setters on other
183
+ # instances of the same class
184
+ define_singleton_method("#{accessor}=") do |current_value|
185
+ instance_variable_set("@#{accessor}", current_value)
186
+ key.attr_accessors[accessor] = current_value
187
+ key.save!(validate: false)
188
+ current_value
189
+ end
177
190
  end
178
191
 
179
192
  true
@@ -185,9 +198,11 @@ module AcidicJob
185
198
  {}.tap do |phases|
186
199
  defined_steps.each_cons(2).map do |enter_method, exit_method|
187
200
  phases[enter_method] = lambda do
188
- method(enter_method).call
201
+ result = method(enter_method).call
189
202
 
190
- if exit_method.to_s == Key::RECOVERY_POINT_FINISHED
203
+ if result.is_a?(Response)
204
+ result
205
+ elsif exit_method.to_s == Key::RECOVERY_POINT_FINISHED
191
206
  Response.new
192
207
  else
193
208
  RecoveryPoint.new(exit_method)
@@ -201,8 +216,7 @@ module AcidicJob
201
216
  return job_id if defined?(job_id) && !job_id.nil?
202
217
  return jid if defined?(jid) && !jid.nil?
203
218
 
204
- require 'securerandom'
205
- SecureRandom.hex
219
+ Digest::SHA1.hexdigest [self.class.name, arguments_for_perform].flatten.join
206
220
  end
207
221
  end
208
222
  # rubocop:enable Metrics/ModuleLength, Metrics/AbcSize, Metrics/MethodLength
@@ -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.2
4
+ version: 0.6.0
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-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord