Merge branch 'main' into glitch-soc/merge-upstream
This commit is contained in:
		@@ -7,6 +7,7 @@ require_relative 'cli_helper'
 | 
			
		||||
module Mastodon
 | 
			
		||||
  class FeedsCLI < Thor
 | 
			
		||||
    include CLIHelper
 | 
			
		||||
    include Redisable
 | 
			
		||||
 | 
			
		||||
    def self.exit_on_failure?
 | 
			
		||||
      true
 | 
			
		||||
@@ -51,10 +52,10 @@ module Mastodon
 | 
			
		||||
 | 
			
		||||
    desc 'clear', 'Remove all home and list feeds from Redis'
 | 
			
		||||
    def clear
 | 
			
		||||
      keys = Redis.current.keys('feed:*')
 | 
			
		||||
      keys = redis.keys('feed:*')
 | 
			
		||||
 | 
			
		||||
      Redis.current.pipelined do
 | 
			
		||||
        keys.each { |key| Redis.current.del(key) }
 | 
			
		||||
      redis.pipelined do
 | 
			
		||||
        keys.each { |key| redis.del(key) }
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      say('OK', :green)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								lib/mastodon/rack_middleware.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								lib/mastodon/rack_middleware.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Mastodon::RackMiddleware
 | 
			
		||||
  def initialize(app)
 | 
			
		||||
    @app = app
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def call(env)
 | 
			
		||||
    @app.call(env)
 | 
			
		||||
  ensure
 | 
			
		||||
    clean_up_sockets!
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
end
 | 
			
		||||
@@ -11,13 +11,15 @@ def setup_redis_env_url(prefix = nil, defaults = true)
 | 
			
		||||
  port     = ENV.fetch(prefix + 'REDIS_PORT') { 6379 if defaults }
 | 
			
		||||
  db       = ENV.fetch(prefix + 'REDIS_DB') { 0 if defaults }
 | 
			
		||||
 | 
			
		||||
  ENV[prefix + 'REDIS_URL'] = if [password, host, port, db].all?(&:nil?)
 | 
			
		||||
                                ENV['REDIS_URL']
 | 
			
		||||
                              else
 | 
			
		||||
                                Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri|
 | 
			
		||||
                                  uri.password = password if password.present?
 | 
			
		||||
                                end.normalize.to_str
 | 
			
		||||
                              end
 | 
			
		||||
  ENV[prefix + 'REDIS_URL'] = begin
 | 
			
		||||
    if [password, host, port, db].all?(&:nil?)
 | 
			
		||||
      ENV['REDIS_URL']
 | 
			
		||||
    else
 | 
			
		||||
      Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri|
 | 
			
		||||
        uri.password = password if password.present?
 | 
			
		||||
      end.normalize.to_str
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
setup_redis_env_url
 | 
			
		||||
@@ -33,6 +35,8 @@ REDIS_CACHE_PARAMS = {
 | 
			
		||||
  url: ENV['CACHE_REDIS_URL'],
 | 
			
		||||
  expires_in: 10.minutes,
 | 
			
		||||
  namespace: cache_namespace,
 | 
			
		||||
  pool_size: Sidekiq.server? ? Sidekiq.options[:concurrency] : Integer(ENV['MAX_THREADS'] || 5),
 | 
			
		||||
  pool_timeout: 5,
 | 
			
		||||
}.freeze
 | 
			
		||||
 | 
			
		||||
REDIS_SIDEKIQ_PARAMS = {
 | 
			
		||||
@@ -40,3 +44,7 @@ REDIS_SIDEKIQ_PARAMS = {
 | 
			
		||||
  url: ENV['SIDEKIQ_REDIS_URL'],
 | 
			
		||||
  namespace: sidekiq_namespace,
 | 
			
		||||
}.freeze
 | 
			
		||||
 | 
			
		||||
if Rails.env.test?
 | 
			
		||||
  ENV['REDIS_NAMESPACE'] = "mastodon_test#{ENV['TEST_ENV_NUMBER']}"
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										37
									
								
								lib/mastodon/sidekiq_middleware.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								lib/mastodon/sidekiq_middleware.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Mastodon::SidekiqMiddleware
 | 
			
		||||
  BACKTRACE_LIMIT = 3
 | 
			
		||||
 | 
			
		||||
  def call(*)
 | 
			
		||||
    yield
 | 
			
		||||
  rescue Mastodon::HostValidationError
 | 
			
		||||
    # Do not retry
 | 
			
		||||
  rescue => e
 | 
			
		||||
    limit_backtrace_and_raise(e)
 | 
			
		||||
  ensure
 | 
			
		||||
    clean_up_sockets!
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def limit_backtrace_and_raise(exception)
 | 
			
		||||
    exception.set_backtrace(exception.backtrace.first(BACKTRACE_LIMIT))
 | 
			
		||||
    raise exception
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user