我们需要在Rails应用程序中使用SQS队列;为此,我们选择使用SQS QueuePoller
对标记过于宽泛的similar question的回复后,我们在初始化程序中创建了以下内容:
require 'json'
ENV['AWS_REGION'] = 'us-east-1'
if Rails.env.production?
Thread.abort_on_exception = true
Thread.new do
CUSTOM_LOGGER = [Custom Logger Defn...]
queue_url = "...."
poller = Aws::SQS::QueuePoller.new(queue_url)
poller.before_request do |stats|
CUSTOM_LOGGER.info("requests: #{stats.request_count}")
CUSTOM_LOGGER.info("messages: #{stats.received_message_count}")
CUSTOM_LOGGER.info("last-timestamp: #{stats.last_message_received_at}")
end
CUSTOM_LOGGER.info("Start polling!")
poller.poll do |msg|
CUSTOM_LOGGER.debug("Received message: "+msg.body)
begin
json_msg = JSON.parse(msg.body)
job_name = json_msg["Message"]
job_name.constantize.perform_later if defined? job_name.constantize
CUSTOM_LOGGER.debug("Submitted to delayed job: "+job_name)
rescue => e
throw :skip_delete
end
end
CUSTOM_LOGGER.info("Finished polling - exiting thread!")
ActiveRecord::Base.connection.close
end
end
这产生了奇怪的行为 - 当运行rails服务器(守护进程/生产)模式时,轮询器似乎消耗当前队列中的所有消息然后停止(没有日志输出用于线程完成轮询,只是看起来卡住)。但是,如果我们从Rails控制台运行,该线程似乎无限期地从SQS快乐地消耗。
任何特殊原因导致背景线程会在Unicorn中卡住/死掉吗?