Skip to content

Commit

Permalink
added StandardWorker with example of use (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Jun 12, 2018
1 parent eec068f commit b91e9ba
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 3 deletions.
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@
],
"require-dev": {
"phpunit/phpunit": "6.*",
"mockery/mockery": "1.0",
"mockery/mockery": "1.1.*",
"codeclimate/php-test-reporter": "0.4.*",
"mikey179/vfsStream": "1.6.*"
"mikey179/vfsStream": "1.6.*",
"monolog/monolog": "1.23.*"
},
"require": {
"php": "^7.1",
"sarehub/commons": "0.7.*",
"psr/container": "1.0.0"
"psr/container": "1.*"
},
"autoload": {
"psr-4": {
"SAREhub\\": "src/SAREhub"
}
},
"autoload-dev": {
"psr-4": {
"SAREhub\\": "tests/SAREhub",
"SAREhub\\Example\\": "examples"
}
},
"bin": [
"bin/dockerutil"
]
Expand Down
70 changes: 70 additions & 0 deletions examples/StandardWorker/main.php
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();
8 changes: 8 additions & 0 deletions examples/bootstrap.php
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";
}
49 changes: 49 additions & 0 deletions src/SAREhub/DockerUtil/Worker/StandardWorker.php
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 tests/unit/SAREhub/DockerUtil/Worker/StandardWorkerTest.php
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();
}
}

0 comments on commit b91e9ba

Please sign in to comment.