Laravel: Organize Your Cron Schedule In Large Application
Laravel has a very nice way to manage your cron jobs. app/Console/Kernel.php file has a schedule() method. It accepts an instance of Schedule component and every cron task can be defined here:
Then you put one line in your cron list and you are ready to go:
But while your application grows it has more and more cron jobs to manage:
Placing all of the commands in one method becomes a nightmare. Your schedule() method grows and grows, and you have to scroll it to find any command. It is impossible to figure out what your cron schedule looks like. The point is that when we have several cron tasks it is ok, to put all of them in schedule() method. But with large applications, it looks too detailed.
My approach for this kind of problem is to group all of these tasks according to their schedule. For example, some jobs run daily at a certain time, some jobs run several times a day, and others run on certain days. So it looks like we need three methods:
scheduleInDayCommands() commands that run several times a day
scheduleDailyCommands() commands that run daily
scheduleOnDayCommands() commands that run on certain days
Then we place all our commands to appropriate methods, according to their schedule. In the main schedule() method we place a methods chain of our new three methods:
Now our schedule looks much better and more organized. It is easier to find out what commands run more often than others, which of them run daily and which several times a day.
@ 2018. All rights reserved.
If you've got any feedback, comments or just want to chat you can get in
touch via Twitter or email.