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
|
# File 'lib/models/concerns/postgres_notifications_listener.rb', line 7
def listen_notifications
Rails.logger.info "#{name} listening_job started"
Thread.new do
Rails.logger.info "#{name} listening_job running on #{name.pluralize.underscore}_notices"
connection.execute "LISTEN #{name.pluralize.underscore}_notices"
loop do
Rails.logger.info "#{name} listening_job wait_for_notify"
connection.raw_connection.wait_for_notify do |event, pid, payload|
data = JSON.parse payload, symbolize_names: true
Rails.logger.info "postgres #{event.inspect}, pid: #{pid.inspect}, data: #{data.inspect}"
process_notification(data)
end
rescue PG::ConnectionBad => e
measure_exception_severity
Rails.logger.error "#{name} listening_job wait_for_notify lost connection #{e.inspect} retry #{@exception_counter}th time!"
Rails.logger.error 'Reestablish AR connection...'
ActiveRecord::Base.connection.verify!
sleep 0.1
@exception_counter.to_i < TIME_TO_DIE ? retry : raise(e)
rescue StandardError => e
measure_exception_severity
Rails.logger.error "#{name} listening_job wait_for_notify exception #{e.inspect} retry #{@exception_counter}th time!"
sleep 0.1
@exception_counter.to_i < TIME_TO_DIE ? retry : raise(e)
end
end.abort_on_exception = true
end
|