【问题标题】:Rails whenever gem: each month on the 20thRails 每当 gem:每月 20 号
【发布时间】:2011-06-23 11:58:26
【问题描述】:

我在整个互联网上搜索了这个,文档并没有真正谈论每月的工作。所以我希望这里有人可以告诉我如何做到这一点。

我已经安装了when gem,我只需要知道正确的语法即可:

every :month, :on => '20th', :at => '02:00' do
  runner "Mailer.send_friend_sheet"
end

希望有人能指出我正确的方向..

谢谢!

【问题讨论】:

    标签: ruby-on-rails whenever


    【解决方案1】:

    如果您不知道如何使用 ruby​​ 语法,也可以使用原始 cron 语法。

    你想要的会是这样的:

    every '0 2 20 * *' do
      command "echo 'you can use raw cron syntax too'"
    end
    

    这里是如何使用 cron 语法的快速备忘单

    *     *     *   *    *        command to be executed
    -     -     -   -    -
    |     |     |   |    |
    |     |     |   |    +----- day of week (0 - 6) (Sunday=0)
    |     |     |   +------- month (1 - 12)
    |     |     +--------- day of month (1 - 31)
    |     +----------- hour (0 - 23)
    +------------- min (0 - 59)
    

    无耻地盗取自:http://adminschoice.com/crontab-quick-reference

    【讨论】:

      【解决方案2】:

      如果您希望它更具可读性,您也可以只解析文本中的日期。

      every 1.month, :at => 'January 20th 2:00am' do
        runner "Mailer.send_friend_sheet"
      end
      

      这也会生成0 2 20 * *

      【讨论】:

        【解决方案3】:

        据我所知,无论何时不支持 :on 选项,但您应该可以这样做

        every '0 2 20 * *' do
          runner "Mailer.send_friend_sheet"
        end
        

        '0 2 20 * *' 只是相关的 cron 语法 - 请参阅 http://www.manpagez.com/man/5/crontab/

        【讨论】:

        • 太糟糕了,没有可读的解决方案,但它会做。谢谢!
        【解决方案4】:

        更好:

        every 1.month, at: 'start of the month at 2am' do
          runner "Mailer.send_friend_sheet"
        end
        

        【讨论】:

        • 如果我需要每月的第一天会很好,但我需要第 20 天,不过语法很好。
        猜你喜欢
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 2011-06-04
        • 1970-01-01
        • 2016-04-11
        • 2014-12-18
        相关资源
        最近更新 更多