繁体   English   中英

使用Net :: HTTP库发出HTTP GET请求的Heroku计划任务-Ruby on Rails

[英]Heroku Scheduled Task that uses Net::HTTP library to make HTTP GET request - Ruby on Rails

我正在尝试使用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应用目录中任何位置创建的特殊Class的class方法中。

# 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请求,有很多有用的工具 不错的是ExconHTTP Client

我决定使用已经处理过的“ HTTParty”库:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM