How do I listen for AWS SQS messages in the background of my rails app? [closed]

限于喜欢 提交于 2019-12-24 15:27:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!