distribute_reads 0.1.1 → 0.1.2

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: 96dec398d9905bde755d83b504ec450047b11128
4
- data.tar.gz: 63dc0b236f1829b9fb2df0400299a35713717766
3
+ metadata.gz: 731daba05fd408780f3548cac44ddeda0d4c0c7c
4
+ data.tar.gz: 403b13ce349e56794b0bdc3457222724f1da4f8b
5
5
  SHA512:
6
- metadata.gz: c96152f662c6fca7f75a51d2db9ab75e236ccc15f0aa3543ff73084fb509a34642422b53727f856fc11992a1ab643ca5fbda4251e1d794a634633000111cb871
7
- data.tar.gz: 3f5ac5f959d935953795fab5877e01f666fde7c6b77d0cce6b0e9e237afacf3cf57c5a56456cde598e0322a06c3c296fbb80c1d1810ab5c17115a5da070513e7
6
+ metadata.gz: 76250c6a063c2797328938254bdc0b9c18508929c11a969f9f03d05ada6fe419038d1d25b0772ba536a486f3daf2cfa58673c5ec637324d49ebcddfb04e625d5
7
+ data.tar.gz: 4e7289ee561734260874b0a4098f1f991d2ee4965132b59a2257530a3ad54d13510bf6ad3de9fb2c0498b1b9a058cd5f25b29995bc577891178dff05d32504c6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.1.2
2
+
3
+ - Raise `ArgumentError` when missing block
4
+ - Improved lag query
5
+ - Warn if returning `ActiveRecord::Relation`
6
+
1
7
  ## 0.1.1
2
8
 
3
9
  - Added method for jobs
@@ -14,7 +14,12 @@ module DistributeReads
14
14
  def self.lag
15
15
  conn = ActiveRecord::Base.connection
16
16
  if %w(PostgreSQL PostGIS).include?(conn.adapter_name)
17
- conn.execute("SELECT EXTRACT(EPOCH FROM NOW() - pg_last_xact_replay_timestamp()) AS lag").first["lag"].to_f
17
+ conn.execute(
18
+ "SELECT CASE
19
+ WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0
20
+ ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
21
+ END AS lag"
22
+ ).first["lag"].to_f
18
23
  else
19
24
  raise "Option not supported with this adapter"
20
25
  end
@@ -1,6 +1,8 @@
1
1
  module DistributeReads
2
2
  module GlobalMethods
3
3
  def distribute_reads(max_lag: nil)
4
+ raise ArgumentError, "Missing block" unless block_given?
5
+
4
6
  if max_lag && DistributeReads.lag > max_lag
5
7
  raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
6
8
  end
@@ -8,7 +10,9 @@ module DistributeReads
8
10
  previous_value = Thread.current[:distribute_reads]
9
11
  begin
10
12
  Thread.current[:distribute_reads] = true
11
- yield
13
+ value = yield
14
+ warn "[distribute_reads] Call `to_a` inside block to execute query on replica" if value.is_a?(ActiveRecord::Relation) && !previous_value
15
+ value
12
16
  ensure
13
17
  Thread.current[:distribute_reads] = previous_value
14
18
  end
@@ -1,8 +1,8 @@
1
1
  module DistributeReads
2
2
  module JobMethods
3
- def distribute_reads
3
+ def distribute_reads(max_lag: nil)
4
4
  around_perform do |job, block|
5
- distribute_reads { block.call }
5
+ distribute_reads(max_lag: max_lag) { block.call }
6
6
  end
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module DistributeReads
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-05-15 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: makara
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project:
135
- rubygems_version: 2.6.11
135
+ rubygems_version: 2.6.13
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Scale database reads with replicas in Rails