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

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".

Related

Check cron syntax to execute now

I do heavy automation on servers.
Biggest problem is the time-based execution of automation things.
The idea is to use a cron-syntax-style to time the executions.
So I need a way to check if a command that is combined to a cron syntax string can be executed now.
Things like:
./parser.sh 0 0 * * *
will only return OK on Midnight not on all the other minutes of a day.
Also
./parser.sh */10 0,1,2,3,4-22/4 * * *
and all combinations possible in cron syntax needs to work.
There will be several executions per day, every execution has different syntax.
Is there any kind of stuff that can do this?
It is not possible to actually create cronjobs for this.
Only can use Bash, maybe static compiled binaries, no Python nor higher languages.
Already tried https://github.com/morganhk/BashCronParse but this cannot interpret things like 1,2,3,4,5... only single numbers and */n, neither combinations.
I cannot get your question clearly. But, if you are trying to run parser.sh every minute of the day.
Use this
./parser.sh * * * * *

How to schedule daily job using Rufus Scheduler

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.

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".

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