Heroku Cron 将不会运行

发布于 2024-11-15 14:49:58 字数 1679 浏览 5 评论 0原文

所以,我在 heroku 上运行一个简单的 cron 任务。然而,它不起作用。我不断在日志中看到这个:

2011-06-15T03:21:35+00:00 heroku[web.1]: State changed from up to bouncing
2011-06-15T03:21:35+00:00 heroku[web.1]: State changed from created to starting
2011-06-15T03:21:35+00:00 heroku[slugc]: Slug compilation finished
2011-06-15T03:21:41+00:00 heroku[web.1]: Starting process with command: `thin -p 5926 -e production -R /home/heroku_rack/heroku.ru start`
2011-06-15T03:21:46+00:00 app[web.1]: >> Thin web server (v1.2.6 codename Crazy Delicious)
2011-06-15T03:21:46+00:00 app[web.1]: >> Maximum connections set to 1024
2011-06-15T03:21:46+00:00 app[web.1]: >> Listening on 0.0.0.0:5926, CTRL+C to stop
2011-06-15T03:21:47+00:00 heroku[web.1]: State changed from starting to up
2011-06-15T03:22:30+00:00 heroku[web.1]: Stopping process with SIGTERM
2011-06-15T03:22:30+00:00 app[web.1]: >> Stopping ...
2011-06-15T03:22:30+00:00 heroku[web.1]: Process exited

我的 cron.rake 文件(每 4 小时执行一次 put)

 desc "This task is called by the Heroku cron add-on"
 task :cron => :environment do
   if Time.now.hour % 4 == 0 # run every four hours
     puts "Sending email of most popular post."
     puts "done."
   end
 end

不知道为什么它不执行 put。另外,一旦这有效,我想经常从我的模型中调用函数。如果我的模型是 Micropost 并且函数是 has_fun 我可以在 cron.rake 中使用... Micropost.has_fun 来调用它吗?

任何帮助将不胜感激。 Stack Overflow 上的人们对我学习 Rails 提供了难以置信的帮助。再次感谢。

更新: 我将 put 更改为:

UserMailer.registration_confirmation().deliver

但是,这会产生相同的日志输出。我已经用actionmailer多次测试了registration_confirmation电子邮件,它们可以使用上述功能。 (电子邮件在 user_mailer.rb 中被编码为静态。

So, I'm running a simple cron task on heroku. However, it does not work. I keep getting this in the log :

2011-06-15T03:21:35+00:00 heroku[web.1]: State changed from up to bouncing
2011-06-15T03:21:35+00:00 heroku[web.1]: State changed from created to starting
2011-06-15T03:21:35+00:00 heroku[slugc]: Slug compilation finished
2011-06-15T03:21:41+00:00 heroku[web.1]: Starting process with command: `thin -p 5926 -e production -R /home/heroku_rack/heroku.ru start`
2011-06-15T03:21:46+00:00 app[web.1]: >> Thin web server (v1.2.6 codename Crazy Delicious)
2011-06-15T03:21:46+00:00 app[web.1]: >> Maximum connections set to 1024
2011-06-15T03:21:46+00:00 app[web.1]: >> Listening on 0.0.0.0:5926, CTRL+C to stop
2011-06-15T03:21:47+00:00 heroku[web.1]: State changed from starting to up
2011-06-15T03:22:30+00:00 heroku[web.1]: Stopping process with SIGTERM
2011-06-15T03:22:30+00:00 app[web.1]: >> Stopping ...
2011-06-15T03:22:30+00:00 heroku[web.1]: Process exited

My cron.rake file (just does puts every 4 hours)

 desc "This task is called by the Heroku cron add-on"
 task :cron => :environment do
   if Time.now.hour % 4 == 0 # run every four hours
     puts "Sending email of most popular post."
     puts "done."
   end
 end

Not sure why it wouldn't just do the puts. Also, once this works I want to call functions from my models every so often. If my model was Micropost and the function was has_fun could I call it with... Micropost.has_fun in the cron.rake?

Any help would be more than appreciated. The people on stack overflow have been an incredible help for me learning rails. Thanks again.

Update:
I changed the puts to:

UserMailer.registration_confirmation().deliver

However, this results in the same log output. I have tested registration_confirmation emails with actionmailer multiple times and they work with the above function. (the email is coded as static in the user_mailer.rb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2024-11-22 14:49:59

Thoughtbot 不久前发表了一篇博文,概述了一种方法使用 RSpec、Timecop 和 Bourne 在本地测试 Heroku cron 作业。

Thoughtbot made a blog post not too long ago outlining a method to test Heroku cron jobs locally using RSpec, Timecop, and Bourne.

疯狂的代价 2024-11-22 14:49:58

您应该尝试 rufus 调度程序:

安装 gem

 gem install rufus-scheduler

确保您将 gem 包含在捆绑器中,或者以任何方式包含它。

然后,您需要创建一个名为 task_scheduler.rb 的文件,并将其粘贴到您的 initializers 目录中。

 require 'rufus/scheduler'
 scheduler = Rufus::Scheduler.start_new

 scheduler.every '10m' do

      puts "Test!"      
      #do something here

 end

如果您遇到任何问题,可以查看此博客文章:

http://intridea.com/2009/2/13/dead-simple-task-scheduling-in-rails?blog=company

You should try rufus scheduler:

Install the gem

 gem install rufus-scheduler

Make sure you include the gem in bundler or however you are including it.

Then you need to create a file called task_scheduler.rb and stick this in you initializers directory.

 require 'rufus/scheduler'
 scheduler = Rufus::Scheduler.start_new

 scheduler.every '10m' do

      puts "Test!"      
      #do something here

 end

If you have any trouble you can see this blog post:

http://intridea.com/2009/2/13/dead-simple-task-scheduling-in-rails?blog=company

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文