If Heroku runs cron daily, do we still need 'if time.now.hour...`? - ruby-on-rails

I'm trying to figure out how Heroku daily cron works, specifically in this way:
As per Heroku's own cron docs, cron tasks are often written like this:
if Time.now.hour == 0 # run at midnight
User.send_reminders
end
Well, what happens if I set up cron at a time other than midnight? At least from my debugging, it seems that whenever Heroku cron runs (nearly always not at midnight), the above section of code is simply ignored.
Is it good practice to eliminate the time element from cron.rake and have the simple statement User.send_reminders, to be executed whenever that document is run?

The Heroku FAQ says this:
Cron jobs execute based on when you
enable the add-on. If you enable the
hourly add-on at 9:35 in the morning,
for instance, the cron job will run at
35 minutes past the hour every hour;
if you enabled the daily add-on at the
same time, it would run every day at
9:35.
I believe this is how my daily cron jobs run, although I didn't pay too much attention to that. I don't have any time checks in my daily cron task.
The time check in the Heroku example would be useful when using hourly cron, but not when using daily.

Related

Long running schedule job

I new to ROR. Wanted to ask something for confirmation. If I run long schedule job. Will it block others schedule job? I have others job running every 5 minutes, Plan to write something that easily run more than 3 hours. Will it block the 5 minutes job?
The whenever gem is basically only a way to configure and handle Cron jobs.
That said: At the given time Cron will just start and run a configured job. Cron will not block other jobs nor it cares if a job fails or if another job is still running.
Limiting factor might be:
Memory/CPU consumption: Each job consumes memory/CPU. If there are too many jobs running at the same time your server might run out of memory or might have a high load. But this doesn't really block other jobs it just slows down the whole server.
Database locks: If your jobs perform tasks that lock database tables other queries might be blocked and need to wait. But this is not Cron specific, this depends on what your code actually does.

Want to schedule a task on every 2 hours from 8am to 12pm on heroku through scheduler add on

I want to create a task that run on every 2 hours from morning 8 to midnight. I have created task and tested locally , when i made a job through scheduler it just gives me three times daily , hourly and every 10 min. How can i customize it.
You'll have to upgrade the Heroku Scheduler Add-On to make more precise settings. With the free dyno you can only choose between these 3.
The only way to achieve this would make these cron jobs from the framework itself. With a gem like https://github.com/jmettraux/rufus-scheduler

How do I implement something like cron job on Heroku?

I know about Heroku Scheduler addon but is it very flexible like I want to be able run a task on the 1st and 15th of every month as well as at daily intervals.
What else can I use or is Scheduler the best option for Heroku?
According to my knowledge Scheduler is the best option. It is good and reliable go for it.
If you lowest frequency of running a task is it daily then config scheduler to run daily and then in the code that it runs check the day of the month and perform your desired tasks whether it be day 1, day 15 or day x.
If that doesn't suit then you'd need to look at one of the background processors like Sidekiq, DelayedJob etc that allowed scheduled jobs and then have your jobs requeue themselves at whatever frequency you want but you'll need to be running a worker for this to work in a similar fashion to scheduler.

Fortnightly task on Heroku scheduler

I have the Heroku Scheduler add-on set up and I came across techniques for running a task weekly. However, I need a task that runs every other week, but I can't think of any elegant/simple way to do this.
How about setting it to run weekly on Heroku, but then in your actual task make the first step be to check if the task was already run last week, and if so abort. This would also involve storing the current date every time the task is successfully run.

Need to run a job at a specific time

I'm trying to create a job in order to send a notification to a Twilio call, therefore it is important to have a robust solution that can make sure jobs are run at a specific time (I don't think being put into a queue is accurate enough).
What is the best solution for this type of a task?
These notifications need to happen at a specific time in the call. Such as "1 minute left". Therefore it needs to be able to:
Run at arbitrary times (1:22PM or 2:45AM)
Be defined by user input (they set the time of the call)
(It would be nice if that solution could run on Heroku)
You can use Heroku cron to run jobs either daily or hourly.
Daily cron is free, hourly cron costs $3/month: http://addons.heroku.com/cron.
Typically cron runs when you first initiate it (i.e. if you set it up at 3pm, it'll run at 3pm every day), but you can change that by sending an e-mail to support#heroku.com.
To run code in a cron, add your code to a cron.rake file and check out the cron docs here.
FYI
Heroku's own samples for cron suggest doing a time check, i.e.
if Time.now.hour % 4 == 0 # run every four hours
...
But, if you are running a daily cron, the code will run at a time that is likely to fail the above conditional. So, unless you are paying for hourly cron and you only want it to run specific hours, leave out that part of their sample code and just include your own code normally.
Running at Specific Times
Try delayed_job's :run_at column, which may give you the flexibility you need to run jobs at very specific times.
Heroku Docs: http://devcenter.heroku.com/articles/delayed-job
You need to add a cronjob for that. If you are on a Linux box then you can add a cron to the crontab and specify the time at which it runs. It is very flexible. You can find the details here:
http://en.wikipedia.org/wiki/Cron
If you want to do it in a ruby way, try whenever gem:
https://github.com/javan/whenever
For the specific case that you have mentioned, I think that you should give delayed_job a try:
https://github.com/collectiveidea/delayed_job#readme
it has a run_at option where you can specify the time at which you want to run the job.
Goto cron jobs in your hosting control panel

Resources