getServiceFactory()->create('EmailAccount'); $entity = $this->getEntityManager()->getEntity('EmailAccount', $targetId); if (!$entity) { throw new Error("Job CheckEmailAccounts '".$targetId."': EmailAccount does not exist.", -1); }; if ($entity->get('status') !== 'Active') { throw new Error("Job CheckEmailAccounts '".$targetId."': EmailAccount is not active.", -1); } try { $service->fetchFromMailServer($entity); } catch (\Exception $e) { throw new Error('Job CheckEmailAccounts '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage()); } return true; } public function prepare($scheduledJob, $executeTime) { $collection = $this->getEntityManager()->getRepository('EmailAccount')->join([['assignedUser', 'assignedUserAdditional']])->where([ 'status' => 'Active', 'useImap' => true, 'assignedUserAdditional.isActive' => true ])->find(); foreach ($collection as $entity) { $running = $this->getEntityManager()->getRepository('Job')->where([ 'scheduledJobId' => $scheduledJob->id, 'status' => CronManager::RUNNING, 'targetType' => 'EmailAccount', 'targetId' => $entity->id ])->findOne(); if ($running) continue; $countPending = $this->getEntityManager()->getRepository('Job')->where([ 'scheduledJobId' => $scheduledJob->id, 'status' => CronManager::PENDING, 'targetType' => 'EmailAccount', 'targetId' => $entity->id ])->count(); if ($countPending > 1) continue; $jobEntity = $this->getEntityManager()->getEntity('Job'); $jobEntity->set([ 'name' => $scheduledJob->get('name'), 'scheduledJobId' => $scheduledJob->id, 'executeTime' => $executeTime, 'targetType' => 'EmailAccount', 'targetId' => $entity->id ]); $this->getEntityManager()->saveEntity($jobEntity); } return true; } }