sidekiq-serialized_current_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21dbbeb4be1ba5aae80db8434a3708247e43844aae1dc733ab18cbfed82b7dad
4
+ data.tar.gz: 0113e0e2fe42bc22e570290c785482696eaf4da1fda0de83d7a757d2d8159ba2
5
+ SHA512:
6
+ metadata.gz: a22ed569eb797e646694f592baf787570f2b8841b14f2655091e66260289bf1049d07f1de8881de540be66cc0b00be8cc6ab754c46172972b78123dfe4ef6efe
7
+ data.tar.gz: 1a463411930f80a1fe92d59c52abcc87206582bfb9315a0424ef352a2e92bc6860bb75c86a5b7cbea403bcfa81e02574f8aee4b63256662ac407ac77d5ffa929
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Fabian Schwahn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Sidekiq::SerializedCurrentAttributes
2
+
3
+ Extends Sidekiq::CurrentAttributes to serialize values (such as activerecord objects).
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```sh
10
+ bundle add sidekiq-serialized_current_attributes
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Automatically save and load any current attributes in the execution context so context attributes "flow" from Rails actions into any associated jobs. This can be useful for multi-tenancy, i18n locale, timezone, any implicit per-request attribute. See `ActiveSupport::CurrentAttributes`.
16
+
17
+ For multiple current attributes, pass an array of current attributes.
18
+
19
+ ```ruby
20
+ # in your initializer
21
+ require "sidekiq/serialized_current_attributes"
22
+ Sidekiq::SerializedCurrentAttributes.persist("Myapp::Current")
23
+ # or multiple current attributes
24
+ Sidekiq::SerializedCurrentAttributes.persist(["Myapp::Current", "Myapp::OtherCurrent"])
25
+ ```
26
+
27
+ ## License
28
+
29
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ module SerializedCurrentAttributes
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+ require "active_support/current_attributes"
5
+ require "active_job/arguments"
6
+
7
+ require_relative "serialized_current_attributes/version"
8
+
9
+ # Code is pretty directly taken from https://github.com/sidekiq/sidekiq/blob/main/lib/sidekiq/middleware/current_attributes.rb, and adjusted so it uses ActiveJob argument serialization to support storing models.
10
+ module Sidekiq
11
+ module SerializedCurrentAttributes
12
+ class Save
13
+ include Sidekiq::ClientMiddleware
14
+
15
+ def initialize(cattrs)
16
+ @cattrs = cattrs
17
+ end
18
+
19
+ def call(_, job, _, _)
20
+ @cattrs.each do |(key, strklass)|
21
+ if !job.has_key?(key)
22
+ attrs = serialize(strklass.constantize.attributes)
23
+ # Retries can push the job N times, we don't
24
+ # want retries to reset cattr. #5692, #5090
25
+ job[key] = attrs if attrs.any?
26
+ end
27
+ end
28
+ yield
29
+ end
30
+
31
+ def serialize(attributes)
32
+ serialized_values = ActiveJob::Arguments.serialize(attributes.values)
33
+ attributes.keys.zip(serialized_values).to_h
34
+ end
35
+ end
36
+
37
+ class Load
38
+ include Sidekiq::ServerMiddleware
39
+
40
+ def initialize(cattrs)
41
+ @cattrs = cattrs
42
+ end
43
+
44
+ def call(_, job, _, &block)
45
+ cattrs_to_reset = []
46
+
47
+ @cattrs.each do |(key, strklass)|
48
+ if job.has_key?(key)
49
+ constklass = strklass.constantize
50
+ cattrs_to_reset << constklass
51
+
52
+ attributes = deserialize(job[key])
53
+ attributes.each do |(attribute, value)|
54
+ constklass.public_send("#{attribute}=", value)
55
+ end
56
+ end
57
+ end
58
+
59
+ yield
60
+ ensure
61
+ cattrs_to_reset.each(&:reset)
62
+ end
63
+
64
+ def deserialize(attributes)
65
+ deserialized_values = ActiveJob::Arguments.deserialize(attributes.values)
66
+ attributes.keys.zip(deserialized_values).to_h
67
+ end
68
+ end
69
+
70
+ class << self
71
+ def persist(klass_or_array, config = Sidekiq.default_configuration)
72
+ cattrs = build_cattrs_hash(klass_or_array)
73
+
74
+ config.client_middleware.add Save, cattrs
75
+ config.server_middleware.add Load, cattrs
76
+ end
77
+
78
+ private
79
+
80
+ def build_cattrs_hash(klass_or_array)
81
+ if klass_or_array.is_a?(Array)
82
+ {}.tap do |hash|
83
+ klass_or_array.each_with_index do |klass, index|
84
+ hash[key_at(index)] = klass.to_s
85
+ end
86
+ end
87
+ else
88
+ {key_at(0) => klass_or_array.to_s}
89
+ end
90
+ end
91
+
92
+ def key_at(index)
93
+ (index == 0) ? "cattr" : "cattr_#{index}"
94
+ end
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-serialized_current_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabian Schwahn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - fabian.schwahn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - Rakefile
65
+ - lib/sidekiq/serialized_current_attributes.rb
66
+ - lib/sidekiq/serialized_current_attributes/version.rb
67
+ homepage: https://github.com/denkungsart/sidekiq-serialized_current_attributes
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/denkungsart/sidekiq-serialized_current_attributes
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.6.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.4.10
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Serialize CurrentAttributes for Sidekiq
91
+ test_files: []