Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bupy7 committed Aug 18, 2017
1 parent 2889a3f commit c2e1480
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/Entity/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,12 @@ public function getStopAt(): ?DateTime;

public function incNumberErrors(): TaskInterface;

/**
* Returns number errors by default 0.
*/
public function getNumberErrors(): int;

public function setParams(array $params): TaskInterface;

public function getParams(): array;
}
21 changes: 13 additions & 8 deletions src/Service/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bupy7\Queue\Exception\UnknownTaskException;
use Bupy7\Queue\Task\TaskInterface;
use Bupy7\Queue\Options\ModuleOptions;
use Exception;

class QueueService
{
Expand Down Expand Up @@ -45,15 +46,19 @@ public function __construct(
public function run(): void
{
$entities = $this->taskRepository->findForRun($this->config->getOneTimeLimit() ?: null);
foreach ($entities as $entity) {
if (in_array($entity->getStatusId(), [
TaskEntityInterface::STATUS_WAIT,
TaskEntityInterface::STATUS_ERROR,
])) {
$this->executeTask($entity);
try {
foreach ($entities as $entity) {
if (in_array($entity->getStatusId(), [
TaskEntityInterface::STATUS_WAIT,
TaskEntityInterface::STATUS_ERROR,
])) {
$this->executeTask($entity);
}
}
} catch (Exception $e) {
$this->entityManager->flush();
throw $e;
}
$this->entityManager->flush();
}

protected function executeTask(TaskEntityInterface $entity): void
Expand All @@ -64,7 +69,7 @@ protected function executeTask(TaskEntityInterface $entity): void
/** @var TaskInterface $task */
$task = $this->queueManager->get($entity->getName());
$entity->setRunAt(new DateTime);
if ($task->execute()) {
if ($task->execute($entity->getParams())) {
$entity->setStatusId(TaskEntityInterface::STATUS_OK);
} else {
$entity->incNumberErrors();
Expand Down
6 changes: 2 additions & 4 deletions src/Service/TaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class TaskService
{
private const DEFAULT_NUMBER_ERRORS = 0;

/**
* @var EntityManagerInterface
*/
Expand All @@ -27,8 +25,8 @@ public function create(array $data, TaskInterface $task): bool
}
$task->setStatusId(TaskInterface::STATUS_WAIT)
->setCreatedAt(new DateTime)
->setNumberErrors(self::DEFAULT_NUMBER_ERRORS)
->setName($data['name']);
->setName($data['name'])
->setParams($data['params'] ?? []);
$this->entityManager->persist($task);
$this->entityManager->flush();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Task/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
interface TaskInterface
{
public function execute(): bool;
public function execute(array $params): bool;
}
15 changes: 15 additions & 0 deletions test/Assert/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Task implements TaskInterface
* @var int
*/
protected $numberErrors = 0;
/**
* @var array
*/
protected $params = [];

public function __construct()
{
Expand Down Expand Up @@ -135,4 +139,15 @@ public function getNumberErrors(): int
{
return $this->numberErrors;
}

public function setParams(array $params): TaskInterface
{
$this->params = $params;
return $this;
}

public function getParams(): array
{
return $this->params;
}
}
2 changes: 1 addition & 1 deletion test/Assert/Task/ErrorHelloTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class ErrorHelloTask implements TaskInterface
{
public function execute(): bool
public function execute(array $params): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/Assert/Task/SuccessHelloTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class SuccessHelloTask implements TaskInterface
{
public function execute(): bool
public function execute(array $params): bool
{
return true;
}
Expand Down

0 comments on commit c2e1480

Please sign in to comment.