How to schedule daily job using Rufus Scheduler - ruby

I am trying to schedule a daily job using Rufus Scheduler.
Is this a correct way doing it?
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.every '1d' do
# Daily job code
end
Or should I create a cron job ?
Cannot find anything in Rufus documentation.

Is this a correct way doing it ?
Yes, it is a correct way of doing it.
Or should I create a cron job ?
It depends on what you are building.
Doing
scheduler.every '1d' do
# ...
end
will schedule now and then again in now + 24 * 3600 seconds and then again in now + 2 * 24 * 3600 seconds...
Doing
scheduler.cron '0 2 * * *' do
# ...
end
will schedule every day at 2am.
Some people prefer the certainty of having some admin jobs run while the system is less busy (hence my example choice of 2am).
The rufus-scheduler documentation is explaining both and trusting you to choose the one that fits your requirements.

Related

How to test jobs which were scheduled (each one min, cron "* * * * *"), but not wait "real" one minute (rufus-scheduler vs virtual time)?

rufus-scheduler lib allows us to schedule tasks
https://github.com/jmettraux/rufus-scheduler
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.cron '* * * * *' do
# do something every minute
end
In fact, when you want to test your app on top of rufus-scheduler you will need to sleep real 1 min which is too much for typical tests.
For example, in Reactor(Java) they provide StepVerifier.withVirtualTime in order to avoid long-running tests.`:
https://projectreactor.io/docs/core/release/reference/#_manipulating_time
https://www.baeldung.com/reactive-streams-step-verifier-test-publisher#3-testing-time-based-publishers
RxJava - https://medium.com/#vanniktech/taking-control-of-the-time-when-testing-rxjava-code-91b2e5e88bdf
Are there any options for time manipulation within tests for rufus-scheduler or Ruby itself?
Current solution
rufus-scheduler has type in, thus for verification of app skeleton I'm using in=0.001s and sleep 0.4.
Yes, it takes half-of a second in the test but this is much better than 1 minute.
Have a look at
https://github.com/travisjeffery/timecop
https://andycroll.com/ruby/replace-timecop-with-rails-time-helpers-in-rspec/
or other "time travel" tools.
I second #spickermann on "only test your own code".

Running a cron on start-up using Rufus Scheduler 2.x

I'm trying to run a cron on start-up and then midnight every day from that point.
I'm bound by Dashing to use Rufus Scheduler 2.0.24, in which I can't use 'first_in' with the cron command. The command in 3.x I want to replicate is like so...
scheduler.cron '00 00 * * *', :first_in => '0' do
I'm wondering if there is any way around this?
I found this which describes a similar issue - but this will only run the cron at the first instance of the specified allotted time and not immediately.
a plain way of doing it would be:
job =
proc do
puts "hello"
end
job.call
# run it right now
scheduler.cron('00 00 * * *', &job)
But maybe this one is more readable:
job =
scheduler.cron '00 00 * * *' do
puts 'hello'
end
job.block.call
# run it right now
scheduler.join
Thanks for posting a new question, it made everything clear. The question at Rufus Scheduler :first_in option unknown with cron is a bit different.
I know this is about rufus-scheduler 2.0.24, but I'd like to point to a new feature in 3.3.x: https://github.com/jmettraux/rufus-scheduler/issues/214 where you can do job.trigger_off_schedule and it invokes the job right now if overlap, mutex and other job options allow it.
Back to 2.0.24, the shortcut shown above has no refinement, it will run the block right now. The block might already have an instance running now, imagine you have the schedule set for "midnight every night" and you happen to restart at midnight. Hence, I think the first solution above, is best, because it triggers then schedules.

Rufus Scheduler: How to specify "on the first day of the month, at 8:00am"?

I am trying to configure a schedule that uses the rufus-scheduler gem. I want to create a "once per month" schedule that runs at a specific time. I am trying to use the every syntax for defining it. How can I write this configuration so that it runs every month, on the first day, at 8:00am?
If you read the documentation you link to, you'll notice that rufus-scheduler also has a cron method, here is how you would use it:
require 'rufus-scheduler'
s = Rufus::Scheduler.new
s.cron '0 8 1 * *' do
puts "Hello, it's 8 am on the first day of the month"
end
To answer more squarely, rufus-scheduler "every" syntax doesn't let you specify something like "8am every first day of the month".

Time based build scheduling in Teamcity

In Teamcity (version 7.1) how do I set up a build to be triggered to run every 20 mins for example?
I notice you can set up timed based schedules, for example "Run this build at 18:00 everyday" but that's not quite what I want.
You can use a Schedule trigger with cron expressions to do this.
I believe you'd need this one:
Seconds: 0
Minutes: 0,20,40
Hours: *
Day of month: *
Month: *
Day of week: ?
Year: *
(or whatever interval you desire)
TeamCity uses Quartz for scheduling, see more cron expression examples.
The cron expression you need is "0 0/20 * * * ? *"
See these cron expression examples
The the cron expression here:
Under the Build Configuration Settings
Triggers > "Add new trigger" > "Schedule Trigger"
Select "advanced (cron expression) from the "When:" dropdown
This is for TeamCity version: 2017.1.2
run every half hour
Hours set "*" - run every hour
Minutes set "0,30" - run every 0 minute and 30 minute
You can trigger builds using HTTP and so you could do what you are asking for by making such requests using your operating systems task scheduler.
http://confluence.jetbrains.com/display/TCD7/Accessing+Server+by+HTTP

Using a Rufus-Scheduler event to set more Rufus-Scheduler events

I've got a script where I am trying to use Rufus-Scheduler to create daily schedules at 1am (based around sunrise and sunset). When I run the code it seems to only run the first scheduler event and won't run any events after it. My understanding is Rufus-Scheduler is supposed to spawn a new thread with each schedule but it looks like it is blocking. Do I need to spawn the schedules off on a new thread? Do I need to create a new scheduler instance for each schedule I'm going to create? I've added a test scheduler at the bottom of the code and it doesn't get created.
Here's the portion of code which relates to rufus-scheduler
def get_todays_show()
this_year = Time.now.year
check_sunset
#the time format that comes back isn't reconginzed by rufus scheduler so recreate it with chronic
sunrise = Chronic.parse("#{#local_sunrise.to_s[0,10]} #{#local_sunrise.to_s[11,8]}" )
sunset = Chronic.parse("#{#local_sunset.to_s[0,10]} #{#local_sunset.to_s[11,8]}" )
schedule_array = create_array_from_csv
schedule_array.each do |sub_array|
earlier = Chronic.parse("#{sub_array[0]} #{this_year} 12:00:01 am")
later = Chronic.parse("#{sub_array[1]} #{this_year} 11:59:59")
range = earlier..later
if range.cover?(Time.now)
#scheduler.at sunset do
madrix_call(sub_array[2].strip)
end
#scheduler.at sunrise do
madrix_call(#off_slot)
end
end
end
end
#set up the scheduler
#scheduler = Rufus::Scheduler.start_new(:frequency => 30.0)
#run it once to handle today
get_todays_show
#the schedule to handle the future
#scheduler.cron '1 1 * * *' do
get_todays_show
p #scheduler.jobs
end
#scheduler.cron '* * * * *' do
p "test schedule - #{Time.now}"
end
#scheduler.join
Do I need to spawn the schedules off on a new thread?
No, rufus-scheduler does it for you.
Do I need to create a new scheduler instance for each schedule I'm going to create?
No, not at all.
Have you tried without setting a frequency (using rufus-scheduler's default frequency)?
Although you are not hitting a rufus-scheduler issue, please read: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html
You are not giving any detail about your environment, it's very hard to help you.
Try to iterate from small to big. Have a small schedule thinggy work and then proceed one step after the other.

Resources