sidekiq_lifecycle_hooks 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87e878d6e4faa8edd1ddd411fd37ff636ab3178de43d50412224f152e4e609d2
4
- data.tar.gz: 5e69ab953b3ace67e84015c4bbe44e1cc22af1fb406d79ed4adaceae5bc20e35
3
+ metadata.gz: e680eb3a1bd1a627e65eb60a1af029b7792fecf91af35eddac1219a82d83cbbe
4
+ data.tar.gz: d6160ba25cdc33755c531b8289da34c0b17627131142128d1c505f93d764efb1
5
5
  SHA512:
6
- metadata.gz: fe5a1d94f1332b871acf7617474070bbbbe972e63e2b196a6eb28ea6695411f792e52d6a7fc31ba2b4d91e073ba3f33e9fe39bb69512e2fe0bcd5816bb4a2ee0
7
- data.tar.gz: 90a73f90eae0d611a852a7f579d0de37e290ebe3dd6de9b85b9b2b37eba54886d1908573ad76301f08f84c114262d138b783caac42d41c21d9c98773ddee187e
6
+ metadata.gz: 2cc51496cd0b9d004f488d924714e700c1725e2c8ec03d5394ab23a71a5a731562a50b555837ee952dd788cb3dfea573a65c1e4dd5aa39286be76f3224f61206
7
+ data.tar.gz: 260dafdde9bb4f6347c41beba08686c08d68c6f81b7b37c469fb906a5eb9413b6395eb3595b81ec010931be52da14ce11d759f0f63112b8f7e841b1343f44a85
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
1
 
2
+ ### 1.3.0
3
+
4
+ - Move to after_commit
5
+
6
+ ### 1.2.0
7
+
8
+ - Added a retry in Async job where record cannot be found, in case
9
+ attempt to find record occurred too early. This can happen when the initial record creation is delayed by e.g. after_create hooks and indexes.
10
+
2
11
  ### 1.1.0
3
12
 
4
13
  - Add early return in Async Job for when record cannot be found
data/README.md CHANGED
@@ -102,3 +102,11 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/WillTa
102
102
  ## License
103
103
 
104
104
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
105
+
106
+ ## Notes to self
107
+
108
+ To deploy a new version
109
+ - Update the version number & changelog
110
+ - gem build sidekiq_lifecycle_hooks.gemspec
111
+ - gem push sidekiq_lifecycle_hooks-1.X.0.gem
112
+
data/lib/lifecycle_job.rb CHANGED
@@ -3,11 +3,14 @@ class LifecycleJob
3
3
  sidekiq_options queue: :default, retry: 1
4
4
 
5
5
  def perform(class_name, record_id, action, previous_attrs = '{}')
6
- return unless (record = if action == 'destroy'
7
- class_name.constantize.new(JSON.parse(previous_attrs))
8
- else
9
- class_name.constantize.find_by(id: record_id)
10
- end)
6
+
7
+ record = fetch_record(class_name, record_id, action, previous_attrs)
8
+ unless record
9
+ sleep 3 # waits for 3 seconds before retrying
10
+ record = fetch_record(class_name, record_id, action, previous_attrs)
11
+ end
12
+
13
+ return unless record
11
14
 
12
15
  case action
13
16
  when 'create'
@@ -18,4 +21,14 @@ class LifecycleJob
18
21
  record.async_after_destroy_actions
19
22
  end
20
23
  end
24
+
25
+ private
26
+
27
+ def fetch_record(class_name, record_id, action, previous_attrs)
28
+ if action == 'destroy'
29
+ class_name.constantize.new(JSON.parse(previous_attrs))
30
+ else
31
+ class_name.constantize.find_by(id: record_id)
32
+ end
33
+ end
21
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SidekiqLifecycleHooks
4
- VERSION = "1.1.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -4,22 +4,27 @@ module SidekiqLifecycleHooks
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- after_create :enqueue_async_after_create_actions, if: -> { self.class.method_defined?(:async_after_create_actions) }
8
- after_update :enqueue_async_after_update_actions, if: -> { self.class.method_defined?(:async_after_update_actions) }
9
- after_destroy :enqueue_async_after_destroy_actions, if: -> { self.class.method_defined?(:async_after_destroy_actions) }
7
+ after_create_commit :enqueue_async_after_create_actions, if: -> { self.class.method_defined?(:async_after_create_actions) }
8
+ after_update_commit :enqueue_async_after_update_actions, if: -> { self.class.method_defined?(:async_after_update_actions) }
9
+ before_destroy :prepare_async_after_destroy_actions, if: -> { self.class.method_defined?(:async_after_destroy_actions) }
10
+ after_destroy_commit :enqueue_async_after_destroy_actions, if: -> { self.class.method_defined?(:async_after_destroy_actions) }
10
11
  end
11
12
 
12
13
  private
13
14
 
14
15
  def enqueue_async_after_create_actions
15
- LifecycleJob.perform_in(1.second, self.class.name, id, 'create')
16
+ LifecycleJob.perform_async(self.class.name, id, 'create')
16
17
  end
17
18
 
18
19
  def enqueue_async_after_update_actions
19
- LifecycleJob.perform_in(1.second, self.class.name, id, 'update')
20
+ LifecycleJob.perform_async(self.class.name, id, 'update')
21
+ end
22
+
23
+ def prepare_async_after_destroy_actions
24
+ @destroyed_attributes = attributes.to_json
20
25
  end
21
26
 
22
27
  def enqueue_async_after_destroy_actions
23
- LifecycleJob.perform_in(1.second, self.class.name, id, 'destroy', attributes.to_json)
28
+ LifecycleJob.perform_async(self.class.name, id, 'destroy', @destroyed_attributes)
24
29
  end
25
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_lifecycle_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-07 00:00:00.000000000 Z
11
+ date: 2024-09-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: