sidekiq_middleware.rb 808 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Mastodon::SidekiqMiddleware
  3. BACKTRACE_LIMIT = 3
  4. def call(*, &block)
  5. Chewy.strategy(:mastodon, &block)
  6. rescue Mastodon::HostValidationError
  7. # Do not retry
  8. rescue => e
  9. limit_backtrace_and_raise(e)
  10. ensure
  11. clean_up_sockets!
  12. end
  13. private
  14. def limit_backtrace_and_raise(exception)
  15. exception.set_backtrace(exception.backtrace.first(BACKTRACE_LIMIT)) unless ENV['BACKTRACE']
  16. raise exception
  17. end
  18. def clean_up_sockets!
  19. clean_up_redis_socket!
  20. clean_up_statsd_socket!
  21. end
  22. def clean_up_redis_socket!
  23. RedisConfiguration.pool.checkin if Thread.current[:redis]
  24. Thread.current[:redis] = nil
  25. end
  26. def clean_up_statsd_socket!
  27. Thread.current[:statsd_socket]&.close
  28. Thread.current[:statsd_socket] = nil
  29. end
  30. end