2020-02-17 11:21:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Setting TRACE_PG_CONNECTIONS=1 will cause all pg connections
|
|
|
|
# to be streamed to files for debugging. The filenames are formatted
|
|
|
|
# like tmp/pgtrace/{{PID}}_{{CONNECTION_OBJECT_ID}}.txt
|
|
|
|
#
|
2020-02-17 13:14:14 -05:00
|
|
|
# Setting TRACE_PG_CONNECTIONS=SIDEKIQ will only trace connections
|
|
|
|
# on in sidekiq (safer, because there will be minimal user-facing perf impact)
|
|
|
|
#
|
2020-02-17 11:21:26 -05:00
|
|
|
# Files will be automatically deleted when the connection is closed gracefully
|
|
|
|
# (e.g. when activerecord closes it after a period of inactivity)
|
|
|
|
# Files will not be automatically deleted when closed abruptly
|
|
|
|
# (e.g. terminating/restarting the app process)
|
|
|
|
#
|
|
|
|
# Warning: this could create some very large files!
|
|
|
|
|
|
|
|
if ENV["TRACE_PG_CONNECTIONS"]
|
|
|
|
PG::Connection.prepend(
|
|
|
|
Module.new do
|
|
|
|
TRACE_DIR = "tmp/pgtrace"
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super(*args).tap do
|
2020-02-17 13:14:14 -05:00
|
|
|
next if ENV["TRACE_PG_CONNECTIONS"] == "SIDEKIQ" && !Sidekiq.server?
|
2020-02-17 11:21:26 -05:00
|
|
|
FileUtils.mkdir_p(TRACE_DIR)
|
|
|
|
@trace_filename = "#{TRACE_DIR}/#{Process.pid}_#{self.object_id}.txt"
|
|
|
|
trace File.new(@trace_filename, "w")
|
2023-01-07 06:59:28 -05:00
|
|
|
end
|
2020-02-18 09:20:28 -05:00
|
|
|
@access_log_mutex = Mutex.new
|
|
|
|
@accessor_thread = nil
|
2020-02-17 11:21:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
super.tap do
|
2020-02-17 13:14:14 -05:00
|
|
|
next if ENV["TRACE_PG_CONNECTIONS"] == "SIDEKIQ" && !Sidekiq.server?
|
2020-02-17 11:21:26 -05:00
|
|
|
File.delete(@trace_filename)
|
2023-01-07 06:59:28 -05:00
|
|
|
end
|
2020-02-17 11:21:26 -05:00
|
|
|
end
|
|
|
|
|
2020-02-18 08:50:15 -05:00
|
|
|
def log_access(&blk)
|
|
|
|
@access_log_mutex.synchronize do
|
|
|
|
if !@accessor_thread.nil?
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
Rails.logger.error <<~TEXT
|
2020-02-18 08:50:15 -05:00
|
|
|
PG Clash: A connection is being accessed from two locations
|
|
|
|
|
|
|
|
#{@accessor_thread} was using the connection. Backtrace:
|
|
|
|
|
2020-02-18 10:45:44 -05:00
|
|
|
#{@accessor_thread&.backtrace&.join("\n")}
|
2020-02-18 08:50:15 -05:00
|
|
|
|
|
|
|
#{Thread.current} is now attempting to use the connection. Backtrace:
|
|
|
|
|
2020-02-18 10:45:44 -05:00
|
|
|
#{Thread.current&.backtrace&.join("\n")}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
TEXT
|
2020-02-18 20:50:26 -05:00
|
|
|
|
|
|
|
if ENV["ON_PG_CLASH"] == "byebug"
|
|
|
|
require "byebug"
|
2020-02-18 21:10:30 -05:00
|
|
|
byebug # rubocop:disable Lint/Debugger
|
2023-01-07 06:59:28 -05:00
|
|
|
end
|
2020-02-18 20:50:26 -05:00
|
|
|
end
|
2020-02-18 08:50:15 -05:00
|
|
|
@accessor_thread = Thread.current
|
|
|
|
end
|
2023-01-07 06:59:28 -05:00
|
|
|
yield
|
2020-02-18 08:50:15 -05:00
|
|
|
ensure
|
|
|
|
@access_log_mutex.synchronize { @accessor_thread = nil }
|
|
|
|
end
|
|
|
|
end,
|
2020-02-17 11:21:26 -05:00
|
|
|
)
|
2020-02-18 08:50:15 -05:00
|
|
|
|
|
|
|
class PG::Connection
|
|
|
|
LOG_ACCESS_METHODS = %i[
|
|
|
|
exec
|
|
|
|
sync_exec
|
|
|
|
async_exec
|
|
|
|
sync_exec_params
|
|
|
|
async_exec_params
|
|
|
|
sync_prepare
|
|
|
|
async_prepare
|
|
|
|
sync_exec_prepared
|
|
|
|
async_exec_prepared
|
|
|
|
]
|
|
|
|
|
|
|
|
LOG_ACCESS_METHODS.each do |method|
|
|
|
|
new_method = "#{method}_without_logging".to_sym
|
|
|
|
alias_method new_method, method
|
|
|
|
|
|
|
|
define_method(method) { |*args, &blk| log_access { send(new_method, *args, &blk) } }
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 11:21:26 -05:00
|
|
|
end
|