-
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.
added StandardWorker with example of use (#16)
- Loading branch information
Showing
5 changed files
with
204 additions
and
3 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,70 @@ | ||
<?php | ||
|
||
use SAREhub\Commons\Logger\DefaultJsonLogFormatter; | ||
use SAREhub\Commons\Logger\StreamLoggerFactoryProvider; | ||
use SAREhub\Commons\Process\PcntlSignals; | ||
use SAREhub\Commons\Service\ServiceManager; | ||
use SAREhub\Commons\Service\ServiceSupport; | ||
use SAREhub\Commons\Task\Task; | ||
use SAREhub\DockerUtil\Worker\StandardWorker; | ||
use SAREhub\DockerUtil\Worker\WorkerRunner; | ||
|
||
require dirname(__DIR__) . "/bootstrap.php"; | ||
|
||
class ExampleInitTask implements Task | ||
{ | ||
public function run() | ||
{ | ||
printLine("INIT_TASK: initiating worker..."); | ||
sleep(1); | ||
printLine("INIT_TASK: worker initiated"); | ||
} | ||
} | ||
|
||
class ExampleService extends ServiceSupport | ||
{ | ||
/** | ||
* @var PcntlSignals | ||
*/ | ||
private $pcntl; | ||
|
||
private $tickCounter = 0; | ||
|
||
|
||
public function __construct(PcntlSignals $pcntl) | ||
{ | ||
$this->pcntl = $pcntl; | ||
} | ||
|
||
protected function doStart() | ||
{ | ||
sleep(1); | ||
} | ||
|
||
protected function doTick() | ||
{ | ||
printLine("SERVICE: hard working..."); | ||
sleep(2); | ||
$this->tickCounter++; | ||
if ($this->tickCounter > 2) { | ||
printLine("sending SIGTERM..."); | ||
// pcntl signals are not supported on Windows platform but we can send fake signal to show stop worker logic | ||
$this->pcntl->dispatchSignal(PcntlSignals::SIGTERM); | ||
} | ||
} | ||
|
||
protected function doStop() | ||
{ | ||
sleep(1); | ||
} | ||
} | ||
|
||
$loggerFactory = (new StreamLoggerFactoryProvider("DEBUG", new DefaultJsonLogFormatter()))->get(); | ||
$pcntl = new PcntlSignals(); | ||
$service = new ExampleService($pcntl); | ||
$service->setLogger($loggerFactory->create("Example Service")); | ||
$worker = new StandardWorker(new ExampleInitTask(), new ServiceManager([$service])); | ||
$worker->setLogger($loggerFactory->create("Worker")); | ||
$workerRunner = new WorkerRunner($worker, $pcntl); | ||
|
||
$workerRunner->run(); |
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,8 @@ | ||
<?php | ||
|
||
require dirname(__DIR__) . "/vendor/autoload.php"; | ||
|
||
function printLine(string $text) | ||
{ | ||
echo "$text\n"; | ||
} |
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,49 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\DockerUtil\Worker; | ||
|
||
|
||
use SAREhub\Commons\Service\ServiceManager; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
class StandardWorker extends BasicWorker | ||
{ | ||
/** | ||
* @var Task | ||
*/ | ||
private $initTask; | ||
|
||
/** | ||
* @var ServiceManager | ||
*/ | ||
private $serviceManager; | ||
|
||
public function __construct(Task $initTask, ServiceManager $serviceManager) | ||
{ | ||
$this->initTask = $initTask; | ||
$this->serviceManager = $serviceManager; | ||
} | ||
|
||
protected function doStart() | ||
{ | ||
$this->initTask->run(); | ||
$this->getServiceManager()->start(); | ||
} | ||
|
||
protected function doTick() | ||
{ | ||
$this->getServiceManager()->tick(); | ||
} | ||
|
||
protected function doStop() | ||
{ | ||
$this->getServiceManager()->stop(); | ||
} | ||
|
||
private function getServiceManager(): ServiceManager | ||
{ | ||
return $this->serviceManager; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
tests/unit/SAREhub/DockerUtil/Worker/StandardWorkerTest.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,67 @@ | ||
<?php | ||
|
||
namespace SAREhub\DockerUtil\Worker; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Commons\Service\ServiceManager; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
class StandardWorkerTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
/** | ||
* @var Task | MockInterface | ||
*/ | ||
private $initTask; | ||
|
||
/** | ||
* @var ServiceManager | MockInterface | ||
*/ | ||
private $serviceManager; | ||
|
||
/** | ||
* @var Worker | ||
*/ | ||
private $worker; | ||
|
||
protected function setUp() | ||
{ | ||
$this->initTask = \Mockery::mock(Task::class); | ||
$this->serviceManager = \Mockery::mock(ServiceManager::class); | ||
|
||
$this->worker = new StandardWorker($this->initTask, $this->serviceManager); | ||
} | ||
|
||
public function testStart() | ||
{ | ||
$this->initTask->expects("run"); | ||
$this->serviceManager->expects("start"); | ||
|
||
$this->worker->start(); | ||
} | ||
|
||
public function testTick() | ||
{ | ||
$this->initTask->allows("run"); | ||
$this->serviceManager->allows("start"); | ||
$this->worker->start(); | ||
|
||
$this->serviceManager->expects("tick"); | ||
|
||
$this->worker->tick(); | ||
} | ||
|
||
public function testStop() | ||
{ | ||
$this->initTask->allows("run"); | ||
$this->serviceManager->allows("start"); | ||
$this->worker->start(); | ||
|
||
$this->serviceManager->expects("stop"); | ||
|
||
$this->worker->stop(); | ||
} | ||
} |