sidekiq-activerecord-shard 0.1.3 → 0.1.4
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 +4 -4
- data/README.md +23 -17
- data/lib/sidekiq-activerecord-shard/middleware.rb +7 -2
- data/lib/sidekiq-activerecord-shard/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc3fd046c8730189dc8afc6549c1182e4ae0777e5dc686ad50765c2e0f577945
|
4
|
+
data.tar.gz: 570c63c2c849cf1e563da69ffd40cc4079d4a3cace6919e81aa97819ea45dada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ec87050a506362f909b2327c25752e953080faebb41f43c2efe58148fe43da9778468bceb8268f96ae6bda9970f9383cf6ddd8aba5f41aa04da45819ef53bfe
|
7
|
+
data.tar.gz: 21e121eded72e9530548eea897e8132ca3fd79190b4f4d51ab10fbd4ac5d8b1bde36a6d52abbf5e020acdf13663ba81b9df93ec9c87d60e92386a79fdb5528c2
|
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
A Sidekiq middleware for supports ActiveRecord Shard with [ActiveSupport:CurrentAttribute](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html).
|
4
4
|
|
5
|
-
>
|
5
|
+
> **Warning**
|
6
|
+
> This gem can work with [sidekiq-cron](https://github.com/ondrejbartas/sidekiq-cron) or other schedulers, because when Schedule perform a job, they can't know the which shard to use.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -19,21 +20,6 @@ $ bundle
|
|
19
20
|
|
20
21
|
## Usage
|
21
22
|
|
22
|
-
Add follow code into `config/initializers/sidekiq-activerecord-shard.rb`:
|
23
|
-
|
24
|
-
```rb
|
25
|
-
SidekiqActiveRecordShard.configure do
|
26
|
-
self.selected_shard = -> do
|
27
|
-
case Current.tenant_id
|
28
|
-
when "other"
|
29
|
-
:other
|
30
|
-
else
|
31
|
-
:primary
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
```
|
36
|
-
|
37
23
|
Create `app/models/current.rb`:
|
38
24
|
|
39
25
|
```rb
|
@@ -54,11 +40,31 @@ class ApplicationController < ActionController::Base
|
|
54
40
|
end
|
55
41
|
```
|
56
42
|
|
43
|
+
Add sidekiq config `config/initializers/sidekiq-activerecord-shard.rb`:
|
44
|
+
|
45
|
+
```rb
|
46
|
+
# Enable Sidekiq CurrentAttributes middleware for store Current
|
47
|
+
require "sidekiq/middleware/current_attributes"
|
48
|
+
Sidekiq::CurrentAttributes.persist(Current)
|
49
|
+
|
50
|
+
SidekiqActiveRecordShard.configure do
|
51
|
+
self.selected_shard = -> do
|
52
|
+
case Current.tenant_id
|
53
|
+
when "other"
|
54
|
+
:other
|
55
|
+
else
|
56
|
+
:primary
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
|
57
63
|
### Perform Job by set shared
|
58
64
|
|
59
65
|
Some times, you want to perform a job without Request context, or start Job in schedulers.
|
60
66
|
|
61
|
-
Now use can use `set(shard: "
|
67
|
+
Now use can use `set(shard: "shard_name")` to set shared in directly.
|
62
68
|
|
63
69
|
```rb
|
64
70
|
# Call job with "other" shard db
|
@@ -2,7 +2,9 @@ require "sidekiq"
|
|
2
2
|
|
3
3
|
module SidekiqActiveRecordShard
|
4
4
|
class Client
|
5
|
-
|
5
|
+
if Sidekiq::VERSION >= "6.5"
|
6
|
+
include Sidekiq::ClientMiddleware
|
7
|
+
end
|
6
8
|
|
7
9
|
def call(_jobclass, job, _queue, _redis)
|
8
10
|
# Store shard value in Job arguments
|
@@ -13,7 +15,10 @@ module SidekiqActiveRecordShard
|
|
13
15
|
end
|
14
16
|
|
15
17
|
class Server
|
16
|
-
|
18
|
+
if Sidekiq::VERSION >= "6.5"
|
19
|
+
include Sidekiq::ServerMiddleware
|
20
|
+
end
|
21
|
+
|
17
22
|
def call(_jobclass, job, _queue, &block)
|
18
23
|
if job["shard"].nil?
|
19
24
|
yield
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-activerecord-shard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-06-
|
11
|
+
date: 2022-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|