marj 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/lib/marj_record.rb +21 -8
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42a85f9f7752849bf9e7781eef989294d45420763c16fb5fadee3df3fc7a0c2e
4
- data.tar.gz: acadf4b197599bd3150629a557857b8d48fa8bed662a2f4140a8dbe16f01e7ba
3
+ metadata.gz: '078c3d48b27784e09425ba8eda2207038d6ce5c7a711953e8567c76aac689435'
4
+ data.tar.gz: c129b4848de1e2fe55de0de3a4893586a052cd814df798879dc196a81c1a94d5
5
5
  SHA512:
6
- metadata.gz: a30fe477c647ca284fdcde1bcccc8b000f3b4ef78b62de84f486376fa65450fcf3a52c7132df520fd45ee7e0f2856d8812a16b804ce89baa93a8d2353dc8348f
7
- data.tar.gz: 72a63c15a36f6bade30e93bb5f84d5d8c6bbab33b12f4c92c78579f07eb86689e8e7609dae9ec16b94334140e8e4c2828b6e6cb19a580ad9a42730f27afab8cb
6
+ metadata.gz: 4cef48fb7c862123f4a5ad7ed5bf7a9d95eb3d7036831db79ad3dc9cf94f074a112ce58f0189484f991a52185cdf8e8ceed48dd9237bf4a04d5e3bbe30b2d274
7
+ data.tar.gz: 7631b1a0fef1ece1edcf15541dc85d25b2d5d3ba1cb8ca716133975126e5ce9bb4f28d627a4c2a5d751029ce8f67ba4db47c6fa9d197467fade4c09d20a04716
data/README.md CHANGED
@@ -290,6 +290,8 @@ SomeJob.perform_now(args)
290
290
  SomeJob.new(args).enqueue
291
291
  SomeJob.new(args).enqueue(options)
292
292
 
293
+ SomeJob.perform_later(SomeJob.new(args))
294
+
293
295
  SomeJob.perform_later(args)
294
296
  SomeJob.set(options).perform_later(args)
295
297
 
data/lib/marj_record.rb CHANGED
@@ -9,7 +9,7 @@ require_relative 'marj_config'
9
9
  # See https://github.com/nicholasdower/marj
10
10
  class Marj < ActiveRecord::Base
11
11
  # The Marj version.
12
- VERSION = '2.0.1'
12
+ VERSION = '2.1.0'
13
13
 
14
14
  # Executes the job associated with this record and returns the result.
15
15
  def execute
@@ -93,14 +93,18 @@ class Marj < ActiveRecord::Base
93
93
  # @param job [ActiveJob::Base]
94
94
  # @return [ActiveJob::Base]
95
95
  def self.register_callbacks(job, record)
96
- raise 'callbacks already registered' if job.singleton_class.instance_variable_get(:@__marj)
96
+ if job.singleton_class.instance_variable_get(:@__marj)
97
+ # Callbacks already registered. We just need to update the record.
98
+ job.singleton_class.instance_variable_set(:@__marj, record)
99
+ return
100
+ end
97
101
 
98
102
  # We need to detect three cases:
99
103
  # - If a job succeeds, after_perform will be called.
100
104
  # - If a job fails and should be retried, enqueue will be called. This is handled by the enqueue method.
101
105
  # - If a job exceeds its max attempts, after_discard will be called.
102
- job.singleton_class.after_perform { |_j| record.destroy! }
103
- job.singleton_class.after_discard { |_j, _exception| record.destroy! }
106
+ job.singleton_class.after_perform { |_j| job.singleton_class.instance_variable_get(:@__marj).destroy! }
107
+ job.singleton_class.after_discard { |_j, _exception| job.singleton_class.instance_variable_get(:@__marj).destroy! }
104
108
  job.singleton_class.instance_variable_set(:@__marj, record)
105
109
 
106
110
  job
@@ -121,19 +125,28 @@ class Marj < ActiveRecord::Base
121
125
  # registered on the job instance so that when the job is executed, the database record is deleted or updated
122
126
  # (depending on the result).
123
127
  #
124
- # There are three cases:
128
+ # There are two normal cases:
125
129
  # - The first time a job is enqueued, we need to create the record and register callbacks.
126
130
  # - If a previously enqueued job instance is re-enqueued, for instance after execution fails, callbacks have
127
131
  # already been registered. In this case we only need to update the record.
128
- # - It is also possible for new job instance to be created for a job that is already in the database. In this case
129
- # we need to update the record and register callbacks.
130
132
  #
131
133
  # We keep track of whether callbacks have been registered by setting the @__marj instance variable on the job's
132
134
  # singleton class. This holds a reference to the record. This allows us to update the record without re-fetching it
133
135
  # and also ensures that if execute is called on a record any updates to the database are reflected on that record
134
136
  # instance.
137
+ #
138
+ # There are also two edge cases:
139
+ # - It is possible for new job instance to be created for a job that is already in the database. In this case
140
+ # we need to update the record and register callbacks.
141
+ # - It is possible for the underlying row corresponding to an existing job to have been deleted. In this case we
142
+ # need to create a new record and update the reference on the job's singleton class.
135
143
  if (record = job.singleton_class.instance_variable_get(:@__marj))
136
- record.update!(serialized)
144
+ if Marj.exists?(job_id: job.job_id)
145
+ record.update!(serialized)
146
+ else
147
+ record = Marj.create!(serialized)
148
+ register_callbacks(job, record)
149
+ end
137
150
  else
138
151
  record = Marj.find_or_create_by!(job_id: job.job_id) { _1.assign_attributes(serialized) }
139
152
  register_callbacks(job, record)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Dower
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-23 00:00:00.000000000 Z
11
+ date: 2024-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -56,8 +56,8 @@ licenses:
56
56
  - MIT
57
57
  metadata:
58
58
  bug_tracker_uri: https://github.com/nicholasdower/marj/issues
59
- changelog_uri: https://github.com/nicholasdower/marj/releases/tag/v2.0.1
60
- documentation_uri: https://www.rubydoc.info/github/nicholasdower/marj/v2.0.1
59
+ changelog_uri: https://github.com/nicholasdower/marj/releases/tag/v2.1.0
60
+ documentation_uri: https://www.rubydoc.info/github/nicholasdower/marj/v2.1.0
61
61
  homepage_uri: https://github.com/nicholasdower/marj
62
62
  rubygems_mfa_required: 'true'
63
63
  source_code_uri: https://github.com/nicholasdower/marj