I want to run a cron just once at a custom date & time entered by the user in a form. What is the best way to do this?
I found that a custom cron can be scheduled in laravel like this
->cron(‘* * * * * *’);
Run the task on a custom Cron schedule.
But I could not find the time format what the * mean.
Or much simpler, can it be done like this by adding the date and time.
->at('28/04/2020 13:00');
How can this be done?
you can easily do that with when() method
$schedule->command('command')->when(function (){
return Carbon::create(2020,4,28,13)->isPast();
});
@the_hasanov answer is the closest one but it's not complete:
Using his method will run the closure every minute after the date you want!
There is -at least- two methods to avoid this.
The first one is to store the "already run" status of the schedule somewhere: database or file and use this kind of schedule:
// Using a scheduled closure here to be explicit
// Setting is a generic model to store configuration data.
// it could be replaced by Redis or other key/value storage
$schedule->call(function() {
// Run here the wanted logic
Setting::create(['name' => 'cron_already_runned', 'value' => true]); // Store the already_runned status
})->when(function (){
$already_runned = Setting::firstWhere('name', 'cron_already_runned');
return
optional($already_runned)->value
&&
Carbon::create(2020,4,28,13)->isPast();
});
The second one is to use a double "time-condition" which could be true only once:
$schedule->command('command')->when(function (){
return
Carbon::create(2021,4,28,13)->isPast()
&&
Carbon::create(2021,4,28,14)->isFuture();
});
Using this method is not 100% safe as it could fail if the server is down when the conditions are met
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With