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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b6e76d892b0953452781a38155801969acf50d81897fb78645b561280620ddc
4
- data.tar.gz: 6640c17792135307d21d20f05b49136512368ff84ed748fb2b1ad98d552fc8fb
3
+ metadata.gz: e4041a344bd53943f281696cafdd0b0a7fa05969b33ac849475f39efd9333018
4
+ data.tar.gz: 73a7b1175df2b8e8b7ce6e89cbc13bce208e575b662a31ea03f11ed66fbd0961
5
5
  SHA512:
6
- metadata.gz: c489d047845c6fcd27482dfe1a3c0cae89a326cd3c81d4816ca9758c7db9e0edd09d954e719eedcc0d8b2a689137e21486482571bd05b7518af795152fc4bc61
7
- data.tar.gz: ddc07f0514be3ad3f8fdec4ffd4bf350a039a4ad95f6b28984f1cb73f29d51ed3511df94eb628f8f336b81d4edae3d9b1c9af3a8b55ab5ac08efc015775c9249
6
+ metadata.gz: bb042126f12c372d0d3767d785d095aa68b48bb71faa10866fa7de6216b47ff7e885ef55611b10cd1d65f9d53aff515a7723fc429ee3095dc3b6ac749e0f761d
7
+ data.tar.gz: 8cbf5b168781b8ea1125382b998492e6f6d38cf1b46dc8c766b432e93f19e786547dc5dfc8f5437b5623d33970d67d090857abdcd58a5eb5e80804fc682efd36
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.5.3)
5
5
  activerecord (>= 4.0.0)
6
6
  activesupport
7
7
 
@@ -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.2"
4
+ VERSION = "0.5.3"
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,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
- 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,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 'securerandom'
213
+ require "securerandom"
205
214
  SecureRandom.hex
206
215
  end
207
216
  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],
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.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-09-29 00:00:00.000000000 Z
11
+ date: 2021-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord