entityManager = $entityManager; } public function prepare(Data $data, DateTimeImmutable $executeTime): void { $collection = $this->entityManager ->getRDBRepository('InboundEmail') ->where([ 'status' => 'Active', 'useImap' => true, ]) ->find(); foreach ($collection as $entity) { $running = $this->entityManager ->getRDBRepository(JobEntity::ENTITY_TYPE) ->where([ 'scheduledJobId' => $data->getId(), 'status' => [ Status::RUNNING, Status::READY, ], 'targetType' => 'InboundEmail', 'targetId' => $entity->getId(), ]) ->findOne(); if ($running) { continue; } $countPending = $this->entityManager ->getRDBRepository(JobEntity::ENTITY_TYPE) ->where([ 'scheduledJobId' => $data->getId(), 'status' => Status::PENDING, 'targetType' => 'InboundEmail', 'targetId' => $entity->getId(), ]) ->count(); if ($countPending > 1) { continue; } $jobEntity = $this->entityManager->getNewEntity(JobEntity::ENTITY_TYPE); $jobEntity->set([ 'name' => $data->getName(), 'scheduledJobId' => $data->getId(), 'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT), 'targetType' => 'InboundEmail', 'targetId' => $entity->getId(), ]); $this->entityManager->saveEntity($jobEntity); } } }