Rails - 每当 gem - 动态值 [英] Rails - Whenever gem - Dynamic values

查看:18
本文介绍了Rails - 每当 gem - 动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的定时任务:

Lets say I have a cronjob like this:

every 1.day, :at => '4:30 am' do  
  runner "MyModel.task_to_run_at_four_thirty_in_the_morning"  
end  

虽然,我想让1.day"更具动态性,例如通过管理页面中的表单更改值.

Although, I would like to make '1.day' more dynamic, such as changing the value via a form in an administration page.

然后它可能看起来像这样:

Then it could look like this:

every Constant.find(2).value, :at => '4:30 am' do 

@const = Constant.find(2)
every @const.span, :at => @const.time do 

有人能想出如何使这项工作发挥作用的想法吗?

Can anyone come up with an idea on how to make this work?

显然,这样做的原因是我可以使用存储在我网站上的数据库中的值,就像一条消息说

Obviously, the reason for this would be that I could use the values stored in the database on my site, like an message saying

<%= "The next update is in less than #{@const.time}" #or something similar %>

推荐答案

我从来没有尝试过,但是您应该能够通过在任何时候加载 Rails 环境来做到这一点,以便您可以使用您的模型类.假设您的日程安排,只需在 schedule.rb 中使用 require File.dirname(__FILE__) + "./environment"(或 "./application" for Rails 3).rb 在配置目录中.

I've never tried, but you should be able to do this by loading up the Rails environment in whenever so that you can use your model classes. Just use require File.dirname(__FILE__) + "./environment" (or "./application" for Rails 3) in your schedule.rb, assuming your schedule.rb is in the config dir.

然而,由于所有时间都会在 crontab 中生成行,因此对任何 Constant 所做的任何更改都需要再次运行 whenever --update-crontab.

However, since all whenever does is generate lines in the crontab, any changes made to any Constant would require running whenever --update-crontab again.

这篇关于Rails - 每当 gem - 动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