2018-02-24 18:16:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-28 15:47:34 +00:00
|
|
|
class Mastodon::SidekiqMiddleware
|
2020-05-10 08:30:27 +00:00
|
|
|
BACKTRACE_LIMIT = 3
|
|
|
|
|
2023-03-12 22:47:55 +00:00
|
|
|
def call(*, &block)
|
|
|
|
Chewy.strategy(:mastodon, &block)
|
2019-07-01 23:01:17 +00:00
|
|
|
rescue Mastodon::HostValidationError
|
2018-02-24 18:16:11 +00:00
|
|
|
# Do not retry
|
2020-05-10 08:30:27 +00:00
|
|
|
rescue => e
|
|
|
|
limit_backtrace_and_raise(e)
|
2019-07-01 23:01:17 +00:00
|
|
|
ensure
|
2022-04-28 15:47:34 +00:00
|
|
|
clean_up_sockets!
|
2018-02-24 18:16:11 +00:00
|
|
|
end
|
2020-05-10 08:30:27 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-26 23:47:20 +00:00
|
|
|
def limit_backtrace_and_raise(exception)
|
2023-09-06 07:18:10 +00:00
|
|
|
exception.set_backtrace(exception.backtrace.first(BACKTRACE_LIMIT)) unless ENV['BACKTRACE']
|
2021-12-26 23:47:20 +00:00
|
|
|
raise exception
|
2020-05-10 08:30:27 +00:00
|
|
|
end
|
2022-04-28 15:47:34 +00:00
|
|
|
|
|
|
|
def clean_up_sockets!
|
|
|
|
clean_up_redis_socket!
|
|
|
|
clean_up_statsd_socket!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_redis_socket!
|
2022-04-29 20:43:07 +00:00
|
|
|
RedisConfiguration.pool.checkin if Thread.current[:redis]
|
2022-04-28 15:47:34 +00:00
|
|
|
Thread.current[:redis] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_statsd_socket!
|
|
|
|
Thread.current[:statsd_socket]&.close
|
|
|
|
Thread.current[:statsd_socket] = nil
|
|
|
|
end
|
2018-02-24 18:16:11 +00:00
|
|
|
end
|