Rails - 每当 gem - 动态值
假设我有一个像这样的 cronjob:
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
尽管如此,我想让“1.day”更加动态,例如通过管理页面中的表单更改值。
然后它可能看起来像这样:
every Constant.find(2).value, :at => '4:30 am' do
或者
@const = Constant.find(2)
every @const.span, :at => @const.time do
任何人都可以想出如何使这项工作有效的想法吗?
显然,这样做的原因是我可以使用我网站上数据库中存储的值,就像一条消息说
<%= "The next update is in less than #{@const.time}" #or something similar %>
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
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
or
@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 %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢各位闲人!完全有效。这是我的解决方案:
.value 可以包含例如: 1.day 或 :sunday
.additional_data 包含时间戳,例如:11:00am
是的,我知道我需要再次运行 --update crontab :)
但我会让 cronjob 自行更新,呵呵。
Thank you idlefingers! It totally worked. Here's my solution:
.value can contain for example: 1.day or :sunday
.additional_data contains the timestamp, example: 11:00am
And yes, I'm aware that I need to run --update crontab again :)
But I'll let a cronjob update itself, hehe.
我从未尝试过,但您应该能够通过随时加载 Rails 环境来做到这一点,以便您可以使用模型类。只需在您的 Schedule.rb 中使用
require File.dirname(__FILE__) + "./environment"
(或"./application"
for Rails 3),假设您的日程安排。 rb 位于配置目录中。但是,由于所有每当执行的操作都会在 crontab 中生成行,因此对任何
Constant
进行的任何更改都需要再次运行whenever --update-crontab
。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.However, since all whenever does is generate lines in the crontab, any changes made to any
Constant
would require runningwhenever --update-crontab
again.随时别忘记更新
Do not forget to update whenever
我有同样的任务,并决定用另一种方法来解决它。
在我的表中有很多记录,其中存储时间表的设置
(每日、每月、每周、时间)
在
schedule.rb
文件中,我添加了一个 cron 作业,该作业每天 00:00 运行并选择今天可以运行的记录。然后使用perform_at
将其添加到 sidekiq 队列中。I had the same task and decided to solve it a bit another approach.
In my table have a lot of records where storing settings for schedule
(daily, monthly, weekly, time)
In
schedule.rb
file I added a cron job, which was running every day at 00:00 and select the records, which can be run today. And after it added to queue with sidekiq usingperform_at
.