acidic_job 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/acidic_job.rb +33 -6
- data/lib/acidic_job/response.rb +1 -1
- data/lib/acidic_job/version.rb +1 -1
- data/lib/generators/acidic_job_generator.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed6991438cd55a757d0c3606591dd3e39cf470a2fca7486afea1d9efbb948aa6
|
4
|
+
data.tar.gz: d5f078c2111b538ebc7b16df70b2986fa7e1c958716806ad4778c81817b9d3f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24aa6c22959133bd68f3947bd15a742b6b314e7fb174b45fede5b598a85a63f04c2ba95a96e53ad3167fbfa1b9c9dcb981c7d59786be87d36520bab825255605
|
7
|
+
data.tar.gz: 02fad1948a7a17b81c2a6be0e48c271563fc7ce84a8de5ec5b84d3e1bb2ca65dbe6e838c4f4eb3c05eb2497424fe33fb5ea0e2c8e1001861ec75cb0c835a153a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,9 +33,9 @@ And then execute:
|
|
33
33
|
|
34
34
|
$ bundle install
|
35
35
|
|
36
|
-
Or install
|
36
|
+
Or simply execute to install the gem yourself:
|
37
37
|
|
38
|
-
$
|
38
|
+
$ bundle add acidic_job
|
39
39
|
|
40
40
|
Then, use the following command to copy over the AcidicJobKey migration.
|
41
41
|
|
data/lib/acidic_job.rb
CHANGED
@@ -18,6 +18,33 @@ module AcidicJob
|
|
18
18
|
|
19
19
|
class SerializedTransactionConflict < StandardError; end
|
20
20
|
|
21
|
+
class Key < ActiveRecord::Base
|
22
|
+
RECOVERY_POINT_FINISHED = "FINISHED"
|
23
|
+
|
24
|
+
self.table_name = "acidic_job_keys"
|
25
|
+
|
26
|
+
serialize :job_args, Hash
|
27
|
+
serialize :error_object
|
28
|
+
|
29
|
+
validates :job_name, presence: true
|
30
|
+
validates :job_args, presence: true
|
31
|
+
validates :idempotency_key, presence: true
|
32
|
+
validates :last_run_at, presence: true
|
33
|
+
validates :recovery_point, presence: true
|
34
|
+
|
35
|
+
def finished?
|
36
|
+
recovery_point == RECOVERY_POINT_FINISHED
|
37
|
+
end
|
38
|
+
|
39
|
+
def succeeded?
|
40
|
+
finished? && !failed?
|
41
|
+
end
|
42
|
+
|
43
|
+
def failed?
|
44
|
+
error_object.present?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
21
48
|
extend ActiveSupport::Concern
|
22
49
|
|
23
50
|
included do
|
@@ -51,7 +78,7 @@ module AcidicJob
|
|
51
78
|
phases = define_atomic_phases(defined_steps)
|
52
79
|
# { create_ride_and_audit_record: <#Method >, ... }
|
53
80
|
|
54
|
-
# find or create an
|
81
|
+
# find or create an Key record (our idempotency key) to store all information about this job
|
55
82
|
# side-effect: will set the @key instance variable
|
56
83
|
#
|
57
84
|
# A key concept here is that if two requests try to insert or update within
|
@@ -69,7 +96,7 @@ module AcidicJob
|
|
69
96
|
recovery_point = @key.recovery_point.to_sym
|
70
97
|
|
71
98
|
case recovery_point
|
72
|
-
when
|
99
|
+
when Key::RECOVERY_POINT_FINISHED.to_sym
|
73
100
|
break
|
74
101
|
else
|
75
102
|
raise UnknownRecoveryPoint unless phases.key? recovery_point
|
@@ -125,7 +152,7 @@ module AcidicJob
|
|
125
152
|
end
|
126
153
|
|
127
154
|
ActiveRecord::Base.transaction(isolation: isolation_level) do
|
128
|
-
@key =
|
155
|
+
@key = Key.find_by(idempotency_key: key_val)
|
129
156
|
|
130
157
|
if @key
|
131
158
|
# Programs enqueuing multiple jobs with different parameters but the
|
@@ -139,7 +166,7 @@ module AcidicJob
|
|
139
166
|
# Lock the key and update latest run unless the job is already finished.
|
140
167
|
@key.update!(last_run_at: Time.current, locked_at: Time.current) unless @key.finished?
|
141
168
|
else
|
142
|
-
@key =
|
169
|
+
@key = Key.create!(
|
143
170
|
idempotency_key: key_val,
|
144
171
|
locked_at: Time.current,
|
145
172
|
last_run_at: Time.current,
|
@@ -163,14 +190,14 @@ module AcidicJob
|
|
163
190
|
end
|
164
191
|
|
165
192
|
def define_atomic_phases(defined_steps)
|
166
|
-
defined_steps <<
|
193
|
+
defined_steps << Key::RECOVERY_POINT_FINISHED
|
167
194
|
|
168
195
|
{}.tap do |phases|
|
169
196
|
defined_steps.each_cons(2).map do |enter_method, exit_method|
|
170
197
|
phases[enter_method] = lambda do
|
171
198
|
method(enter_method).call
|
172
199
|
|
173
|
-
if exit_method.to_s ==
|
200
|
+
if exit_method.to_s == Key::RECOVERY_POINT_FINISHED
|
174
201
|
Response.new
|
175
202
|
else
|
176
203
|
RecoveryPoint.new(exit_method)
|
data/lib/acidic_job/response.rb
CHANGED
data/lib/acidic_job/version.rb
CHANGED
@@ -23,7 +23,7 @@ class AcidicJobGenerator < ActiveRecord::Generators::Base
|
|
23
23
|
# Copies the migration template to db/migrate.
|
24
24
|
def copy_files
|
25
25
|
migration_template "migration.rb",
|
26
|
-
"db/migrate/
|
26
|
+
"db/migrate/create_acidic_job_keys.rb"
|
27
27
|
end
|
28
28
|
|
29
29
|
protected
|