distribute_reads 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: b58c208898bdc32763b35155e78946bd6a2cec3f
4
- data.tar.gz: 758dbc1bd72a7d2541435f4e1bcca5d795ad230d
3
+ metadata.gz: 96dec398d9905bde755d83b504ec450047b11128
4
+ data.tar.gz: 63dc0b236f1829b9fb2df0400299a35713717766
5
5
  SHA512:
6
- metadata.gz: 3c46fd083ba3256c568e89f27c77d22a042fcbae6f56ae7a0b58f0f7f81217cff7f2ca1f189b4052a302238a7e96672bce6ff04ac3964db515be6f46c9fc9b7f
7
- data.tar.gz: 0ba5b6f61fca1d3f9825147cdcead1eb8f267df86a05c61022b7f1e4f41b6c75d6c66510ce35cd91ccccfd12a3793688788f46818d69fc7b292b70f1af607cbf
6
+ metadata.gz: c96152f662c6fca7f75a51d2db9ab75e236ccc15f0aa3543ff73084fb509a34642422b53727f856fc11992a1ab643ca5fbda4251e1d794a634633000111cb871
7
+ data.tar.gz: 3f5ac5f959d935953795fab5877e01f666fde7c6b77d0cce6b0e9e237afacf3cf57c5a56456cde598e0322a06c3c296fbb80c1d1810ab5c17115a5da070513e7
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm: 2.4.1
4
+ script: bundle exec rake test
5
+ before_script:
6
+ - psql -c 'create database distribute_reads_test_primary;' -U postgres
7
+ - psql -c 'create database distribute_reads_test_replica;' -U postgres
8
+ notifications:
9
+ email:
10
+ on_success: never
11
+ on_failure: change
12
+ gemfile:
13
+ - Gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.1.1
2
+
3
+ - Added method for jobs
4
+
1
5
  ## 0.1.0
2
6
 
3
7
  - First release
data/README.md CHANGED
@@ -4,6 +4,8 @@ Scale database reads to replicas in Rails
4
4
 
5
5
  :tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
6
6
 
7
+ [![Build Status](https://travis-ci.org/ankane/distribute_reads.svg?branch=master)](https://travis-ci.org/ankane/distribute_reads)
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application’s Gemfile:
@@ -18,13 +20,13 @@ gem 'distribute_reads'
18
20
 
19
21
  ```yml
20
22
  default: &default
21
- adapter: postgresql_makara
23
+ url: postgresql-makara:///
22
24
  makara:
23
25
  sticky: true
24
26
  connections:
25
27
  - role: master
26
28
  name: primary
27
- url: <%= ENV["PRIMARY_DATABASE_URL"] %>
29
+ url: <%= ENV["DATABASE_URL"] %>
28
30
  - name: replica
29
31
  url: <%= ENV["REPLICA_DATABASE_URL"] %>
30
32
 
@@ -54,6 +56,20 @@ distribute_reads do
54
56
  end
55
57
  ```
56
58
 
59
+ ## Jobs
60
+
61
+ Distribute all reads in a job with:
62
+
63
+ ```ruby
64
+ class TestJob < ApplicationJob
65
+ distribute_reads
66
+
67
+ def perform
68
+ # ...
69
+ end
70
+ end
71
+ ```
72
+
57
73
  ## Options
58
74
 
59
75
  Raise an error when replica lag is too high - *PostgreSQL only*
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "minitest"
27
27
  spec.add_development_dependency "pg"
28
+ spec.add_development_dependency "activejob"
28
29
  end
@@ -23,3 +23,8 @@ end
23
23
 
24
24
  Makara::Proxy.send :prepend, DistributeReads::AppropriatePool
25
25
  Object.send :include, DistributeReads::GlobalMethods
26
+
27
+ ActiveSupport.on_load(:active_job) do
28
+ require "distribute_reads/job_methods"
29
+ extend DistributeReads::JobMethods
30
+ end
@@ -2,7 +2,10 @@ module DistributeReads
2
2
  module AppropriatePool
3
3
  def _appropriate_pool(*args)
4
4
  if Thread.current[:distribute_reads]
5
- if needs_master?(*args) || @slave_pool.completely_blacklisted? || in_transaction?
5
+ if needs_master?(*args) || @slave_pool.completely_blacklisted?
6
+ stick_to_master(*args) unless DistributeReads.default_to_primary
7
+ @master_pool
8
+ elsif in_transaction?
6
9
  @master_pool
7
10
  else
8
11
  @slave_pool
@@ -1,11 +1,12 @@
1
1
  module DistributeReads
2
2
  module GlobalMethods
3
3
  def distribute_reads(max_lag: nil)
4
+ if max_lag && DistributeReads.lag > max_lag
5
+ raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
6
+ end
7
+
4
8
  previous_value = Thread.current[:distribute_reads]
5
9
  begin
6
- if max_lag && DistributeReads.lag > max_lag
7
- raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
8
- end
9
10
  Thread.current[:distribute_reads] = true
10
11
  yield
11
12
  ensure
@@ -0,0 +1,9 @@
1
+ module DistributeReads
2
+ module JobMethods
3
+ def distribute_reads
4
+ around_perform do |job, block|
5
+ distribute_reads { block.call }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module DistributeReads
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distribute_reads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-27 00:00:00.000000000 Z
11
+ date: 2017-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: makara
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activejob
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - andrew@chartkick.com
@@ -88,6 +102,7 @@ extensions: []
88
102
  extra_rdoc_files: []
89
103
  files:
90
104
  - ".gitignore"
105
+ - ".travis.yml"
91
106
  - CHANGELOG.md
92
107
  - Gemfile
93
108
  - README.md
@@ -96,6 +111,7 @@ files:
96
111
  - lib/distribute_reads.rb
97
112
  - lib/distribute_reads/appropriate_pool.rb
98
113
  - lib/distribute_reads/global_methods.rb
114
+ - lib/distribute_reads/job_methods.rb
99
115
  - lib/distribute_reads/version.rb
100
116
  homepage: https://github.com/ankane/distribute_reads
101
117
  licenses: []
@@ -116,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
132
  version: '0'
117
133
  requirements: []
118
134
  rubyforge_project:
119
- rubygems_version: 2.6.8
135
+ rubygems_version: 2.6.11
120
136
  signing_key:
121
137
  specification_version: 4
122
138
  summary: Scale database reads with replicas in Rails