acidic_job 0.1.3 → 0.1.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: 62d9233935caa3f86b25ae851b5039dbc6fc3ca29365acceb9e2fb6c50a37e84
4
- data.tar.gz: 17488a81beea742db0f5e2ef24f40c11e43e89199e1986d95882313dc2cf5f64
3
+ metadata.gz: fb1de28508c8a662b772a9b9f8fb4046d8c2a64e242826ec92c1ecc820dbc18b
4
+ data.tar.gz: a0db4307ec7d7c971d9a5e1ff30ccb2243afa9ea35a7f17fe91f8c81d775a52b
5
5
  SHA512:
6
- metadata.gz: 1634b63e1d1a6552f1f3c4615ac8376b9b055d2812a8b1e3018ba8f3297121c04f7a3c41e53b17fdfa30c0d01878183074c730907139b02cd3c4567ec5eb51ef
7
- data.tar.gz: 2d4837e2ac68315de5e6ab60b757f10cc26244de1294956f4b6834f2552062487b7969a28758a6770815c3e352e4573f8da22d65ebafe53589459580f2140560
6
+ metadata.gz: 0e39981533a09f2f197f8c047d057328be88c5a34666bd8520500a805bee7fb4d86ac74fdebe673cd67fe92c3faff072a3d62eef79e4e14b21ffdfb05b292e2b
7
+ data.tar.gz: b2eb0c3c2fe92b27fc065fc0a450261743b3b6b397c2212e1b46ed4773074342c64c8433930b71f4125a71b9c692f5baa1d29b3ec9d9fc6ac13905c1fe4e2d2f
data/Gemfile CHANGED
@@ -11,6 +11,10 @@ gem "minitest", "~> 5.0"
11
11
 
12
12
  gem "rubocop", "~> 1.7"
13
13
 
14
+ gem "rubocop-minitest"
15
+
16
+ gem "rubocop-rake"
17
+
14
18
  gem "activerecord", "~> 6.1.3.2"
15
19
 
16
20
  gem "activejob", "~> 6.1.3.2"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acidic_job (0.1.3)
4
+ acidic_job (0.1.4)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -53,6 +53,10 @@ GEM
53
53
  unicode-display_width (>= 1.4.0, < 3.0)
54
54
  rubocop-ast (1.7.0)
55
55
  parser (>= 3.0.1.1)
56
+ rubocop-minitest (0.14.0)
57
+ rubocop (>= 0.90, < 2.0)
58
+ rubocop-rake (0.6.0)
59
+ rubocop (~> 1.0)
56
60
  ruby-progressbar (1.11.0)
57
61
  simplecov (0.21.2)
58
62
  docile (~> 1.1)
@@ -67,6 +71,7 @@ GEM
67
71
  zeitwerk (2.4.2)
68
72
 
69
73
  PLATFORMS
74
+ ruby
70
75
  x86_64-darwin-17
71
76
 
72
77
  DEPENDENCIES
@@ -77,6 +82,8 @@ DEPENDENCIES
77
82
  minitest (~> 5.0)
78
83
  rake (~> 13.0)
79
84
  rubocop (~> 1.7)
85
+ rubocop-minitest
86
+ rubocop-rake
80
87
  simplecov
81
88
  sqlite3
82
89
 
data/bin/console CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "bundler/setup"
5
5
  require "acidic_job"
6
+ require_relative "../test/setup"
6
7
 
7
8
  # You can add fixtures and/or initialization code here to make experimenting
8
9
  # with your gem easier. You can also use a different console, if you like.
data/lib/acidic_job.rb CHANGED
@@ -6,7 +6,7 @@ require_relative "acidic_job/recovery_point"
6
6
  require_relative "acidic_job/response"
7
7
  require "active_support/concern"
8
8
 
9
- # rubocop:disable Metrics/ModuleLength, Style/Documentation, Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
9
+ # rubocop:disable Metrics/ModuleLength, Style/Documentation, Metrics/AbcSize, Metrics/MethodLength
10
10
  module AcidicJob
11
11
  class IdempotencyKeyRequired < StandardError; end
12
12
 
@@ -62,12 +62,11 @@ module AcidicJob
62
62
  IDEMPOTENCY_KEY_MIN_LENGTH = 20
63
63
 
64
64
  # &block
65
- def idempotently(key:, with:)
65
+ def idempotently(with:)
66
66
  # set accessors for each argument passed in to ensure they are available
