mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Created TaskRunnerTest
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
13
module/EventDispatcher/src/Async/TaskInterface.php
Normal file
13
module/EventDispatcher/src/Async/TaskInterface.php
Normal 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;
|
||||
}
|
||||
@@ -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('');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user