2012-11-16 1 views
0

특정 컨트롤러의 작업에 HTTP를 사용하도록하는 Heroku 예약 작업을 사용하려고합니다. 실행시Net :: HTTP 라이브러리를 사용하여 HTTP GET 요청을하는 Heroku 예약 된 작업 - 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)>' 

어떤 생각을?

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

헤로 쿠에서 레이크를 실행하려면 커맨드 라인에서 실제로 'heroku run rake db : migrate'등을 호출해야한다고 생각합니다. –

답변

0

사전에

덕분에 내가 알아서 한 'HTTParty'라이브러리를 사용하기로 결정 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 

그런 다음 레일 주자의 방법으로 Heroku가 스케줄러 부가 기능에 요청 예약 할 수있다 (당신이 당신의 Rails 애플리케이션의 컨텍스트에서 임의의 명령을 실행하자를.)

rails runner RequestWorker.retrieve_testapp 

실제로 실행하기 Ruby/Rails의 HTTP 요청에는 a lot of useful gems이 있습니다. 좋은 것들은 ExconHTTP Client입니다.

1

이 깨끗한 솔루션은의 클래스 메소드 내부의 요청 로직을 넣어 수 있습니다 :