2012-11-16 30 views
0

我正在尝试使用使特定控制器的操作获得HTTP GET的Heroku计划任务。下面是任务的样子:Heroku使用Net :: HTTP库进行HTTP GET请求的计划任务 - Ruby on Rails

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)>' 

有什么想法?

在此先感谢

+0

要在Heroku上运行rake我认为你应该从命令行调用'heroku run rake db:migrate'等。 –

回答

0

我决定用“HTTParty”库,它已采取的照顾它:

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

一个干净的解决方案可能是把请求逻辑的一类方法中您在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的亚军方式与Heroku的调度程序插件的请求(你在Rails应用程序的上下文中执行任何命令,让我们)

rails runner RequestWorker.retrieve_testapp 

实际执行的来自Ruby/Rails的HTTP请求有a lot of useful gems。好的是ExconHTTP Client