Heroku Scheduled Task,使用 Net::HTTP 库发出 HTTP GET 请求 - Ruby on



我正在尝试使用Heroku计划任务,该任务使HTTP GET到特定控制器的操作。任务如下所示:

require 'net/http'
desc "This task is called by the Heroku scheduler add-on"
task :send_notifications => :environment do
    Net::HTTP.get("http://testapp.com", "/test/send_notifications")
end

运行时:

heroku rake send_notifications

我收到以下错误:

rake aborted!
getaddrinfo: Name or service not known
/app/lib/tasks/scheduler.rake:5:in `block in <top (required)>'

有什么想法吗?

提前致谢

一个干净的解决方案可能是将请求逻辑放在您在 Rails 应用程序目录中任何位置创建的特殊类的类方法中。

# app/workers/request_worker.rb
class RequestWorker
  # Request X from Y
  def self.retrieve_testapp
    # Do anything nescessary to build and execute the request in here
  end
end

然后,您可以通过 Rails runner 使用 Heroku Scheduler 插件调度请求(它允许您在 Rails 应用程序的上下文中执行任何命令)。

rails runner RequestWorker.retrieve_testapp

要实际执行来自Ruby/Rails的HTTP请求,有很多有用的宝石。不错的是Excon和HTTP Client。

我决定使用'HTTParty'库来处理它:

response = HTTParty.get('http://testapp.com/test/send_notifications')

最新更新