本文介绍了如何在Rails应用程序的后台监听AWS SQS消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Rails应用程序的后台异步轮询消息. Shoryuken不起作用,因为我也希望我的Rails应用程序也监听传入的HTTP请求.

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.

推荐答案

config/initializers中创建初始化程序,如下所示:

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

您可以使用活动作业:

像这样创建作业:rails g job process_a_message

然后在轮询器块中:

    poller.poll do |msg|
      ProcessAMessageJob.perform_later msg.body
    end

这篇关于如何在Rails应用程序的后台监听AWS SQS消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:21