'ASC') ->setMaxResults($limit) ->setParameter('status', ScheduledTaskEntity::STATUS_SCHEDULED) ->setParameter('now', $now) ->setParameter('type', SendingQueue::TASK_TYPE) ->getQuery() ->getResult(); } public function invalidateTask(ScheduledTaskEntity $task): void { $task->setStatus(ScheduledTaskEntity::STATUS_INVALID); $this->persist($task); $this->flush(); } public function cancelTask(ScheduledTaskEntity $task): void { if (!in_array($task->getStatus(), self::CANCELLABLE_STATUSES)) { throw new \Exception(__('Only scheduled and running tasks can be cancelled', 'mailpoet'), 400); } $task->setStatus(ScheduledTaskEntity::STATUS_CANCELLED); $task->setCancelledAt(Carbon::now()->millisecond(0)); $this->persist($task); $this->flush(); } public function rescheduleTask(ScheduledTaskEntity $task): void { if ($task->getStatus() !== ScheduledTaskEntity::STATUS_CANCELLED) { throw new \Exception(__('Only cancelled tasks can be rescheduled', 'mailpoet'), 400); } if ($task->getScheduledAt() <= Carbon::now()->millisecond(0)) { $task->setStatus(ScheduledTaskEntity::VIRTUAL_STATUS_RUNNING); $queue = $task->getSendingQueue(); if ($queue) { $this->sendingQueuesRepository->resume($queue); } } else { $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); } $task->setCancelledAt(null); $this->persist($task); $this->flush(); } /** @param int[] $ids */ public function deleteByIds(array $ids): void { $this->entityManager->createQueryBuilder() ->delete(ScheduledTaskEntity::class, 't') ->where('t.id IN (:ids)') ->setParameter('ids', $ids) ->getQuery() ->execute(); // delete was done via DQL, make sure the entities are also detached from the entity manager $this->detachAll(function (ScheduledTaskEntity $entity) use ($ids) { return in_array($entity->getId(), $ids, true); }); } protected function findByTypeAndStatus($type, $status, $limit = null, $future = false) { $queryBuilder = $this->doctrineRepository->createQueryBuilder('st') ->select('st') ->where('st.type = :type') ->setParameter('type', $type) ->andWhere('st.deletedAt IS NULL'); if (is_null($status)) { $queryBuilder->andWhere('st.status IS NULL'); } else { $queryBuilder ->andWhere('st.status = :status') ->setParameter('status', $status); } if ($future) { $queryBuilder->andWhere('st.scheduledAt > :now'); } else { $queryBuilder->andWhere('st.scheduledAt <= :now'); } $now = Carbon::now()->millisecond(0); $queryBuilder->setParameter('now', $now); if ($limit) { $queryBuilder->setMaxResults($limit); } return $queryBuilder->getQuery()->getResult(); } protected function getEntityClassName() { return ScheduledTaskEntity::class; } }