67
67
  # to the step methods the job will have written
68
68
  define_accessors_for_passed_arguments(with)
69
69
 
70
- validate_passed_idempotency_key(key)
71
70
  validate_passed_arguments(with)
72
71
 
73
72
  # execute the block to gather the info on what phases are defined for this job
@@ -78,7 +77,7 @@ module AcidicJob
78
77
 
79
78
  # find or create an AcidicJobKey record to store all information about this job
80
79
  # side-effect: will set the @key instance variable
81
- ensure_idempotency_key_record(key, with[:params], defined_steps.first)
80
+ ensure_idempotency_key_record(job_id, with[:params], defined_steps.first)
82
81
 
83
82
  # if the key record is already marked as finished, immediately return its result
84
83
  return @key.succeeded? if @key.finished?
@@ -110,17 +109,14 @@ module AcidicJob
110
109
 
111
110
  private
112
111
 
113
- def atomic_phase(key = nil, proc = nil, &block)
112
+ def atomic_phase(key, proc = nil, &block)
114
113
  error = false
115
114
  phase_callable = (proc || block)
116
115
 
117
116
  begin
118
- # ActiveRecord::Base.transaction(isolation: :serializable) do
119
- ActiveRecord::Base.transaction(isolation: :read_uncommitted) do
117
+ key.with_lock do
120
118
  phase_result = phase_callable.call
121
119
 
122
- # TODO: why is this here?
123
- key ||= @key
124
120
  phase_result.call(key: key)
125
121
  end
126
122
  rescue StandardError => e
@@ -129,20 +125,26 @@ module AcidicJob
129
125
  ensure
130
126
  # If we're leaving under an error condition, try to unlock the idempotency
131
127
  # key right away so that another request can try again.
132
- if error && !key.nil?
133
- begin
134
- key.update_columns(locked_at: nil, error_object: error)
135
- rescue StandardError => e
136
- # We're already inside an error condition, so swallow any additional
137
- # errors from here and just send them to logs.
138
- puts "Failed to unlock key #{key.id} because of #{e}."
139
- end
128
+ begin
129
+ key.update_columns(locked_at: nil, error_object: error) if error.present?
130
+ rescue StandardError => e
131
+ # We're already inside an error condition, so swallow any additional
132
+ # errors from here and just send them to logs.
133
+ puts "Failed to unlock key #{key.id} because of #{e}."
140
134
  end
141
135
  end
142
136
  end
143
137
 
144
138
  def ensure_idempotency_key_record(key_val, params, first_step)
145
- atomic_phase do
139
+ # isolation_level = case ActiveRecord::Base.connection.adapter_name.downcase.to_sym
140
+ # when :sqlite
141
+ # :read_uncommitted
142
+ # else # :nocov:
143
+ # :serializable # :nocov:
144
+ # end
145
+ isolation_level = :read_uncommitted
146
+
147
+ ActiveRecord::Base.transaction(isolation: isolation_level) do
146
148
  @key = AcidicJobKey.find_by(idempotency_key: key_val)
147
149
 
148
150
  if @key
@@ -167,9 +169,6 @@ module AcidicJob
167
169
  job_args: params.as_json
168
170
  )
169
171
  end
170
-
171
- # no response and no need to set a recovery point
172
- NoOp.new
173
172
  end
174
173
  end
175
174
 
@@ -184,13 +183,6 @@ module AcidicJob
184
183
  true
185
184
  end
186
185
 
187
- def validate_passed_idempotency_key(key)
188
- raise IdempotencyKeyRequired if key.nil?
189
- raise IdempotencyKeyTooShort if key.length < IDEMPOTENCY_KEY_MIN_LENGTH
190
-
191
- true
192
- end
193
-
194
186
  def validate_passed_arguments(attributes)
195
187
  missing_attributes = self.class.required_attributes.select do |required_attribute|
196
188
  attributes[required_attribute].nil?
@@ -220,4 +212,4 @@ module AcidicJob
220
212
  end
221
213
  end
222
214
  end
223
- # rubocop:enable Metrics/ModuleLength, Style/Documentation, Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
215
+ # rubocop:enable Metrics/ModuleLength, Style/Documentation, Metrics/AbcSize, Metrics/MethodLength
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AcidicJob
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  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.1.3
4
+ version: 0.1.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-07-07 00:00:00.000000000 Z
11
+ date: 2021-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport