Skip to content

Commit

Permalink
Merge pull request #65 from job-runner/remove-php-job
Browse files Browse the repository at this point in the history
Remove php job
  • Loading branch information
fezfez committed Oct 14, 2022
2 parents cfb135d + 84f9070 commit 2a91192
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 123 deletions.
20 changes: 14 additions & 6 deletions README.md
Expand Up @@ -26,7 +26,6 @@ This cron job manager is inspired by https://github.com/jobbyphp/jobby but with
declare(strict_types=1);

use JobRunner\JobRunner\Job\CliJob;
use JobRunner\JobRunner\Job\PhpJob;
use JobRunner\JobRunner\Job\JobList;
use JobRunner\JobRunner\CronJobRunner;

Expand All @@ -35,10 +34,23 @@ require 'vendor/autoload.php';

$jobList = new JobList();
$jobList->push(new CliJob('php ' . __DIR__ . '/tutu.php', '* * * * *'));
$jobList->push(new PhpJob('<?php echo "yo";', '* * * * *', 'php job'));
$jobList->push(new CliJob('php ' . __DIR__ . '/tutu2.php', '* * * * *'));

CronJobRunner::create()->run($jobList);

// or

CronJobRunner::create()->run(JobList::fromArray([
[
'command' => 'php ' . __DIR__ . '/tutu.php',
'cronExpression' => '* * * * *',
],[
[
'command' => 'php ' . __DIR__ . '/tutu2.php',
'cronExpression' => '* * * * *',
]
]));

````

# Using you own locker storage
Expand All @@ -49,7 +61,6 @@ CronJobRunner::create()->run($jobList);
declare(strict_types=1);

use JobRunner\JobRunner\Job\CliJob;
use JobRunner\JobRunner\Job\PhpJob;
use JobRunner\JobRunner\Job\JobList;
use JobRunner\JobRunner\CronJobRunner;

Expand All @@ -59,7 +70,6 @@ require 'vendor/autoload.php';
$mySymfonyLockerStore = new \Symfony\Component\Lock\Store\MongoDbStore();
$jobList = new JobList();
$jobList->push(new CliJob('php ' . __DIR__ . '/tutu.php', '* * * * *'));
$jobList->push(new PhpJob('<?php echo "yo";', '* * * * *', 'php job'));

CronJobRunner::create()->withPersistingStore($mySymfonyLockerStore)->run($jobList);

Expand All @@ -85,7 +95,6 @@ there is various of event you can listen
declare(strict_types=1);

use JobRunner\JobRunner\Job\CliJob;
use JobRunner\JobRunner\Job\PhpJob;
use JobRunner\JobRunner\Job\JobList;
use JobRunner\JobRunner\CronJobRunner;
use JobRunner\JobRunner\PsrLog\PsrLogEventListener;
Expand All @@ -96,7 +105,6 @@ require 'vendor/autoload.php';
$myLogger = new \Psr\Log\NullLogger();
$jobList = new JobList();
$jobList->push(new CliJob('php ' . __DIR__ . '/tutu.php', '* * * * *'));
$jobList->push(new PhpJob('<?php echo "yo";', '* * * * *', 'php job'));

CronJobRunner::create()->withEventListener(new PsrLogEventListener($myLogger));->run($jobList);

Expand Down
22 changes: 17 additions & 5 deletions src/Job/CliJob.php
Expand Up @@ -6,18 +6,20 @@

use Symfony\Component\Process\Process;

use function array_key_exists;

class CliJob implements Job
{
private string $name;
private const TTL_DEFAULT_VALUE = 300;
private const AUTO_RELEASE_DEFAULT_VALUE = true;

public function __construct(
private readonly string $command,
private readonly string $cronExpression,
string|null $name = null,
private readonly int $ttl = 300,
private readonly bool $autoRelease = true,
private readonly string $name,
private readonly int $ttl = self::TTL_DEFAULT_VALUE,
private readonly bool $autoRelease = self::AUTO_RELEASE_DEFAULT_VALUE,
) {
$this->name = $name ?? $command;
}

public function getProcess(): Process
Expand All @@ -44,4 +46,14 @@ public function isAutoRelease(): bool
{
return $this->autoRelease;
}

/** @inheritDoc */
public static function fromArray(array $job): self
{
$name = array_key_exists('name', $job) ? $job['name'] : $job['command'];
$ttl = array_key_exists('ttl', $job) ? $job['ttl'] : self::TTL_DEFAULT_VALUE;
$autoRelease = array_key_exists('autoRelease', $job) ? $job['autoRelease'] : self::AUTO_RELEASE_DEFAULT_VALUE;

return new self($job['command'], $job['cronExpression'], $name, $ttl, $autoRelease);
}
}
3 changes: 3 additions & 0 deletions src/Job/Job.php
Expand Up @@ -17,4 +17,7 @@ public function getName(): string;
public function getTtl(): int;

public function isAutoRelease(): bool;

/** @param array{command : string, cronExpression : string, name? : string, ttl? : int, autoRelease? : bool} $job */
public static function fromArray(array $job): self;
}
7 changes: 7 additions & 0 deletions src/Job/JobList.php
Expand Up @@ -7,6 +7,7 @@
use JobRunner\JobRunner\Exceptions\DuplicateJob;

use function array_key_exists;
use function array_map;
use function array_values;

class JobList
Expand Down Expand Up @@ -37,4 +38,10 @@ public function getList(): array
{
return array_values($this->jobs);
}

/** @param array<array-key, array{command : string, cronExpression : string, name? : string, ttl? : int, autoRelease? : bool}> $jobs */
public static function fromArray(array $jobs): self
{
return new self(...array_map(static fn (array $job) => CliJob::fromArray($job), $jobs));
}
}
45 changes: 0 additions & 45 deletions src/Job/PhpJob.php

This file was deleted.

25 changes: 0 additions & 25 deletions tests/E2e/TestPhpJob.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Unit/Job/CliJobTest.php
Expand Up @@ -31,4 +31,20 @@ public function testOkWithValue(): void
self::assertSame(20, $sUT->getTtl());
self::assertFalse($sUT->isAutoRelease());
}

public function testFromArray(): void
{
$sUT = CliJob::fromArray([
'command' => 'toto',
'cronExpression' => 'hello',
'name' => 'myName',
'ttl' => 100,
'autoRelease' => true,
]);

self::assertSame('hello', $sUT->getCronExpression());
self::assertSame('myName', $sUT->getName());
self::assertSame(100, $sUT->getTtl());
self::assertTrue($sUT->isAutoRelease());
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Job/JobListTest.php
Expand Up @@ -47,4 +47,26 @@ public function testDuplicateProcess(): void

$sUT->push($second);
}

public function testFromArrayEmpty(): void
{
$sUT = JobList::fromArray([]);

self::assertCount(0, $sUT->getList());
}

public function testFromArrayWithOneJob(): void
{
$sUT = JobList::fromArray([
[
'command' => 'ffff',
'cronExpression' => 'ffff',
'name' => 'ffff',
'ttl' => 100,
'autoRelease' => true,
],
]);

self::assertCount(1, $sUT->getList());
}
}
42 changes: 0 additions & 42 deletions tests/Unit/Job/PhpJobTest.php

This file was deleted.

0 comments on commit 2a91192

Please sign in to comment.