orisai/nette-scheduler – planning crontab jobs within php application
- Marek Bartoš
- Nette Blogger | 1275
Just released new Orisai library, orisai/nette-scheduler.
Why?
- One line setup in crontab. If you have multiple vendor modules, multiple app
run environments and multiple apps, tracking all the cron jobs can get quite
hard. Instead we can manage all crontab jobs inside the app. And list them with
scheduler:list
command, including next running times. - Easy development. Instead of setting up crontab locally, just run
scheduler:worker
. Or run individual job withscheduler:run-job <id>
- Locking. If job takes too long to complete, it can overlap with the next run. Better not let big data transformations run twice at the same time. While regular crontab is possible to setup with locking, it is not so easily available. Also these locks usually don't work well with complex environments.
- Run evidence. With before/after events, we can easily track run times of every job and how often they fail or are skipped due to being locked.
Why not other libs?
- They don't implement all features provided by native crontab. Parallelism for fast and in-time execution. Error handling and job isolation – jobs should not interact directly or indirectly in any way. Proper cron expression evaluation.
- Well, one does it all. It is called crunz. But being a separate application, it is harder to configure. And is missing a worker command.
So, how to install scheduler?
- Install –
composer require orisai/nette-scheduler
- Register extension
extensions: orisai.scheduler: OriNette\Scheduler\DI\SchedulerExtension
- Add your jobs
orisai.scheduler: jobs: - expression: * * * * * callback: [@example.job.service, 'run']
- Configure crontab –
* * * * * cd path/to/project && php bin/console scheduler:run >> /dev/null 2>&1
Or run
scheduler:worker
(on your local machine)
Check the docs, this was just a brief overview
If you want to use it even in applications without Nette, it is built on top of orisai/scheduler
(It was really helpful to have orisai/clock during development. Testing crontab execution times is painful without frozen clock.)
Last edited by Marek Bartoš (2023-03-23 04:47)