diff --git a/.docs/README.md b/.docs/README.md deleted file mode 100644 index 9fcf7b6..0000000 --- a/.docs/README.md +++ /dev/null @@ -1,171 +0,0 @@ -# Contributte Scheduler - -Executing php callbacks using cron expression. - -## Content - -- [Setup](#setup) -- [Configuration](#configuration) -- [Jobs](#jobs) -- [Commands](#commands) - -## Setup - -Require package - -```bash -composer require contributte/scheduler -``` - -Register extension - -```neon -extensions: - scheduler: Contributte\Scheduler\DI\SchedulerExtension -``` - -## Configuration - -Set-up crontab. Use the `scheduler:run` command. - -``` -* * * * * php path-to-project/console scheduler:run -``` - -Optionally, you can set a temp path for storing lock files. - -```neon -scheduler: - path: '%tempDir%/scheduler' -``` - -## Jobs - -This package defines 2 types of jobs: - -- callback job -- service job - -### Callback job - -Register your callbacks under `scheduler.jobs` key. - -```neon -services: - stats: App\Model\Stats - -scheduler: - jobs: - # stats must be registered as service and have method calculate - - { cron: '* * * * *', callback: [ @stats, calculate ] } - - # monitor is class with static method echo - - { cron: '*/2 * * * *', callback: App\Model\Monitor::echo } -``` - -Be careful with cron syntax, take a look at following example. You can also validate your cron -using [crontab.guru](https://crontab.guru). - -``` - * * * * * - - - - - - - | | | | | - | | | | | - | | | | +----- 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 - -Create new class which implements `IJob` interface. - -```php -use Contributte\Scheduler\IJob; - -class ScheduledJob implements IJob -{ - - private $dateService; - - private $statisticsService; - - public function __construct($dateService, $statisticsService) { - $this->dateService = $dateService; - $this->statisticsService = $statisticsService; - } - - public function isDue(DateTime $dateTime): bool - { - if ($this->dateService->isRightTime($dateTime)) { - return true; - } - return false; - } - - public function run(): void - { - $this->statisticsService->calculate(); - } - -} - -``` - -Register your class into `config.neon` as regular services -into [nette dependency-injection container](https://doc.nette.org/en/3.0/dependency-injection). - -```neon -scheduler: - jobs: - - App\Model\ScheduledJob - - App\Model\OtherScheduledJob -``` - -You can also reference already registered service. - -```neon -services: - scheduledJob: App\Model\ScheduledJob - -scheduler: - jobs: - - @scheduledJob -``` - -### Job with inject support - -If your job class uses `inject*` methods for dependency injection, you can enable auto-injection using the `inject` option: - -```neon -scheduler: - jobs: - myJob: {class: App\Model\ScheduledJob, inject: true} -``` - -This will automatically call all `inject*` methods on the job class after instantiation. - -## Console - -This package relies on `symfony/console`, use prepared [contributte/console](https://github.com/contributte/console) -integration. - -```bash -composer require contributte/console -``` - -```neon -extensions: - console: Contributte\Console\DI\ConsoleExtension(%consoleMode%) -``` - -After that you can fire one of these commands. - -| Command | Info | -|----------------|-------------------- | -| scheduler:help | Print cron syntax. | -| scheduler:list | List all jobs. | -| scheduler:run | Run all due jobs. | -| scheduler:force-run | Force run selected scheduler job. | diff --git a/README.md b/README.md index 699ea42..a76a7d4 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,16 @@ Website 🚀 contributte.org | Contact 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte

-## Usage +Executing PHP callbacks using cron expressions. + +## Versions + +| State | Version | Branch | Nette | PHP | +|-------------|---------|----------|-------|---------| +| dev | `^0.10` | `master` | 3.2+ | `>=8.2` | +| stable | `^0.9` | `master` | 3.2+ | `>=8.2` | + +## Installation To install latest version of `contributte/scheduler` use [Composer](https://getcomposer.org). @@ -26,16 +35,158 @@ To install latest version of `contributte/scheduler` use [Composer](https://getc composer require contributte/scheduler ``` -## Documentation +Register extension. -For details on how to use this package, check out our [documentation](.docs). +```neon +extensions: + scheduler: Contributte\Scheduler\DI\SchedulerExtension +``` -## Versions +## Configuration -| State | Version | Branch | Nette | PHP | -|-------------|---------|----------|-------|---------| -| dev | `^0.10` | `master` | 3.2+ | `>=8.2` | -| stable | `^0.9` | `master` | 3.2+ | `>=8.2` | +Set up crontab. Use the `scheduler:run` command. + +``` +* * * * * php path-to-project/console scheduler:run +``` + +Optionally, you can set a temp path for storing lock files. + +```neon +scheduler: + path: '%tempDir%/scheduler' +``` + +## Jobs + +This package defines 2 types of jobs: + +- callback job +- service job + +### Callback job + +Register your callbacks under `scheduler.jobs` key. + +```neon +services: + stats: App\Model\Stats + +scheduler: + jobs: + # stats must be registered as service and have method calculate + - { cron: '* * * * *', callback: [ @stats, calculate ] } + + # monitor is class with static method echo + - { cron: '*/2 * * * *', callback: App\Model\Monitor::echo } +``` + +Be careful with cron syntax, take a look at following example. You can also validate your cron +using [crontab.guru](https://crontab.guru). + +``` + * * * * * + - - - - - + | | | | | + | | | | +----- 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 + +Create new class which implements `IJob` interface. + +```php +use Contributte\Scheduler\IJob; + +class ScheduledJob implements IJob +{ + + private $dateService; + + private $statisticsService; + + public function __construct($dateService, $statisticsService) { + $this->dateService = $dateService; + $this->statisticsService = $statisticsService; + } + + public function isDue(DateTime $dateTime): bool + { + if ($this->dateService->isRightTime($dateTime)) { + return true; + } + + return false; + } + + public function run(): void + { + $this->statisticsService->calculate(); + } + +} + +``` + +Register your class into `config.neon` as regular services +into [nette dependency-injection container](https://doc.nette.org/en/3.0/dependency-injection). + +```neon +scheduler: + jobs: + - App\Model\ScheduledJob + - App\Model\OtherScheduledJob +``` + +You can also reference already registered service. + +```neon +services: + scheduledJob: App\Model\ScheduledJob + +scheduler: + jobs: + - @scheduledJob +``` + +### Job with inject support + +If your job class uses `inject*` methods for dependency injection, you can enable auto-injection using the `inject` option: + +```neon +scheduler: + jobs: + myJob: {class: App\Model\ScheduledJob, inject: true} +``` + +This will automatically call all `inject*` methods on the job class after instantiation. + +## Console + +This package relies on `symfony/console`, use prepared [contributte/console](https://github.com/contributte/console) +integration. + +```bash +composer require contributte/console +``` + +```neon +extensions: + console: Contributte\Console\DI\ConsoleExtension(%consoleMode%) +``` + +After that you can fire one of these commands. + +| Command | Info | +|-----------------------|-------------------------| +| scheduler:help | Print cron syntax. | +| scheduler:list | List all jobs. | +| scheduler:run | Run all due jobs. | +| scheduler:force-run | Force run selected job. | ## Development