Created TaskRunnerTest

This commit is contained in:
Alejandro Celaya
2019-07-19 20:59:06 +02:00
parent af40e8de5c
commit af4ee8f7ec
6 changed files with 158 additions and 39 deletions

View File

@@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\EventDispatcher\Async;
use Psr\Container\ContainerInterface;
use function get_class;
use function sprintf;
class Task
{
/** @var string */
private $regularListenerName;
/** @var object */
private $event;
public function __construct(string $regularListenerName, object $event)
{
$this->regularListenerName = $regularListenerName;
$this->event = $event;
}
public function __invoke(ContainerInterface $container)
{
($container->get($this->regularListenerName))($this->event);
}
public function toString(): string
{
return sprintf('Listener -> "%s", Event -> "%s"', $this->regularListenerName, get_class($this->event));
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\EventDispatcher\Async;
use Psr\Container\ContainerInterface;
interface TaskInterface
{
public function run(ContainerInterface $container): void;
public function toString(): string;
}

View File

@@ -27,8 +27,8 @@ class TaskRunner
public function __invoke(HttpServer $server, int $taskId, int $fromId, $task): void
{
if (! $task instanceof Task) {
$this->logger->error('Invalid task provided to task worker: {type}', [
if (! $task instanceof TaskInterface) {
$this->logger->warning('Invalid task provided to task worker: {type}. Task ignored', [
'type' => is_object($task) ? get_class($task) : gettype($task),
]);
$server->finish('');
@@ -41,14 +41,13 @@ class TaskRunner
]);
try {
$task($this->container);
$task->run($this->container);
} catch (Throwable $e) {
$this->logger->error('Error processing task {taskId}: {e}', [
'taskId' => $taskId,
'e' => $e,
]);
} finally {
// Notify the server that processing of the task has finished:
$server->finish('');
}
}

View File

@@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\EventDispatcher\Listener;
use Shlinkio\Shlink\EventDispatcher\Async\Task;
use Swoole\Http\Server as HttpServer;
class AsyncEventListener
@@ -21,6 +20,6 @@ class AsyncEventListener
public function __invoke(object $event): void
{
$this->server->task(new Task($this->regularListenerName, $event));
$this->server->task(new EventListenerTask($this->regularListenerName, $event));
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\EventDispatcher\Listener;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\EventDispatcher\Async\TaskInterface;
use function get_class;
use function sprintf;
class EventListenerTask implements TaskInterface
{
/** @var string */
private $listenerName;
/** @var object */
private $event;
public function __construct(string $listenerName, object $event)
{
$this->listenerName = $listenerName;
$this->event = $event;
}
public function run(ContainerInterface $container): void
{
($container->get($this->listenerName))($this->event);
}
public function toString(): string
{
return sprintf('Listener -> "%s", Event -> "%s"', $this->listenerName, get_class($this->event));
}
}