sidekiq_lifecycle_hooks 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1c808c622f93fbfe6d9d61ae9113ad611528b50a472d6712ac91a8a74b1ccb30
4
+ data.tar.gz: cb6344d7529e6972c7012fee6ace78372f3e3f98d66d02941bbc7a911dd359f8
5
+ SHA512:
6
+ metadata.gz: e6a55ba37e8204b54e9de3bb1f69f8c6a8a2d1f038d52bdea79076454ee7528933b913f8a5c63943e59ae940361e7db911ca1458a45678d0ae779ae910bb77f7
7
+ data.tar.gz: 0c937985feab1f1a220dd419840e0534855da25797dbd01c0768720958a154251d8d33206d32f79aa5a8c31bdc7cccc97247416dfe098909060377290ab81963
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.9.0
2
+
3
+ - Set up Gem
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Sidekiq Lifecycle Hooks
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/sidekiq_lifecycle_hooks.svg)](https://badge.fury.io/rb/sidekiq_lifecycle_hooks)
4
+
5
+ Async model lifecycle hooks for Rails, using Sidekiq.
6
+
7
+ This Ruby on Rails gem lets you replace `after_create`, `after_update`, and `after_destroy` with asynchronous equivelants, improving the performance and decreasing the memory usage of your web server.
8
+
9
+ Ideal for offloading notifications, analytics, webhooks, large DB writes and anything else that doesn't *need* to delay your response.
10
+
11
+ This approach is generally easier to write and maintain than creating a whole sidekiq job for every little task. The ease of use also encourages good async hygiene.
12
+
13
+
14
+ ## Installation
15
+
16
+ Setup [Sidekiq](https://github.com/sidekiq/sidekiq) as your background task runner.
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'sidekiq_lifecycle_hooks'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```bash
27
+ $ bundle install
28
+ ```
29
+
30
+ Include `SidekiqLifecycleHooks` in your ApplicationRecord
31
+
32
+ ```ruby
33
+ class ApplicationRecord < ActiveRecord::Base
34
+ include SidekiqLifecycleHooks
35
+ end
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ In each model, replace any synchronous lifecycle hooks with the following:
41
+
42
+ ```ruby
43
+ class MyModel < ApplicationRecord
44
+
45
+ # Replace these:
46
+ after_create :report_to_analytics
47
+ after_update :something_else
48
+ after_destroy :another_thing
49
+
50
+ # With these:
51
+ def async_after_create_actions
52
+ report_to_analytics
53
+ # and_you_can_add_more_than_one ...
54
+ end
55
+
56
+ def async_after_update_actions
57
+ something_else
58
+ end
59
+
60
+ def async_after_destroy_actions # READ NOTE BELOW
61
+ another_thing
62
+ end
63
+
64
+ end
65
+ ```
66
+
67
+ ## How it works
68
+
69
+ These methods enqueue a Sidekiq job called `LifecycleJob` which loads your model (or a shallow clone, if after deleting), then runs any methods required.
70
+
71
+
72
+ ## Usage notes
73
+
74
+ ### after_destroy creates a shallow clone
75
+
76
+ async_after_destroy_actions instantiates
77
+ `YourObject.new(previous_object_attributes)`
78
+ and then runs the methods from this new model.
79
+
80
+ Any child models of the original will already have been destroyed or rendered inaccessible. If you require those, use a regular before_destroy hook.
81
+
82
+
83
+ ### Separate mission critical actions
84
+
85
+ Errors prevent subsequent methods from running.
86
+
87
+ ```
88
+ def async_after_create_actions
89
+ report_to_analytics
90
+ raise # Oops
91
+ create_slack_notification # Will not run
92
+ end
93
+ ```
94
+
95
+ Enqueue mission critical tasks in seperate Sidekiq jobs.
96
+
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/WillTaylor22/sidekiq_lifecycle_hooks. This project is intended to be a safe, welcoming space for collaboration. Please be chill in this project's codebase and issue tracker.
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,21 @@
1
+ class LifecycleJob
2
+ include Sidekiq::Job
3
+ sidekiq_options queue: :default, retry: 1
4
+
5
+ def perform(class_name, record_id, action, previous_attrs = '{}')
6
+ 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
11
+
12
+ case action
13
+ when 'create'
14
+ record.async_after_create_actions
15
+ when 'update'
16
+ record.async_after_update_actions
17
+ when 'destroy'
18
+ record.async_after_destroy_actions
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqLifecycleHooks
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'lifecycle_job'
2
+
3
+ module SidekiqLifecycleHooks
4
+ extend ActiveSupport::Concern
5
+
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) }
10
+ end
11
+
12
+ private
13
+
14
+ def enqueue_async_after_create_actions
15
+ LifecycleJob.perform_in(1.second, self.class.name, id, 'create')
16
+ end
17
+
18
+ def enqueue_async_after_update_actions
19
+ LifecycleJob.perform_in(1.second, self.class.name, id, 'update')
20
+ end
21
+
22
+ def enqueue_async_after_destroy_actions
23
+ LifecycleJob.perform_in(1.second, self.class.name, id, 'destroy', attributes.to_json)
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sidekiq_lifecycle_hooks/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sidekiq_lifecycle_hooks"
7
+ spec.version = SidekiqLifecycleHooks::VERSION
8
+ spec.authors = ["Will Taylor"]
9
+ spec.email = ["wrftaylor@gmail.com"]
10
+
11
+ spec.summary = "Asynchronous lifecycle hooks for rails apps using sidekiq."
12
+ spec.homepage = "https://github.com/WillTaylor22/sidekiq_lifecycle_hooks."
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/WillTaylor22/sidekiq_lifecycle_hooks"
18
+ spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/WillTaylor22/sidekiq_lifecycle_hooks/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) ||
25
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
@@ -0,0 +1,3 @@
1
+ module SidekiqLifecycleHooks
2
+ VERSION: 0.9.0
3
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_lifecycle_hooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Will Taylor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - wrftaylor@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - README.md
22
+ - Rakefile
23
+ - lib/lifecycle_job.rb
24
+ - lib/sidekiq_lifecycle_hooks.rb
25
+ - lib/sidekiq_lifecycle_hooks/version.rb
26
+ - sidekiq_lifecycle_hooks.gemspec
27
+ - sig/sidekiq_lifecycle_hooks.rbs
28
+ homepage: https://github.com/WillTaylor22/sidekiq_lifecycle_hooks.
29
+ licenses: []
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org
32
+ homepage_uri: https://github.com/WillTaylor22/sidekiq_lifecycle_hooks.
33
+ source_code_uri: https://github.com/WillTaylor22/sidekiq_lifecycle_hooks
34
+ changelog_uri: https://raw.githubusercontent.com/WillTaylor22/sidekiq_lifecycle_hooks/main/CHANGELOG.md
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.4.20
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Asynchronous lifecycle hooks for rails apps using sidekiq.
54
+ test_files: []