0

Currently, the users for my app set a specific date and time for a single reminder (using the Chronic gem).

I have a cron job which runs every 10 minutes and checks if the time in the reminder is < Time.now at which point, it sends the reminder and marks that reminder as sent.

However, I want them to be able to specify recurring reminders. The customer should be able to say, "Every other day at 10am".

I can use ice_cube for the recurring part it seems. Then I use Chronic to come up with the start time which will have the day and recurring time.

But I don't have a good way to make it recurring since these are not separate events in the data base.

Here is what I have tried:

``` reminder = response.body['results'] #array of recurring reminders with start_epoch and 'via' d {reminder}

  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

```

Questions:

  • Is there a way to tell when a reminder has been sent without writing each specific daily reminder to a database?
  • Is there a more elegant way for the user in a text string to define the recurrence?
Satchel
  • 16,414
  • 23
  • 106
  • 192

1 Answers1

0

Have you considered trying the whenever gem to implement recurring tasks through cron jobs? I think you should be able to set the schedule times dynamically in the whenever schedule.rb file, see related issue here: Rails - Whenever gem - Dynamic values

Community
  • 1
  • 1
beaorn
  • 454
  • 3
  • 9
  • Will this work for multi users. Eg each of my customers have their own time for the recurring so it could be lots of different times and each event has a different action sent to the main service. – Satchel Jul 07 '15 at 00:45
  • This looks like it would create a separate from job. But if I have 100 customers with he same time I would want a single from job to send to them. Conversely if I had more 132 different times I wouldn't want them to be separate jobs since they could still be covered by a job every 10 minutes – Satchel Jul 07 '15 at 00:48
  • whenever would let you schedule a rake task to run every 10 minutes. Inside the rake task you can define whatever logic needed to check for all users with occurances in that 10 minute time interval in conjuction with ice_cube or however you are storing the recurring date information and execute the specific action(s) for that user. Might not be much different than what you are already doing though. – beaorn Jul 07 '15 at 15:16