0

I'm trying to use a Heroku Scheduled Task that makes an HTTP GET to a particular controller's action. Here's what the task looks like:

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

Upon running:

heroku rake send_notifications

I get the following error:

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

Any thoughts?

Thanks in advance

dougiebuckets
  • 2,383
  • 3
  • 27
  • 37

2 Answers2

1

A clean solution might be to put the request logic inside a class method of a special Class you create anywhere in your Rails app directory.

# 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

You could then schedule the request with the Heroku Scheduler addon by way of Rails runner (which let's you execute any command in the context of your Rails application.)

rails runner RequestWorker.retrieve_testapp

To actually execute an HTTP Request from Ruby / Rails there are a lot of useful gems. Nice ones are Excon and HTTP Client.

Thomas Klemm
  • 10,678
  • 1
  • 51
  • 54
0

I decided to use the 'HTTParty' library which has taken care of it:

response = HTTParty.get('http://testapp.com/test/send_notifications')
dougiebuckets
  • 2,383
  • 3
  • 27
  • 37