1

I am giving users an option to receive, say, emails at a periodicity of their choice, for example:

Every day at 5am

or

Every Friday at 11pm

or even

Every second Monday of the month at 12am

What would be my best choice for storing this kind of data in order to feed it to Whenever gem?

Update

I was able to solve the question with @mudasobwa 's advice in combination with the cron_parser gem. The process flow is:

  • Get user's input from a select box on the frontend
  • Parse it on the backend and de-code into cron format (e.g. 7 would mean that I will need to send emails every week, so the code could be "1 1 * * 1"), store the value as periodicity.
  • Use the cron_parser gem to generate the next_run dateformat which is used to tell whenever gem which jobs it needs to perform that are dated.
The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85

1 Answers1

2

whenever gem accepts a raw cron syntax. That is why the simplest way, involving as few transformations as possible, would be to store values in raw cron format.

That said, you probably should have a parser from/to allowed user input to/from raw cron format and that’s it.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • yes in theory that's pretty clear but what should I store within the database? My process is that whenever checks every hour for recipients that had yet to receive their messages (so it looks for outdated messages) and sends them away. Those messages have a column called next_send which stores the date/time when the message should be sent out. But to get the date in that column I need to parse it from the initial user input. – The Whiz of Oz Dec 12 '16 at 10:18
  • Within the database you should store **strings in raw cron format** as stated in my answer twice. Smth like `"0 0 27-31 * *"`. – Aleksei Matiushkin Dec 12 '16 at 10:21
  • Thanks, so in a nutshell I would store this raw cron data and parse it in order to get the next_run date and set it on my model? – The Whiz of Oz Dec 12 '16 at 10:25
  • Well, I can’t use a wording “you do,” but yes, if I was to implement this, I would store the raw cron data and parse it to retrieve the human-readable values, like date and time. – Aleksei Matiushkin Dec 12 '16 at 10:26
  • Welcome, не за что. – Aleksei Matiushkin Dec 12 '16 at 10:30