2012-11-16 43 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