sidekiq-activerecord-shard 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/sidekiq-activerecord-shard/middleware.rb +6 -6
- 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:
|
@@ -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.
|
@@ -6,12 +6,8 @@ module SidekiqActiveRecordShard
|
|
6
6
|
|
7
7
|
def call(_jobclass, job, _queue, _redis)
|
8
8
|
# Store shard value in Job arguments
|
9
|
-
shard
|
10
|
-
if shard.nil?
|
11
|
-
raise "No shard selected, SidekiqActiveRecordShard.config.selected_shard return nil."
|
12
|
-
end
|
9
|
+
job["shard"] ||= SidekiqActiveRecordShard.config.selected_shard.call
|
13
10
|
|
14
|
-
job["_active_record_shard"] = shard
|
15
11
|
yield
|
16
12
|
end
|
17
13
|
end
|
@@ -19,7 +15,11 @@ module SidekiqActiveRecordShard
|
|
19
15
|
class Server
|
20
16
|
include Sidekiq::ServerMiddleware
|
21
17
|
def call(_jobclass, job, _queue, &block)
|
22
|
-
|
18
|
+
if job["shard"].nil?
|
19
|
+
yield
|
20
|
+
else
|
21
|
+
set_shard(job["shard"], &block)
|
22
|
+
end
|
23
23
|
end
|
24
24
|
|
25
25
|
# Inspired by ActiveRecord::Middleware::ShardSelector
|