Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 0 additions & 171 deletions .docs/README.md

This file was deleted.

167 changes: 159 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,175 @@
Website 🚀 <a href="https://contributte.org">contributte.org</a> | Contact 👨🏻‍💻 <a href="https://f3l1x.io">f3l1x.io</a> | Twitter 🐦 <a href="https://twitter.com/contributte">@contributte</a>
</p>

## 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).

```bash
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

Expand Down