Moved all event-dispatching stuff to its own module

This commit is contained in:
Alejandro Celaya
2019-07-19 19:54:39 +02:00
parent bccc177414
commit d086131630
16 changed files with 77 additions and 46 deletions

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\EventDispatcher\Async;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Swoole\Http\Server as HttpServer;
use Throwable;
use function get_class;
use function gettype;
use function is_object;
class TaskRunner
{
/** @var LoggerInterface */
private $logger;
/** @var ContainerInterface */
private $container;
public function __construct(LoggerInterface $logger, ContainerInterface $container)
{
$this->logger = $logger;
$this->container = $container;
}
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}', [
'type' => is_object($task) ? get_class($task) : gettype($task),
]);
$server->finish('');
return;
}
$this->logger->notice('Starting work on task {taskId}: {task}', [
'taskId' => $taskId,
'task' => $task->toString(),
]);
try {
$task($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('');
}
}
}