Skip to content
Permalink
Browse files

Nette 3

  • Loading branch information...
mabar authored and f3l1x committed Jun 12, 2019
1 parent e002c40 commit 11cc942d7cd7b0bc1bae28096013229888572f9b
Showing with 55 additions and 39 deletions.
  1. +12 −16 .docs/README.md
  2. +6 −5 README.md
  3. +2 −2 composer.json
  4. +35 −16 src/DI/SchedulerExtension.php
@@ -49,11 +49,12 @@ Set cron expression and php callback.
services:
foo: App\Model\Foo
scheduler:
jobs:
- {cron: '* * * * *', callback: [@foo, echo]}}
- {cron: '*/2 * * * *', callback: App\Model\Bar::echo}
- cron: '* * * * *'
callback: [@foo, echo]
- cron: '*/2 * * * *'
callback: App\Model\Bar::echo
```

Cron expression:
@@ -73,22 +74,17 @@ Cron expression:
Use the `IJob` interface. Every job is registered as a service in the DIC, so you can use other services.

```php
use Contributte\Scheduler\IJob;
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
if ($jobIsReadyToRun) {
return true;
}
return false;
}
public function run(): void
@@ -115,23 +111,23 @@ scheduler:

Print cron syntax.

```
```bash
scheduler:help
```

### List

List all jobs.

```
```bash
scheduler:list
```

### Run

Run all due jobs.

```
```bash
scheduler:run
```

@@ -23,11 +23,12 @@ Executing php callbacks using cron expression.

## Versions

| State | Version | Branch | PHP |
|-------------|---------------|----------|----------|
| development | `^0.5` | `master` | `>= 7.1` |
| stable | `^0.4` | `master` | `>= 7.1` |
| stable | `^0.1` | `master` | `>= 5.6` |
| State | Version | Branch | Nette | PHP |
|-------------|---------|----------|-------|---------|
| dev | `^0.6` | `master` | 3.0+ | `^7.2` |
| stable | `^0.5` | `master` | 3.0+ | `^7.2` |
| stable | `^0.4` | `master` | 2.4 | `>=7.1` |
| stable | `^0.1` | `master` | 2.4 | `>=5.6` |

## Maintainers

@@ -20,8 +20,8 @@
],
"require": {
"php": "^7.2",
"contributte/di": "^0.4.0",
"dragonmantank/cron-expression": "^2.2.0",
"nette/di": "~3.0.0",
"symfony/console": "^4.2.0"
},
"require-dev": {
@@ -52,7 +52,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.5.x-dev"
"dev-master": "0.6.x-dev"
}
}
}
@@ -2,40 +2,52 @@
namespace Contributte\Scheduler\DI;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Contributte\Scheduler\CallbackJob;
use Contributte\Scheduler\Command\HelpCommand;
use Contributte\Scheduler\Command\ListCommand;
use Contributte\Scheduler\Command\RunCommand;
use Contributte\Scheduler\IScheduler;
use Contributte\Scheduler\LockingScheduler;
use Contributte\Scheduler\Scheduler;
use InvalidArgumentException;
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\DI\Statement;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;
/**
* @property-read stdClass $config
*/
class SchedulerExtension extends CompilerExtension
{
public function getConfigSchema(): Schema
{
return Expect::structure([
'path' => Expect::string('%tempDir%/scheduler'),
'jobs' => Expect::array(),
'path' => Expect::string()->nullable(),
'jobs' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
]);
}
/**
* Register services
*/
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = (array) $this->config;
$config = Helpers::expand($config, $builder->parameters);
$config = $this->config;
$definitionHelper = new ExtensionDefinitionsHelper($this->compiler);
// Scheduler
$scheduler = $builder->addDefinition($this->prefix('scheduler'))
->setFactory(LockingScheduler::class, [$config['path']]);
$schedulerDefinition = $builder->addDefinition($this->prefix('scheduler'))
->setType(IScheduler::class);
if ($config->path !== null) {
$schedulerDefinition->setFactory(LockingScheduler::class, [$config->path]);
} else {
$schedulerDefinition->setFactory(Scheduler::class);
}
// Commands
$builder->addDefinition($this->prefix('runCommand'))
@@ -49,14 +61,21 @@ public function loadConfiguration(): void
->setAutowired(false);
// Jobs
foreach ($config['jobs'] as $key => $job) {
if (is_array($job)) {
$job = new Statement(CallbackJob::class, [$job['cron'], $job['callback']]);
foreach ($config->jobs as $jobName => $jobConfig) {
if (is_array($jobConfig) && (isset($jobConfig['cron']) || isset($jobConfig['callback']))) {
if (!isset($jobConfig['cron'], $jobConfig['callback'])) {
throw new InvalidArgumentException(sprintf('Both options "callback" and "cron" of %s > jobs > %s must be configured', $this->name, $jobName));
}
$jobDefinition = new Statement(CallbackJob::class, [$jobConfig['cron'], $jobConfig['callback']]);
} else {
$job = new Statement($job);
$jobPrefix = $this->prefix('job.' . $jobName);
$jobDefinition = $definitionHelper->getDefinitionFromConfig($jobConfig, $jobPrefix);
if ($jobDefinition instanceof Definition) {
$jobDefinition->setAutowired(false);
}
}
$scheduler->addSetup('add', [$job, $key]);
$schedulerDefinition->addSetup('add', [$jobDefinition, $jobName]);
}
}

0 comments on commit 11cc942

Please sign in to comment.
You can’t perform that action at this time.