-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add CronTask * add TimerServicePeriodicTasksRegisterTask
- Loading branch information
Showing
6 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Commons\Time; | ||
|
||
|
||
use Cron\CronExpression; | ||
use DateTimeInterface; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use SAREhub\Commons\Misc\TimeProvider; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
class CronTask implements LoggerAwareInterface | ||
{ | ||
/** | ||
* @var CronExpression | ||
*/ | ||
private $cron; | ||
|
||
/** | ||
* @var TimeProvider | ||
*/ | ||
private $timeProvider; | ||
|
||
/** | ||
* @var Task | ||
*/ | ||
private $taskToRun; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @var DateTimeInterface | ||
*/ | ||
private $nextRunDate; | ||
|
||
public function __construct(CronExpression $cron, TimeProvider $timeProvider, Task $taskToRun) | ||
{ | ||
$this->cron = $cron; | ||
$this->timeProvider = $timeProvider; | ||
$this->taskToRun = $taskToRun; | ||
$this->logger = new NullLogger(); | ||
$this->updateNextRunDate(); | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
$now = $this->timeProvider->now(); | ||
$this->logger->debug("Invoked, current nextRunDate: {nextRunDate}", [ | ||
"nextRunDate" => $this->nextRunDate->format(\DateTime::ATOM) | ||
]); | ||
if ($now >= $this->nextRunDate->getTimestamp()) { | ||
$this->logger->notice("Run task, current nextRunDate: {nextRunDate}", [ | ||
"nextRunDate" => $this->nextRunDate->format(\DateTime::ATOM) | ||
]); | ||
$this->taskToRun->run(); | ||
$this->updateNextRunDate(); | ||
$this->logger->notice("Task ended, current nextRunDate: {nextRunDate}", [ | ||
"nextRunDate" => $this->nextRunDate->format(\DateTime::ATOM) | ||
]); | ||
} | ||
} | ||
|
||
private function updateNextRunDate(): void | ||
{ | ||
$this->nextRunDate = $this->cron->getNextRunDate("@" . $this->timeProvider->now()); | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Commons\Time; | ||
|
||
|
||
class PeriodicTaskDefinition | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $interval; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $callback; | ||
|
||
public function __construct(string $id, int $interval, callable $callback) | ||
{ | ||
$this->id = $id; | ||
$this->interval = $interval; | ||
$this->callback = $callback; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getInterval(): int | ||
{ | ||
return $this->interval; | ||
} | ||
|
||
public function getCallback(): callable | ||
{ | ||
return $this->callback; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/SAREhub/Commons/Time/TimerServicePeriodicTasksRegisterTask.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Commons\Time; | ||
|
||
|
||
use SAREhub\Commons\Task\Task; | ||
use SAREhub\Commons\Time\TimerService; | ||
|
||
class TimerServicePeriodicTasksRegisterTask implements Task | ||
{ | ||
/** | ||
* @var TimerService | ||
*/ | ||
private $service; | ||
|
||
/** | ||
* @var PeriodicTaskDefinition[] | ||
*/ | ||
private $taskDefs; | ||
|
||
public function __construct(TimerService $service, array $taskDefs) | ||
{ | ||
$this->service = $service; | ||
$this->taskDefs = $taskDefs; | ||
} | ||
|
||
public function run() | ||
{ | ||
foreach ($this->taskDefs as $taskDef) { | ||
$this->service->addPeriodicTask( | ||
$taskDef->getId(), | ||
$taskDef->getInterval(), | ||
$taskDef->getCallback() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\Time; | ||
|
||
use Cron\CronExpression; | ||
use Mockery; | ||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Commons\Misc\TimeProvider; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
class CronTaskTest extends TestCase | ||
{ | ||
|
||
use MockeryPHPUnitIntegration; | ||
|
||
/** | ||
* @var CronExpression | ||
*/ | ||
private $cron; | ||
|
||
/** | ||
* @var TimeProvider | ||
*/ | ||
private $timeProvider; | ||
|
||
/** | ||
* @var Task | ||
*/ | ||
private $taskToRun; | ||
|
||
/** | ||
* @var CronTask | ||
*/ | ||
private $task; | ||
|
||
protected function setUp() | ||
{ | ||
$this->cron = CronExpression::factory("@hourly"); | ||
$this->timeProvider = new TimeProvider(); | ||
$this->timeProvider->freezeTime(); | ||
$this->taskToRun = Mockery::mock(Task::class); | ||
$this->task = new CronTask($this->cron, $this->timeProvider, $this->taskToRun); | ||
} | ||
|
||
public function testRunWhenNowIsNextRunDate() | ||
{ | ||
$currentNextRunDate = $this->cron->getNextRunDate("@" . $this->timeProvider->now()); | ||
$this->timeProvider->freezeTime($currentNextRunDate->getTimestamp()); | ||
|
||
$this->taskToRun->expects("run")->with(); | ||
|
||
($this->task)(); | ||
} | ||
|
||
public function testRunWhenNowIsNotNextRunDate() | ||
{ | ||
$currentNextRunDate = $this->cron->getNextRunDate("@" . $this->timeProvider->now()); | ||
$this->timeProvider->freezeTime($currentNextRunDate->getTimestamp() - 1); | ||
|
||
$this->taskToRun->expects("run")->never(); | ||
|
||
($this->task)(); | ||
} | ||
|
||
public function testRunWhenAfterRunDate() | ||
{ | ||
$currentNextRunDate = $this->cron->getNextRunDate("@" . $this->timeProvider->now()); | ||
$this->timeProvider->freezeTime($currentNextRunDate->getTimestamp() + 1); | ||
$this->taskToRun->expects("run")->once(); | ||
($this->task)(); | ||
|
||
$currentNextRunDate = $this->cron->getNextRunDate("@" . $this->timeProvider->now()); | ||
$this->timeProvider->freezeTime($currentNextRunDate->getTimestamp() - 1); | ||
$this->taskToRun->expects("run")->never(); | ||
|
||
($this->task)(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/SAREhub/Commons/Time/TimerServicePeriodicTasksRegisterTaskTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Commons\Time; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use SAREhub\Commons\Time\PeriodicTaskDefinition; | ||
use SAREhub\Commons\Time\TimerService; | ||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Commons\Time\TimerServicePeriodicTasksRegisterTask; | ||
|
||
class TimerServicePeriodicTasksRegisterTaskTest extends TestCase | ||
{ | ||
|
||
use MockeryPHPUnitIntegration; | ||
|
||
public function testRun() | ||
{ | ||
$timerService = \Mockery::mock(TimerService::class); | ||
$id = "test"; | ||
$interval = 60; | ||
$callback = function () { | ||
}; | ||
$def = new PeriodicTaskDefinition($id, $interval, $callback); | ||
$task = new TimerServicePeriodicTasksRegisterTask($timerService, [$def]); | ||
|
||
$timerService->expects("addPeriodicTask")->with($id, $interval, $callback); | ||
|
||
$task->run(); | ||
} | ||
} |