rails:whenever gem:run job every year - ruby-on-rails

How can I write cronjob using whenever gem in rails 4.0 for last day of every year or any specific day of every year.
for ex. 31st dec, 11:59 pm of every year or 15th july of every year.
i am trying this,
every 1.year do
rake "namespace:jobname"
end
but where to mention day and time?
Please help, Thanks in advance.

For 31st dec, 11:59 pm,try this
every 1.year, :at => 'December 31th 11:59pm' do
rake "namespace:jobname"
end
For 15th july,try this
every 1.year, :at => 'July 15th' do
rake "namespace:jobname"
end
OR
every '0 0 15 7 *' do
rake "namespace:jobname"
end
Source
Note: Didn't tested.

Related

Cron expression for running rake task first Thursday of every month

I have written following ruby code in rails application :
every '20 8 1-7 * 4' do
rake 'data_import:check_for_presence_of_file'
end
But above rake task is running ever day at 8:20 am GMT. Is there something wrong with expression ? I have searched internet for cron expression but I have seen conflicting information. Please help.
I'm not sure there is a way with cron.
'20 8 1-7 * 4' means the first seven days of the month and on thursdays. Instead perhaps do it the first seven days of the month and in rails check if its thursday:
every '20 8 1-7 * *' do
rake 'data_import:check_for_presence_of_file' if Date.today.thursday?
end
This is correct cron schedule for a Tuesday weekly:
20 8 * * 2
Here are more details:
https://crontab.guru/#20_8___2
In your sample you are using 1-7 which means every day.

Rails-Schedule on tuesday and friday using whenever

I am using whenever for scheduling in my rails app.
I could not find how to run my cronjobs on every tuesday and friday of the week anywhere in the gem's documentation.
This is what I am doing right now is this correct?
every [:tuesday, :friday], at: '12:00am', :roles => [:my_role] do
runner "User.notify"
end
You can use the plain crontab syntax in the schedule.rb
every '0 0 * * 2,5', :roles => [:my_role] do
runner "User.notify"
end
'0 0 * * 2,5' - “At 12:00 on Tuesday and Friday.”
It's pretty simple:
Minute Hour Day of Month Month Day of Week
(0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 0 * * 2,5
Refs for help 1, 2

Ruby Scheduling Clock Abstraction? Need 24 hour clock. Month irrelevant, Day irrelevant also

In my application I need a 24 hour clock scheduler. Month is irrelevant, Day is irrelevant also but saving objects as datetime makes it hard to compare things.
"The Morning Take" runs from 5:30am till 11am EST
"Afternoon News" runs 11am till 5pm
"Tonight" runs from 5pm till 10pm
"Wrap up" runs 10pm till 5:30am
I'd like to be able to say
FeatureScheduler.get_current_feature
=> "Tonight" if its say 6:30PM
Running into issues getting it 'right' input as a datetime always wants a day and month and year... but I really only care about scheduling around a 24 hour clock... the day messes things up when I try to see if Time.now is between 5:30am and 11am for example.
Any suggestions for a scheduler that only cares about a 24 hour clock... doesn't use day, month and year in comparisons?
Might be an elegant way to do this, but here's my take anyhow.
require 'time'
class FeatureScheduler
def self.get_current_feature
case Time.now.strftime('%R')
when '05:30'...'11:00'
'The Morning Take'
when '11:00'...'17:00'
'Afternoon News'
when '17:00'...'22:00'
'Tonight'
else
'Wrap Up'
end
end
end
FeatureScheduler.get_current_feature
#=> "Wrap Up"

Ruby On Rails, week number is not the correct one (-1)

I need the current week number, if I'm not totally mistaken it's week 51 right now? However when testing it in the console I get this.
Time.now
=> 2013-12-19 11:08:25 +0100
Time.now.strftime('%U')
=> "50"
Date.today
=> Thu, 19 Dec 2013
Date.today.strftime("%U").to_i
=> 50
Why is that?
Time.now.strftime('%V') will give you the week number according to ISO 8601.
why is that?
according to %U or %W, The days in the year before the first week are in week 0 (00..53).
with %V (as #Graeme McLean wrote), The days in the year before the first week are in the last week of
the previous year (01..53).
From here.
Hmm, I'm unsure as to why it is that way, but to get the correct one using Ruby, I use this:
require 'Date'
week_number = Date.today.cweek #=> 51

using whenever gem for scheduling a job every hour on Sunday

We are using whenever gem on our rails project and there is one task that need to be run every hour but only on the day of the sunday.
I understand that i can schedule tasks on hourly basis , by something like this:
every 1.hour do
# do something
end
and i also understand that i can schedule it for a particular time on sunday:
every :sunday, :at => "11:00pm" do
#do something
end
what i am looking for is some syntax to schedule this task for every hour on sunday.
You could do something like that
every '0 * * * 7' do
#...
end
The '0 * * * 7' part is cron syntax:
0 - minutes
first asterisk - every hour
second asterisk - every day
third asterisk - every month
7 - on sundays (7th day of the week)
EDIT - For more detailed information regarding cron tasks syntax, a very clear article can be found here: http://www.thesitewizard.com/general/set-cron-job.shtml

Resources