问题
I want to asynchronously poll messages in the background of my rails application. Shoryuken doesn't work because I want my rails app to listen for incoming HTTP requests too.
回答1:
Create an initializer in config/initializers
like so:
# Allows the thread to crash our app for us
Thread.abort_on_exception = true
Thread.new do
queue_url = "..."
poller = Aws::SQS::QueuePoller.new(queue_url)
poller.poll do |msg|
puts msg.body
end
end
You can process the message through a more complex job using Active Job:
create the job like so:
rails g job process_a_message
then in the poller block:
poller.poll do |msg|
ProcessAMessageJob.perform_later msg.body
end
来源:https://stackoverflow.com/questions/33482560/how-do-i-listen-for-aws-sqs-messages-in-the-background-of-my-rails-app