sidekiq-activerecord-shard 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 758bc7c30968cfc3961024118a4e4dd4f0bbefd4d99995ad7465f05691bac4eb
4
- data.tar.gz: ae47127827bf9933229f22a3e104b698e2591e79acbba39e929b2515220bdb98
3
+ metadata.gz: bc3fd046c8730189dc8afc6549c1182e4ae0777e5dc686ad50765c2e0f577945
4
+ data.tar.gz: 570c63c2c849cf1e563da69ffd40cc4079d4a3cace6919e81aa97819ea45dada
5
5
  SHA512:
6
- metadata.gz: 17fcce8d7723e936f85bc1a8d15448dc7ce89035f29cfc0f1f0d9484bae5793adf66d01c05dc2e00af36e576d1a01e12810ad103d75215d3d269a7be79b59248
7
- data.tar.gz: fafdc2936270b3bc3237baa794559632977c304c69c01ff8057d5c6ea0379c57df8771cf9a0d9e7f32cc28c16b5ab290167e76e03192ff8ac2c46d04813d3500
6
+ metadata.gz: 6ec87050a506362f909b2327c25752e953080faebb41f43c2efe58148fe43da9778468bceb8268f96ae6bda9970f9383cf6ddd8aba5f41aa04da45819ef53bfe
7
+ data.tar.gz: 21e121eded72e9530548eea897e8132ca3fd79190b4f4d51ab10fbd4ac5d8b1bde36a6d52abbf5e020acdf13663ba81b9df93ec9c87d60e92386a79fdb5528c2
data/README.md CHANGED
@@ -2,6 +2,9 @@
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
+ > **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.
7
+
5
8
  ## Installation
6
9
 
7
10
  Add this line to your application's Gemfile:
@@ -17,9 +20,33 @@ $ bundle
17
20
 
18
21
  ## Usage
19
22
 
20
- Add follow code into `config/initializers/sidekiq-activerecord-shard.rb`:
23
+ Create `app/models/current.rb`:
24
+
25
+ ```rb
26
+ class Current < ActiveSupport::CurrentAttributes
27
+ attribute :tenant_id
28
+ end
29
+ ```
30
+
31
+ Set `Current.tenant_id` on ApplicationController:
32
+
33
+ ```rb
34
+ class ApplicationController < ActionController::Base
35
+ before_action :set_current_tenant_id
36
+
37
+ def set_current_tenant_id
38
+ Current.tenant_id = request.headers["x-tenant-id"]
39
+ end
40
+ end
41
+ ```
42
+
43
+ Add sidekiq config `config/initializers/sidekiq-activerecord-shard.rb`:
21
44
 
22
45
  ```rb
46
+ # Enable Sidekiq CurrentAttributes middleware for store Current
47
+ require "sidekiq/middleware/current_attributes"
48
+ Sidekiq::CurrentAttributes.persist(Current)
49
+
23
50
  SidekiqActiveRecordShard.configure do
24
51
  self.selected_shard = -> do
25
52
  case Current.tenant_id
@@ -32,24 +59,19 @@ SidekiqActiveRecordShard.configure do
32
59
  end
33
60
  ```
34
61
 
35
- Create `app/models/current.rb`:
36
62
 
37
- ```rb
38
- class Current < ActiveSupport::CurrentAttributes
39
- attribute :tenant_id
40
- end
41
- ```
63
+ ### Perform Job by set shared
42
64
 
43
- Set `Current.tenant_id` on ApplicationController:
65
+ Some times, you want to perform a job without Request context, or start Job in schedulers.
66
+
67
+ Now use can use `set(shard: "shard_name")` to set shared in directly.
44
68
 
45
69
  ```rb
46
- class ApplicationController < ActionController::Base
47
- before_action :set_current_tenant_id
70
+ # Call job with "other" shard db
71
+ MyJob.set(shard: "other").perform_later
48
72
 
49
- def set_current_tenant_id
50
- Current.tenant_id = request.headers["x-tenant-id"]
51
- end
52
- end
73
+ # Call job with "primary" shard db
74
+ MyJob.set(shard: "primary").perform_later
53
75
  ```
54
76
 
55
77
  ## Contributing
@@ -2,24 +2,34 @@ require "sidekiq"
2
2
 
3
3
  module SidekiqActiveRecordShard
4
4
  class Client
5
- include Sidekiq::ClientMiddleware
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
9
- job["_active_record_shard"] = SidekiqActiveRecordShard.config.selected_shard.call
11
+ job["shard"] ||= SidekiqActiveRecordShard.config.selected_shard.call
12
+
10
13
  yield
11
14
  end
12
15
  end
13
16
 
14
17
  class Server
15
- include Sidekiq::ServerMiddleware
18
+ if Sidekiq::VERSION >= "6.5"
19
+ include Sidekiq::ServerMiddleware
20
+ end
21
+
16
22
  def call(_jobclass, job, _queue, &block)
17
- set_shard(job["_active_record_shard"], &block)
23
+ if job["shard"].nil?
24
+ yield
25
+ else
26
+ set_shard(job["shard"], &block)
27
+ end
18
28
  end
19
29
 
20
30
  # Inspired by ActiveRecord::Middleware::ShardSelector
21
31
  # https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/middleware/shard_selector.rb#L54
22
- def set_shard(shared, &block)
32
+ def set_shard(shard, &block)
23
33
  options = Rails.application.config.active_record.shard_selector
24
34
 
25
35
  ActiveRecord::Base.connected_to(shard: shard.to_sym) do
@@ -1,3 +1,3 @@
1
1
  module SidekiqActiveRecordShard
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.4"
3
3
  end
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.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-15 00:00:00.000000000 Z
11
+ date: 2022-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails