sidekiq-activerecord-shard 0.1.0 → 0.1.3
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 +19 -3
- data/lib/sidekiq-activerecord-shard/middleware.rb +9 -4
- data/lib/sidekiq-activerecord-shard/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5b2f3dd951bd57abc6d8c9751398ca0d1ce7463d9349afc3471bb3d9f1314d4
|
4
|
+
data.tar.gz: a2669c8431a5cb6c45bccb44f79b8c9efecbef1ed5e90e28c8b6f3551b499484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 903d26936d8030a9a80308dd4c4b7eb135f284acfe08d853640d8a11c7b5895eaa7ce16033e1d80cb45dd38edff388166fc5605cf65b3a0293793326575120b3
|
7
|
+
data.tar.gz: 6bf6bdc902d59eebbc320ae7b84f1086a861c2786aa230664e951b52247dd5f7b88acc93b1c62ba50176d38d477941819bae4ee06e1500dc5001a8a62adeaf88
|
data/README.md
CHANGED
@@ -2,6 +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
|
+
> NOTE: This gem can work with sidekiq-cron or other schedulers, because when Schedule perform a job, it can't know the shard name.
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -23,10 +25,10 @@ Add follow code into `config/initializers/sidekiq-activerecord-shard.rb`:
|
|
23
25
|
SidekiqActiveRecordShard.configure do
|
24
26
|
self.selected_shard = -> do
|
25
27
|
case Current.tenant_id
|
26
|
-
when "
|
27
|
-
:
|
28
|
+
when "other"
|
29
|
+
:other
|
28
30
|
else
|
29
|
-
:
|
31
|
+
:primary
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
@@ -52,6 +54,20 @@ class ApplicationController < ActionController::Base
|
|
52
54
|
end
|
53
55
|
```
|
54
56
|
|
57
|
+
### Perform Job by set shared
|
58
|
+
|
59
|
+
Some times, you want to perform a job without Request context, or start Job in schedulers.
|
60
|
+
|
61
|
+
Now use can use `set(shard: "hard_name")` to set shared in directly.
|
62
|
+
|
63
|
+
```rb
|
64
|
+
# Call job with "other" shard db
|
65
|
+
MyJob.set(shard: "other").perform_later
|
66
|
+
|
67
|
+
# Call job with "primary" shard db
|
68
|
+
MyJob.set(shard: "primary").perform_later
|
69
|
+
```
|
70
|
+
|
55
71
|
## Contributing
|
56
72
|
|
57
73
|
Contribution directions go here.
|
@@ -2,11 +2,12 @@ require "sidekiq"
|
|
2
2
|
|
3
3
|
module SidekiqActiveRecordShard
|
4
4
|
class Client
|
5
|
-
include
|
5
|
+
include Sidekiq::ClientMiddleware
|
6
6
|
|
7
7
|
def call(_jobclass, job, _queue, _redis)
|
8
8
|
# Store shard value in Job arguments
|
9
|
-
job["
|
9
|
+
job["shard"] ||= SidekiqActiveRecordShard.config.selected_shard.call
|
10
|
+
|
10
11
|
yield
|
11
12
|
end
|
12
13
|
end
|
@@ -14,12 +15,16 @@ module SidekiqActiveRecordShard
|
|
14
15
|
class Server
|
15
16
|
include Sidekiq::ServerMiddleware
|
16
17
|
def call(_jobclass, job, _queue, &block)
|
17
|
-
|
18
|
+
if job["shard"].nil?
|
19
|
+
yield
|
20
|
+
else
|
21
|
+
set_shard(job["shard"], &block)
|
22
|
+
end
|
18
23
|
end
|
19
24
|
|
20
25
|
# Inspired by ActiveRecord::Middleware::ShardSelector
|
21
26
|
# https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/middleware/shard_selector.rb#L54
|
22
|
-
def set_shard(
|
27
|
+
def set_shard(shard, &block)
|
23
28
|
options = Rails.application.config.active_record.shard_selector
|
24
29
|
|
25
30
|
ActiveRecord::Base.connected_to(shard: shard.to_sym) do
|