我可以手动执行任务,但我不能对cron作业执行相同的操作。 我目前正在使用免费的Heroku cron工作。
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
if Time.now.hour == 11 # at 11am
require 'net/http'
uri = URI('http://pure-spring-2794.herokuapp.com/today')
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth 'usernameXXX', 'password***'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
end
end
基本上,cron作业点击“http://pure-spring-2794.herokuapp.com/today”,并执行某种方法。如果我在rails控制台上运行cron作业,那就完美了。