Contributte Scheduler

Executing php callbacks using cron expression.

Content

Setup

Require package

composer require contributte/scheduler
1

Register extension

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

Configuration

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.

services:
    foo: App\Model\Foo

scheduler:
    jobs:
        - cron: '* * * * *'
          callback: [@foo, echo]
        - cron: '*/2 * * * *'
          callback: App\Model\Bar::echo
1
2
3
4
5
6
7
8
9

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.

use Contributte\Scheduler\IJob;

class MyAwesomeJob implements IJob
{

	public function isDue(DateTime $dateTime): bool
	{
		if ($jobIsReadyToRun) {
			return true;
		}
		return false;
	}

	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

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