Latest release: v0.2.1

Scheduler

Small lib for executing php callbacks using cron expression.

Configuration

Register extension.

extensions:
    scheduler: Contributte\Scheduler\DI\SchedulerExtension
1
2

Set-up crontab. Use the scheduler:run command.

* * * * * php path-to-project/console scheduler:run
1

Optionally, you can set a temp path for storing lock files.

scheduler:
    path: '%tempDir%/scheduler'
1
2

Jobs

Callback job

Set cron expression and php callback.

scheduler:
    jobs:
        - {cron: '* * * * *', callback: App\Model\Pirate::arrgghh}
        - {cron: '*/2 * * * *', callback: App\Model\Parrot::echo}
1
2
3
4

Cron expression:

*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
|    |    |    +---------- month (1 - 12)
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

Custom job

Use the IJob interface. Every job is registered as a service in the DIC, so you can use other services.


class MyAwesomeJob implements IJob
{

	/** @var Database */
	private $database;
	
	public function __construct(Database $database)
	{
		$this->database = $database;
	}

	public function isDue(DateTime $dateTime): bool
	{
		//$this->database->...
		return TRUE; // When the job is ready to run
	}

	public function run(): void
	{
		// Do something
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

And don't forget to register it.

scheduler:
    jobs:
        - App\Model\MyAwesomeJob
        myOtherJob: App\Model\MyOtherJob
1
2
3
4

Commands

Help

Print cron syntax.

scheduler:help
1

List

List all jobs.

scheduler:list
1

Run

Run all due jobs.

scheduler:run
1