Ruby/Rails - Whenever gem - Loop cron tasks

一世执手 提交于 2019-12-11 18:09:11

问题


I would like to control my cron jobs through my administration page.
Basically I have my cronjobs in my database, and I would like to create my crontab "on the fly".

Here's an example:

require "#{RAILS_ROOT}/config/environment.rb"
Cron.all.each do |cron|
  if cron.at.blank?

    every eval(cron.run_interval) do
      cron.cmd
    end

  else

    every eval(cron.run_interval), :at => cron.time do
      cron.cmd
    end

  end
end

every 1.day do
  command "whenever --update-crontab"
end

But Whenever doesn't output any of the tasks inside the loop, only the "static" one.

0 0 * * * whenever --update-crontab

How can I make Whenever 'understand' my loop?


回答1:


You probably need to move your eval statement higher up, for example:

eval <<-EVAL
  every #{cron.run_interval} do
    #{cron.cmd}
  end
EVAL

Assuming that your cron.cmd is something like 'command "ls -a"'.



来源:https://stackoverflow.com/questions/4617915/ruby-rails-whenever-gem-loop-cron-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!