我在做一个叫读书俱乐部的应用。系统里会有很多有投票的书。每个月,在每月的第一个月,我需要系统自动推广具有最高票数的书成为“每月的书”。推广一本书并确保每月只有一本书的逻辑已经实现。
book.promote!很可爱,是吧?
我有一个测试用例
Given the following books exist:
| title | author | year_published | votes | created_at |
| Lord of the Flies | William Golding | 1954 | 18 | January 12, 2010 |
| The Virgin Suicides | Jeffrey Eugenides | 1993 | 12 | February 15, 2010 |
| Island | Richard Laymon | 1991 | 6 | November 22, 2009 |
And the book "Lord of the Flies" is the current book of the month
And the date is "February 24, 2010"
Then the book "Lord of the Flies" should be the current book of the month
When the date is "March 1, 2010"
And I am on the home page
Then I should see "This Month's Book of the Month Club Book"
And I should see "The Virgin Suicides"
And the book "The Virgin Suicides" should be the current book of the month
And the book "Lord of the Flies" should not be the current book of the month
And the book "Island" should not be the current book of the month我正试着把它传过去。
所以问题是,我如何最好地实现每月一次的自动更新,该更新可以通过此场景进行测试?
Cron对我来说太草率了一点。我想要一种更便携的解决方案。
delayed_job/Resque对于这种情况来说似乎有点太重了。此外,我也不太确定如何让他们每月运行一次作业。
我只是在寻找一个简单,但健壮,可测试的解决方案。
干杯,一如既往!
发布于 2010-12-15 12:20:07
我使用delayed_job来满足这种类型的需求。
class Book
def promote
# code for the promote method..
ensure
# re-run the task in another 30 days.
promote
end
# Date.today.nextmonth.beginning_of_month will be evaluated
#when promote is called
handle_asynchronously :promote, :run_at => Proc.new {
Date.today.next_month.beginning_of_month
}
end发布于 2010-12-15 11:31:55
管理周期性任务的一个很好的方法是使用A:https://github.com/javan/whenever
我使用的是OS cron,但所有的配置都在您的rails应用程序中,所以使用它会更好。
然而,虽然它非常适合于维护类型的任务,例如,如果由于某种原因,某些任务不能准确地在一小时的最高时间运行,或者由于某种原因而被跳过,那么下一次就会解决这个问题,在您的情况下,周期性的事情是任务的一部分,这是一个更困难的调用。
也许应用程序可以“总是”问自己,当排名视图加载时,是否是每月的第一天,如果是,它将只使用适当日期范围内的选票进行计数?
要测试依赖时间的行为,请签出timecop:https://github.com/jtrupiano/timecop
发布于 2010-12-15 11:38:02
正如John开始说的,Cron/ As是这里的方式。其他后台守护进程需要一个单独的进程,该进程大部分时间都是空闲的。使用cron应该不会有任何可移植性问题,除非您担心在Windows上运行。
https://stackoverflow.com/questions/4446375
复制相似问题