Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Scheduler: Run every x hours except for given times

Tags:

php

laravel

I know we can specify certain tasks for every X individual times like following will run after every two hours:

$schedule->command('catalog:update')->cron('0 */2 * * *');

However, I need to run every two hour but still be able to exclude certain times. For example, Scheduler should run my task every two hours but not between these times: 12 am to 5 am

I was thinking of putting current time check login in task itself which might work. Is there any other better or laravel-specific way of doing it ?

Thanks for the help/suggestions

like image 249
dev0010 Avatar asked Dec 26 '17 13:12

dev0010


1 Answers

Use Laravel Scheduler with hourly() and unlessBetween().

The unlessBetween method can be used to exclude the execution of a task for a period of time

An example:

->hourly()->unlessBetween('00:00', '5:00');

https://laravel.com/docs/5.5/scheduling#schedule-frequency-options

like image 175
Alexey Mezenin Avatar answered Nov 14 '22 23:11

Alexey Mezenin