(PHP Cron) Asynchronous task scheduler without the need for extensions. Written in PHP for PHP.
Install package
composer require auguzsto/cronjob
Create the folder for the tasks, preferably at the root of your project.
mkdir tasks
Next step is to set the CRONJOB_TASKS_DIR environment variable with the absolute path of the task path.
CRONJOB_TASKS_DIR=/example/app/tasks
vendor/bin/cronjob create ExampleTask
This command will create a class in your tasks folder with a skeleton already in place.
<?php
use Auguzsto\Cronjob\TaskInterface;
use Auguzsto\Cronjob\SchedulerInterface;
class ExampleTask implements TaskInterface
{
/**
* Schedule
* $scheduler->on("* * * * *", new self);
*/
public static function toScheduler(SchedulerInterface $scheduler): void
{
$scheduler->on("0 0 * * *", new self);
}
/**
* Implement the task to be performed
*/
public static function onTask(): void
{
file_put_contents("backup.txt", "backup");
}
}toScheduler() is used to set the task's period in cron format (* * * * *).
onTask() is the method that will be executed.
vendor/bin/cronjob start
Done! Your scheduling service is up and running.
vendor/bin/cronjob stop
vendor/bin/cronjob schedules ExampleTask
vendor/bin/cronjob errors