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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 943a9bc87b07b1e4a2ec7d0a83ffa8a23917124386cfc601780f95d36c62d23a
4
- data.tar.gz: 2f534e9ebd0d533ae4b7572fb1da1451df9a1a2ed68fc231bc4f4de323de9e84
3
+ metadata.gz: 64a311e997f51bcd3ddf92571120c63976f32c35c9da22d3acd1e844ca4b152e
4
+ data.tar.gz: 68482f3ef456a7e677bfdd3bd9da93ccec5cf3029a9b21a78820cf0d1f210249
5
5
  SHA512:
6
- metadata.gz: 59f4ecc2e773d72bbef2d386a223e9859ee6f14c657e706c593e7a3f4c994c86aae885ef0b2993cbb916a1380daf3231882255685b6ffa360ffc4bf5dee591d8
7
- data.tar.gz: 2a8d540fabb8528a9c305b9d7cf7fe12588c14760557f08f4c380dfb6b45fdafec1232767234809101d6aa0e8f44f8ac895b120664dec8e533d180900ad06c6f
6
+ metadata.gz: c15a9b5155db86ea3fc43323bcb2ed37e82a2fff199077861a1c7698c998fb4ccd08b7ba70f3f9a190f1d2f5e14430caf880ce0a3d468d83f20941c3d9b41f8e
7
+ data.tar.gz: 8248faf77812deb4a073c4215d6796ac60932e97c0fc3efe36ce8a11eb79b5e06a41f7a7dab13c9759ffc79d296ad6d82a85340a59f388a7d19b966b5a6d631a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acidic_job (0.5.0)
4
+ acidic_job (0.5.4)
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.0"
4
+ VERSION = "0.5.4"
5
5
  end
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
- included do
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
- 100.times do
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
- return unless rescued_error
109
-
110
- # If we're leaving under an error condition, try to unlock the idempotency
111
- # key right away so that another request can try again.3
112
- begin
113
- key.update_columns(locked_at: nil, error_object: rescued_error)
114
- rescue StandardError => e
115
- # We're already inside an error condition, so swallow any additional
116
- # errors from here and just send them to logs.
117
- 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
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
- 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|
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 'securerandom'
216
+ require "securerandom"
195
217
  SecureRandom.hex
196
218
  end
197
219
  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.0
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-09-28 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