如何为ruby中的不同用户启用定期提醒?

时间:2015-07-06 20:42:29

标签: ruby cron ice-cube chronic

目前,我的应用的用户为单个提醒设置了一个特定的日期和时间(使用Chronic gem)。

我有一个每10分钟运行一次的cron作业,并检查提醒中的时间是否为< Time.now此时,它会发送提醒并将提醒标记为已发送。

但是,我希望他们能够指定定期提醒。客户应该可以说,"每隔一天上午10点"。

我可以使用ice_cube作为看似重复的部分。然后我使用慢性来提出开始时间,这将有白天和经常性的时间。

但我没有好办法让它反复发生,因为这些不是数据库中的单独事件。

以下是我的尝试:

```       提醒= response.body ['结果'] #array定期提醒,包括start_epoch和' via'       d {提醒}

  reminder.count.times do |i|

    schedule = IceCube::Schedule.new(now = Time.at(reminder[i]['value']['start_epoch'])) # get the initial start day and time
    schedule.add_recurrence_rule(IceCube::Rule.daily) #makes this a daily recurring, but how can I allow this to be customizable?

    if schedule.next_occurrence(Chronic.parse('today at 12:01AM')) < Time.now # Check if today's occurence has happened yet
      bot_response = BotResponse.get_bot_response(bot_client_id, reminder[i]['value']['bot_input_keyword'])

      if reminder[i]['value']['via'] == 'slack'
        slack_response = BotMessage.send_slack(reminder[i]['value']['slack_channel'], 
                                               bot_response[:bot_response])
        d {slack_response} 

      end #if
    end #if
  end #do

```

问题:

  • 有没有办法告诉我何时发送了提醒而没有将每个特定的每日提醒写入数据库?
  • 文本字符串中的用户是否有更优雅的方式来定义重复?

1 个答案:

答案 0 :(得分:0)

您是否考虑过尝试whenever gem通过cron作业实施定期任务?我认为您应该能够在schedule.rb文件中动态设置计划时间,请参阅此处的相关问题:Rails - Whenever gem - Dynamic values