2018-02-25 05:16:11 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-29 01:47:34 +10:00
|
|
|
class Mastodon::SidekiqMiddleware
|
2020-05-10 18:30:27 +10:00
|
|
|
BACKTRACE_LIMIT = 3
|
|
|
|
|
2023-03-13 09:47:55 +11:00
|
|
|
def call(*, &block)
|
|
|
|
Chewy.strategy(:mastodon, &block)
|
2019-07-02 09:01:17 +10:00
|
|
|
rescue Mastodon::HostValidationError
|
2018-02-25 05:16:11 +11:00
|
|
|
# Do not retry
|
2020-05-10 18:30:27 +10:00
|
|
|
rescue => e
|
2024-05-29 18:15:06 +10:00
|
|
|
clean_up_elasticsearch_connections!
|
2020-05-10 18:30:27 +10:00
|
|
|
limit_backtrace_and_raise(e)
|
2019-07-02 09:01:17 +10:00
|
|
|
ensure
|
2022-04-29 01:47:34 +10:00
|
|
|
clean_up_sockets!
|
2018-02-25 05:16:11 +11:00
|
|
|
end
|
2020-05-10 18:30:27 +10:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-27 10:47:20 +11:00
|
|
|
def limit_backtrace_and_raise(exception)
|
2023-09-06 17:18:10 +10:00
|
|
|
exception.set_backtrace(exception.backtrace.first(BACKTRACE_LIMIT)) unless ENV['BACKTRACE']
|
2021-12-27 10:47:20 +11:00
|
|
|
raise exception
|
2020-05-10 18:30:27 +10:00
|
|
|
end
|
2022-04-29 01:47:34 +10:00
|
|
|
|
|
|
|
def clean_up_sockets!
|
|
|
|
clean_up_redis_socket!
|
|
|
|
clean_up_statsd_socket!
|
|
|
|
end
|
|
|
|
|
2024-05-29 18:15:06 +10:00
|
|
|
# This is a hack to immediately free up unused Elasticsearch connections.
|
|
|
|
#
|
|
|
|
# Indeed, Chewy creates one `Elasticsearch::Client` instance per thread,
|
|
|
|
# and each such client manages its long-lasting connection to
|
|
|
|
# Elasticsearch.
|
|
|
|
#
|
|
|
|
# As far as I know, neither `chewy`, `elasticsearch-transport` or even
|
|
|
|
# `faraday` provide a reliable way to immediately close a connection, and
|
|
|
|
# rely on the underlying object to be garbage-collected instead.
|
|
|
|
#
|
|
|
|
# Furthermore, `sidekiq` creates a new thread each time a job throws an
|
|
|
|
# exception, meaning that each failure will create a new connection, and
|
|
|
|
# the old one will only be closed on full garbage collection.
|
|
|
|
def clean_up_elasticsearch_connections!
|
|
|
|
return unless Chewy.enabled? && Chewy.current[:chewy_client].present?
|
|
|
|
|
|
|
|
Chewy.client.transport.transport.connections.each do |connection|
|
|
|
|
# NOTE: This bit of code is tailored for the HTTPClient Faraday adapter
|
|
|
|
connection.connection.app.instance_variable_get(:@client)&.reset_all
|
|
|
|
end
|
|
|
|
|
|
|
|
Chewy.current.delete(:chewy_client)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-04-29 01:47:34 +10:00
|
|
|
def clean_up_redis_socket!
|
2024-09-03 00:19:55 +10:00
|
|
|
RedisConnection.pool.checkin if Thread.current[:redis]
|
2022-04-29 01:47:34 +10:00
|
|
|
Thread.current[:redis] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_statsd_socket!
|
|
|
|
Thread.current[:statsd_socket]&.close
|
|
|
|
Thread.current[:statsd_socket] = nil
|
|
|
|
end
|
2018-02-25 05:16:11 +11:00
|
|
|
end
|