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.
Related
In Laravel, in my Kernel, I have:
protected $commands = [
Commands\SendRenewEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('renew:emails')
->daily();
}
The said function renew:emails, works as intended if I run this manually trough Artisan.
And in my crontab I have:
* */8 * * * cd /path-to-my-project && php artisan schedule:run >> /dev/null 2>&1
I have this to run every 8th hour, instead of every minute at * * * * *, since this is live for testing, and just to ensure that the task wasnt run every minute.
So how does Laravel know, when to run the daily job on the kernal, and when does this happen?
From this setup (which seems to be the basic setup for cronjobs in Laravel, but to run every minute instead of every 8th hour), there is no logs (that I can see), and no table in DB to keep track of this.
So if I where to set my cron to * * * * *, how does Laravel know not to run the scheduled job every minute, just because I have put ->daily(); at the end of the job?
And when I have daily();, at what specific time is that? And at specific what time is hourly();?
TL;DR:
How does Laravel know not to run the same jobs again if it is not supposed to, for example with daily(); rule? Where is this information stored? How can I be certain that a job with rule daily(); wont run every minute if my cronjobs std:out's php artisan schedule:run every minute?
Under the hood, the Laravel Scheduler uses https://github.com/dragonmantank/cron-expression to determine if a command or job is scheduled to run at the given minute the schedule:run is called.
Each task you schedule translates to a cron expression, which is then passed into the package. A method called isDue is then run against that expression to determine whether or not it should run. So, if you set a task to run hourly, then isDue will yield true at the top of the hour, and Laravel will execute to the task within the cron cycle.
As such, the information does not need to be stored anywhere, as determination is done on the fly.
This might also lead you to wonder what might happen if you have a long-running task that might take longer than the interval. This is where withoutOverlapping comes into the picture. When called, it creates what is known as a mutex, which is similar to a 'lock' of sorts (see What is a mutex? for more information), when the task is initially run. If a mutex already exists for a particular task on subsquent cycles, it means that task is currently running in another cycle, and should not be triggered again in this one.
Where are mutexes stored? Simple: Laravel stores them in a cache, and when a mutexed task is finished, the mutex is removed from the cache. And so the cycle continues.
I could go into much further detail here, but I think this answers your question for the most part.
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".
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.
I am looking at https://github.com/jmettraux/rufus-scheduler which nicely allows me to schedule and chain events.
But I want the events to stop after, say, the 10th occurrence or after the 24 days.
How do I do this?
My case would be:
run a script which creates the recurring jobs based on intervals and then stops after a given date or occurrence.
This is what I have done.
def run_schedule(url, count, method, interval)
puts "running scheduler"
scheduler = Rufus::Scheduler.new
scheduler.every interval do
binding.pry
attack_loop(url, count, method)
end
end
I am testing my site and want the attack_loop to be scheduled in memory to run against the interval.
But it appears it never hits the binding.pry line.
Normally these schedulers are running via cron jobs. Then the problem with your requirement is cron job doesn't know whether you hit the 10th occurrence or the 24 days as it doesnt keep a track. One possible solution would be to create a separate table to update the cron job details.
I'm thinking a table like,
scheduler_details
- id
- occurrence_count
- created_date
- updated_date
_ scheduler_type
So, now when you run a script, you can create or update the details. (You can search the script by scheduler_type, that way
you can check the number of occurrences
with created date, you can calculate the 24 days
HTH
Good day, you can specify the number of times a job should run:
https://github.com/jmettraux/rufus-scheduler/#times--nb-of-times-before-auto-unscheduling
If you need a more elaborate stop condition, you could do:
scheduler.every interval do |job|
if Time.now > some_max_date
job.unschedule
else
attack_loop(url, count, method)
end
end
But it appears it never hits the binding.pry line.
What has it to do with your issue title? Are you mixing issues?
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.