How to check for time every minutes in Rails - ruby-on-rails

Let's say I want to build a Reminder App. It allow user to set a time which will remind them to do their work. For example: 24th March 07:03 PM.
The easiest way I can think of is to use whenever gem and set a crontab every minutes and check if there is any reminder needs to be fire.
But I was wondering if there is a better way to achieve this. Or if now users want to set a reminder in seconds, what can I do? For example: 24th March 07:03:33 PM.

Another way to do this and be accurate to seconds is with a delayed background job.
For example resque_scheduler
When a user creates a reminder you enqueue a job at specific time.
reminder = Reminder.last
# assuming reminder has a column reminder_at and there is
# a job that processes these reminders, ProcessReminderJob
Resque.enqueue_at(reminder.remind_at, ProcessReminderJob, reminder.id)

Related

Repeating Local Notifications at specific intervals

Is it possible to have a notification repeat at specific intervals? For example, Remind me to do something on Monday to Friday (from 9:00am to 6:00pm)?
You can schedule local notifications for the specific time, date, weekday, and also specify to be repeated or not.
Here is a comprehensive document provided by Apple that describes how to do it.
If I'm not mistaken, You mean to schedule notification for a continuous period of time, If so, It does not make sense and it's not possible.
But you can schedule multiple notifications for different times of a weekday based on your need.

How can I create custom cron jobs past on a date picker?

I have the following feature on a web app:
For the picture above, I save the time specified on a Schedule model for some work to be performed. At the moment this works perfectly for "Simple" selections meaning that if a user selects Hourly, Weekly, Daily, or Monthly only, then I have a fleet of cron jobs that will start Hourly, Weekly, Daily, or Monthly. So just to be clear, I have 4 cron jobs already placed in my crontab that will run at the specified time and pick the corresponding schedule models.
The problem that I'm facing is the Custom feature. I'm stumped as to how I can effectively create a background task to run on custom input for a user. Here is an example scenario. Let's say I have 3 users who each select a custom time to perform some work.
User 1 selects a custom task to be done on January, February, and March only.
User 2 selects a custom task to be done on Mondays at 1, 3, 6, and 9 pm.
User 3 selects a scan to be done on Friday through Saturday every month only.
And the customization goes on and on.......
What would be an effective and feasible way of implementing this kind of behavior? For the moment, I'm stumped and haven't come up with a way to even approach this based on the requirement. From what I have read, I believe a delayed job will help me based on this stack overflow question custom job based on input but with so much variability, how would this be possible. Some help and guidance would be appreciated. Thank you.
I'd probably go with a mechanism that enqueues the job once for the next occurrence when the configuration is saved. Then, after it has been run, calculate the next occurrence and so on. Don't forget to first remove the already-scheduled run when making changes to the configuration. This can be tricky with some ActiveJob adapters which is why, in case you haven't made a decision towards a specific one yet, I'd like to recommend que. But you can do it in Sidekiq, too.
To facilitate calculation of the next occurrence, you could use, for example, the ice_cube gem.

Swift: Creating a repeating local notification between a set time each day

I am trying to create a repeating local notification that will notify the user every hour between a set time every week day (e.g. 9-5 monday-friday) but after searching cant find any documentation on how to implement this.
If you want only 5/7 it can be a problem.
When you schedule a local notification you can also set a calendar unit as -repeatInterval property. This is cool because there is an per-app limit (maybe 64) of maximum number of notification that you can schedule, thus creating one you can fire it each day at the same time.
If you need to create a different kind of repetition is easy to reach that limit, to avoid that you can recreate further notifications each time the user open the app, or if you implement interactive notifications, each time the user interact with one.
Or but I've never tried you can create a notification for each day of the week NSWeekCalendarUnit(except sat and sun) an set the repetition to weekday for each, in this way you will only spend 5 notifications, for infinite repetition.

Auto-detect time of day

I'm writing a daily to do list app, and want the app to update certain elements with each new day. This isn't a problem if the user closes the app one day and then reopens it the next -- I just compare days in the startup methods.
However, this is a problem if the user happens to be using the app exactly at midnight. At midnight, daily task elements need to reset themselves automatically for the new day.
Here's my issue -- I could have a constant thread running in the background, always accessing [NSDate date] and checking for a new day. But I feel like there must be a better way, especially because this only has to happen once per day (and then, only if the user is using the app at midnight).
Thank you in advance!
In applicationDidFinishLaunching: and applicationWillEnterForeground:, setup an NSTimer scheduled to trigger at midnight. Also, register for UIApplicationSignificantTimeChangeNotification to reschedule the timer for time zone changes etc.

Quartz.NET and daylight savings issue

We have a Quartz.NET cron trigger setup that needs to execute a job using the following schedule:
At 02:00hrs in the merchants time zone daily
However, when Daylight Savings Time (DST) occurs abnormalities occur i.e:
When the merchants timezone moves from +2 GMT to +3 GMT, the job does not get executed.
When the merchants timezone moves from +3 GMT to +2 GMT, the job does gets executed twice.
We already know that this behaviour is by design (http://www.quartz-scheduler.net/faq.html#daylight) but what are people using as a solution to this DST issue?
Cheers
Billy Stack
The link you point to may have been updated in the three years or so (!) since you asked this question. It would appear that the job should not be executed twice.
I'm unsure whether 02:00 officially 'occurs' in the US when daylight savings starts, so the concern about the job skipping execution at 02:00 could be resolved by setting it to 01:59:59 (cron trigger "59 59 1 ? *"), as long as you were in North America. Other countries may change at a different time of night. In Europe, for instance, the change is at 01:00, so there it would be better to set the trigger to 02:00:01
Just experienced the same issue as well, although in my case the trigger was not scheduled to fire between 0:00 and the DST change time (2:00 in the US). Instead, I had a job that runs MON-FRI at 8 in the morning, so it ran twice on the Monday November the 5th, the day following the DST change day.
If you read the FAQ item linked to in the question, it kinda makes sense. The Cron trigger wakes up, fires the job, then calculates next time to wake up. When it woke up on Friday at 8am, it calculated next time to run as Monday at 8am. However, by Monday morning what used to be 8am became 7am, so the trigger fired at that time. And then when it ran, the next time was "correctly" calculated as 8am, so it ran again at that time also.
Hope this helps somebody struggling to understand the issue.
Jobs run twice in the fall and don't run at all in the spring. It doesn't matter what time they are scheduled. The question was and still is, "How do you stop this from happening?" If you manually delete all jobs after the time change and before they run then add them back then it works fine. I haven't looked at it yet but thinking this could be automated. Write something that reads all scheduled jobs and adds them and deletes the old records. Whatever you write would need to run from the Windows job scheduler because it wouldn't run at all in the spring if it ran from Quartz.

Resources