-
Notifications
You must be signed in to change notification settings - Fork 41
/
database_replica.rb
36 lines (33 loc) · 1.08 KB
/
database_replica.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class DatabaseReplica
def self.read(feature_flag_key)
if Flipper.enabled?(feature_flag_key)
# read via the replica settings using Standby gem
Standby.on_standby { yield }
else
yield
end
end
# avoid connection timeouts: introduced in https://github.com/zooniverse/Panoptes/pull/3278
# allow dump workers to have unlimited query time to fetch data
# and ensure we reset the connection timeout back to default after query
def self.read_without_timeout(feature_flag_key, &block)
if Flipper.enabled?(feature_flag_key)
# read via the replica settings using Standby gem
Standby.on_standby do
execute_without_timeout(&block)
end
else
execute_without_timeout(&block)
end
end
def self.execute_without_timeout
# disable the statement timeout
ActiveRecord::Base.connection.execute('SET statement_timeout = 0')
yield
ensure
# reset back to the default for subsequent connection re-use
ActiveRecord::Base.connection.execute(
"SET statement_timeout = #{Panoptes.pg_statement_timeout}"
)
end
end