lib/good_job/notifier.rb in good_job-4.0.3 vs lib/good_job/notifier.rb in good_job-4.1.0
- old
+ new
@@ -25,10 +25,12 @@
WAIT_INTERVAL = 1
# Seconds to wait if database cannot be connected to
RECONNECT_INTERVAL = 5
# Number of consecutive connection errors before reporting an error
CONNECTION_ERRORS_REPORTING_THRESHOLD = 6
+ # Interval for emitting a noop SQL query to keep the connection alive
+ KEEPALIVE_INTERVAL = 10
# Connection errors that will wait {RECONNECT_INTERVAL} before reconnecting
CONNECTION_ERRORS = %w[
ActiveRecord::ConnectionNotEstablished
ActiveRecord::StatementInvalid
@@ -76,10 +78,11 @@
@connection_errors_count = Concurrent::AtomicFixnum.new(0)
@connection_errors_reported = Concurrent::AtomicBoolean.new(false)
@enable_listening = enable_listening
@task = nil
@capsule = capsule
+ @last_keepalive_time = Time.current
start
self.class.instances << self
end
@@ -266,9 +269,13 @@
def wait_for_notify
raw_connection = connection.raw_connection
if @enable_listening && raw_connection.respond_to?(:wait_for_notify)
raw_connection.wait_for_notify(WAIT_INTERVAL) do |channel, _pid, payload|
yield(channel, payload)
+ end
+ if Time.current - @last_keepalive_time >= KEEPALIVE_INTERVAL
+ raw_connection.async_exec("SELECT 1")
+ @last_keepalive_time = Time.current
end
elsif @enable_listening && raw_connection.respond_to?(:jdbc_connection)
raw_connection.execute_query("SELECT 1")
notifications = raw_connection.jdbc_connection.getNotifications
Array(notifications).each do |notification|