sidekiq-activerecord-shard 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +60 -0
- data/Rakefile +3 -0
- data/lib/sidekiq-activerecord-shard/config.rb +17 -0
- data/lib/sidekiq-activerecord-shard/middleware.rb +45 -0
- data/lib/sidekiq-activerecord-shard/version.rb +3 -0
- data/lib/sidekiq-activerecord-shard.rb +3 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f2e44ce2e8eb14ffe2a5d28c3e09ded3aab7e90b113be16e9599c483e24880c6
|
4
|
+
data.tar.gz: '08cc893a7882520acd9a57e5d71e6166e635da77ca6c5e305e2789cf689c0a34'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6073afb41c1ff90745b5084e2a7482586c0fd7262d2152c42957635778cfdfd48fe5c343ebea5d69af721879d90f0a53923e7d8e58085c0d9ff61672a2c0075e
|
7
|
+
data.tar.gz: 0ee0dd719e024ca5559e1b8ce70159c806775b7ea4f9ae7851874990857e379aa54c10018a79b69c28d9fc007e98f797f61c6d4a602b1569048f235e0af5dee4
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022 Jason Lee
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Sidekiq Activerecord::Shard Middleware
|
2
|
+
|
3
|
+
A Sidekiq middleware for supports ActiveRecord Shard with [ActiveSupport:CurrentAttribute](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "sidekiq-activerecord-shard"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```bash
|
15
|
+
$ bundle
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Add follow code into `config/initializers/sidekiq-activerecord-shard.rb`:
|
21
|
+
|
22
|
+
```rb
|
23
|
+
SidekiqActiveRecordShard.configure do
|
24
|
+
self.selected_shard = -> do
|
25
|
+
case Current.tenant_id
|
26
|
+
when "company1"
|
27
|
+
:company1
|
28
|
+
else
|
29
|
+
:default
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Create `app/models/current.rb`:
|
36
|
+
|
37
|
+
```rb
|
38
|
+
class Current < ActiveSupport::CurrentAttributes
|
39
|
+
attribute :tenant_id
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Set `Current.tenant_id` on ApplicationController:
|
44
|
+
|
45
|
+
```rb
|
46
|
+
class ApplicationController < ActionController::Base
|
47
|
+
before_action :set_current_tenant_id
|
48
|
+
|
49
|
+
def set_current_tenant_id
|
50
|
+
Current.tenant_id = request.headers["x-tenant-id"]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Contribution directions go here.
|
58
|
+
|
59
|
+
## License
|
60
|
+
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,17 @@
|
|
1
|
+
module SidekiqActiveRecordShard
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :selected_shard
|
4
|
+
end
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def config
|
8
|
+
return @config if defined?(@config)
|
9
|
+
|
10
|
+
@config = Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def configure(&block)
|
14
|
+
config.instance_exec(&block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "sidekiq"
|
2
|
+
|
3
|
+
module SidekiqActiveRecordShard
|
4
|
+
class Client
|
5
|
+
include ::Sidekiq::ClientMiddleware
|
6
|
+
|
7
|
+
def call(_jobclass, job, _queue, _redis)
|
8
|
+
# Store shard value in Job arguments
|
9
|
+
job["_active_record_shard"] = SidekiqActiveRecordShard.selected_shard.call
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Server
|
15
|
+
include Sidekiq::ServerMiddleware
|
16
|
+
def call(_jobclass, job, _queue, &block)
|
17
|
+
set_shard(job["_active_record_shard"], &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Inspired by ActiveRecord::Middleware::ShardSelector
|
21
|
+
# https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/middleware/shard_selector.rb#L54
|
22
|
+
def set_shard(shared, &block)
|
23
|
+
options = Rails.application.config.active_record.shard_selector
|
24
|
+
|
25
|
+
ActiveRecord::Base.connected_to(shard: shard.to_sym) do
|
26
|
+
ActiveRecord::Base.prohibit_shard_swapping(options.fetch(:lock, true), &block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Sidekiq.configure_client do |config|
|
33
|
+
config.client_middleware do |chain|
|
34
|
+
chain.add SidekiqActiveRecordShard::Client
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Sidekiq.configure_server do |config|
|
39
|
+
config.client_middleware do |chain|
|
40
|
+
chain.add SidekiqActiveRecordShard::Client
|
41
|
+
end
|
42
|
+
config.server_middleware do |chain|
|
43
|
+
chain.add SidekiqActiveRecordShard::Server
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-activerecord-shard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sidekiq
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6'
|
41
|
+
description: A Sidekiq middleware for supports ActiveRecord Shard.
|
42
|
+
email:
|
43
|
+
- huacnlee@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/sidekiq-activerecord-shard.rb
|
52
|
+
- lib/sidekiq-activerecord-shard/config.rb
|
53
|
+
- lib/sidekiq-activerecord-shard/middleware.rb
|
54
|
+
- lib/sidekiq-activerecord-shard/version.rb
|
55
|
+
homepage: https://github.com/huacnlee/sidekiq-activerecord-shard
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata:
|
59
|
+
homepage_uri: https://github.com/huacnlee/sidekiq-activerecord-shard
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.3.12
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Add ActiveRecord Shard supports for Sidekiq.
|
79
|
+
test_files: []
|