diff --git a/src/Entity/TaskExecutionRepository.php b/src/Entity/TaskExecutionRepository.php index 61f0049..3bb6db5 100644 --- a/src/Entity/TaskExecutionRepository.php +++ b/src/Entity/TaskExecutionRepository.php @@ -129,19 +129,24 @@ public function findByTaskUuid($taskUuid) /** * {@inheritdoc} */ - public function findNextScheduled(\DateTime $dateTime = null) + public function findNextScheduled(\DateTime $dateTime = null, array $skippedExecutions = []) { - $query = $this->createQueryBuilder('e') + $queryBuilder = $this->createQueryBuilder('e') ->innerJoin('e.task', 't') ->where('e.status = :status') ->andWhere('e.scheduleTime < :date') ->setParameter('date', $dateTime ?: new \DateTime()) ->setParameter('status', TaskStatus::PLANNED) - ->setMaxResults(1) - ->getQuery(); + ->setMaxResults(1); + + $expr = $queryBuilder->expr(); + if (!empty($skippedExecutions)) { + $queryBuilder->andWhere($expr->not($expr->in('e.uuid', ':skipped'))) + ->setParameter('skipped', $skippedExecutions); + } try { - return $query->getSingleResult(); + return $queryBuilder->getQuery()->getSingleResult(); } catch (NoResultException $exception) { return null; } diff --git a/tests/Functional/Entity/TaskExecutionRepositoryTest.php b/tests/Functional/Entity/TaskExecutionRepositoryTest.php index 707a7b9..1716f3a 100644 --- a/tests/Functional/Entity/TaskExecutionRepositoryTest.php +++ b/tests/Functional/Entity/TaskExecutionRepositoryTest.php @@ -168,6 +168,16 @@ public function testFindScheduledFuture() $this->assertNull($this->taskExecutionRepository->findNextScheduled()); } + public function testFindScheduledSkipped() + { + $task = $this->createTask(); + $this->taskRepository->save($task); + + $this->save($task, new \DateTime('+1 hour')); + + $this->assertNull($this->taskExecutionRepository->findNextScheduled()); + } + /** * Save a new execution to database. *