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('');
}
}