2018-02-25 03:16:11 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-29 00:47:34 +09:00
|
|
|
class Mastodon::SidekiqMiddleware
|
2020-05-10 17:30:27 +09:00
|
|
|
BACKTRACE_LIMIT = 3
|
|
|
|
|
2018-02-25 03:16:11 +09:00
|
|
|
def call(*)
|
|
|
|
yield
|
2019-07-02 08:01:17 +09:00
|
|
|
rescue Mastodon::HostValidationError
|
2018-02-25 03:16:11 +09:00
|
|
|
# Do not retry
|
2020-05-10 17:30:27 +09:00
|
|
|
rescue => e
|
|
|
|
limit_backtrace_and_raise(e)
|
2019-07-02 08:01:17 +09:00
|
|
|
ensure
|
2022-04-29 00:47:34 +09:00
|
|
|
clean_up_sockets!
|
2018-02-25 03:16:11 +09:00
|
|
|
end
|
2020-05-10 17:30:27 +09:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-27 08:47:20 +09:00
|
|
|
def limit_backtrace_and_raise(exception)
|
|
|
|
exception.set_backtrace(exception.backtrace.first(BACKTRACE_LIMIT))
|
|
|
|
raise exception
|
2020-05-10 17:30:27 +09:00
|
|
|
end
|
2022-04-29 00:47:34 +09:00
|
|
|
|
|
|
|
def clean_up_sockets!
|
|
|
|
clean_up_redis_socket!
|
|
|
|
clean_up_statsd_socket!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_redis_socket!
|
|
|
|
Thread.current[:redis]&.close
|
|
|
|
Thread.current[:redis] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_statsd_socket!
|
|
|
|
Thread.current[:statsd_socket]&.close
|
|
|
|
Thread.current[:statsd_socket] = nil
|
|
|
|
end
|
2018-02-25 03:16:11 +09:00
|
|
|
end
|