public function testPing() { $this->assertTrue(defined('TEST_ORCHESTRATOR_DISABLED_SAPI_TOKEN')); $this->assertNotEmpty(constant('TEST_ORCHESTRATOR_DISABLED_SAPI_TOKEN')); $this->storageApi = new StorageApiClient(array('token' => TEST_ORCHESTRATOR_DISABLED_SAPI_TOKEN, 'url' => TEST_ORCHESTRATOR_SAPI_URL)); $pingClient = new PingClient($this->storageApi, new NullLogger()); $this->assertFalse($pingClient->ping()); }
/** * @param Orchestration $orchestration * @return Client */ private function createProjectSapi(Orchestration $orchestration) { $client = new Client(array('token' => $orchestration->getToken(), 'url' => $this->getConfiguration()->getStorageApiUrl(), 'userAgent' => $this->appName)); try { $pingClient = new StorageApi\PingClient($client, $this->logger); if (!$pingClient->ping()) { throw new DisabledException('Project disabled'); } } catch (\Exception $e) { throw new DisabledException('Migration skipped', 0, $e); } return $client; }
protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); $this->logger->info('scheduler.start', array()); $output->writeln('Current time: ' . $this->getStartTime()->format('Y-m-d H:i:s')); $this->lock->setLockName('scheduler-' . $this->getStartTime()->format('Y-m-d-H-i')); // skip locked try { if (!$this->lock->lock()) { $this->logger->info('scheduler.skipped', array('lockName' => $this->lock->getLockName())); return; } } catch (\Exception $e) { $this->logger->info('scheduler.error', array('message' => $e->getMessage(), 'lockName' => $this->lock->getLockName())); return; } $this->logger->info('scheduler.locked', array('lockName' => $this->lock->getLockName())); /** * @var Elasticsearch\JobManagerFactory $jobManagerFactory */ $jobManagerFactory = $this->getContainer()->get('orchestrator.job_manager.factory'); $this->queue = $this->getContainer()->get('syrup.queue_factory')->get(KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME); $this->dbOrchestrationManager = $this->getContainer()->get('orchestrator.doctrine.orchestration_manager'); $newJobs = array(); $orchestrations = $this->dbOrchestrationManager->findOrchestrations(array()); foreach ($orchestrations as $orchestration) { // skip deleted and non-active orchestrations if ($orchestration->getDeleted() || !$orchestration->getActive() || !$orchestration->getCrontabRecord()) { continue; } $cronExpression = CronExpression::factory($orchestration->getCrontabRecord()); if (!$cronExpression->isDue($this->getStartTime())) { continue; } $jobClient = new Client(array('token' => $orchestration->getToken(), 'url' => $this->getConfiguration()->getStorageApiUrl(), 'userAgent' => $this->appName)); try { $pingClient = new StorageApi\PingClient($jobClient, $this->logger); if (!$pingClient->ping()) { $this->logger->info('scheduler.orchestration.skipped.disabled', array('lockName' => $this->lock->getLockName(), 'orchestrationId' => $orchestration->getId(), 'orchestrationName' => $orchestration->getName(), 'projectId' => $orchestration->getProjectId(), 'projectName' => $orchestration->getTokenOwnerName())); continue; } } catch (\Exception $e) { $this->logger->info('scheduler.orchestration.skipped.error', array('lockName' => $this->lock->getLockName(), 'message' => $e->getMessage(), 'orchestrationId' => $orchestration->getId(), 'orchestrationName' => $orchestration->getName(), 'projectId' => $orchestration->getProjectId(), 'projectName' => $orchestration->getTokenOwnerName())); continue; } $token = new StorageApi\Token($jobClient); $jobEsManager = $jobManagerFactory->createJobManager($token); $jobsStats = $jobEsManager->getWaitingStats(); // skip waiting if (array_key_exists($orchestration->getId(), $jobsStats) && $jobsStats[$orchestration->getId()] >= AppConfiguration::MAX_WAITING_JOBS_COUNT) { $this->logger->info('scheduler.orchestration.skipped', array('lockName' => $this->lock->getLockName(), 'waitingCount' => $jobsStats[$orchestration->getId()], 'limit' => AppConfiguration::MAX_WAITING_JOBS_COUNT, 'orchestrationId' => $orchestration->getId(), 'orchestrationName' => $orchestration->getName(), 'projectId' => $token->getProjectId(), 'projectName' => $token->getOwnerName())); continue; } $orchestrationWithTasks = $this->dbOrchestrationManager->findOrchestrationById($orchestration->getId(), new StorageApi\Token($jobClient), true); $tasks = array_map(function ($task) { /** @var StorageApi\OrchestrationTask $task */ return $task->toApiArray(); }, $orchestrationWithTasks->getTasks()); $orchestration = $this->dbOrchestrationManager->updateLastScheduledTime($orchestration, new \DateTime()); // save job to queue $job = new Elasticsearch\Job(); $job->setOrchestrationId($orchestration->getId())->setConfig($orchestration->getId())->setOrchestrationName($orchestration->getName())->setToken($orchestration->getToken())->setTokenId($orchestration->getTokenId())->setTokenDesc($orchestration->getTokenDesc())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId())->setInitializedBy('scheduler')->setTaks($tasks)->setInitiatorTokenId($orchestration->getTokenId())->setInitiatorTokenDesc($orchestration->getTokenDesc())->setInitiatorUserAgent($this->appName . ' Scheduler'); $job = $jobEsManager->saveJob($job, new StorageApi\UniqueManager($jobClient)); $newJobs[] = $job; // add job to queue try { if (is_null($job)) { throw new ApplicationException('Unable to save or retrieve job from ES', null, array('orchestrationId' => $orchestration->getId())); } $this->queue->enqueue($job->getId(), array('jobId' => $job->getId(), 'component' => KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME)); $output->writeln(sprintf('Orchestration %d with cron %s scheduled. Job id: %s', $orchestration->getId(), $orchestration->getCrontabRecord(), $job->getId())); } catch (\Exception $e) { $this->logger->error('Error during adding job to sqs', array('lockName' => $this->lock->getLockName(), 'jobId' => $job->getId(), 'exception' => $e)); continue; } // log event try { $event = new Event(); $event->setComponent($this->appName)->setRunId($job->getRunId())->setParams(array('orchestrationId' => $job->getOrchestrationId()))->setResults(array('jobId' => $job->getId()))->setMessage(sprintf('Orchestration job %s scheduled', $job->getId())); StorageApi\EventLogger::create($event, $jobClient); $this->logger->info($event->getMessage(), array('lockName' => $this->lock->getLockName(), 'configurationId' => $event->getConfigurationId(), 'runId' => $event->getRunId(), 'description' => $event->getDescription(), 'params' => $event->getParams(), 'results' => $event->getResults())); } catch (Exception $e) { //@TODO user notification when notification API will be available $this->logger->warning('Invalid token', array('lockName' => $this->lock->getLockName(), 'jobId' => $job->getId(), 'exception' => $e)); } } $this->logger->info('scheduler.end', array('lockName' => $this->lock->getLockName(), 'duration' => $this->getDuration(), 'orchestrationsCheckedCount' => count($orchestrations), 'scheduledJobsCount' => count($newJobs))); sleep(90); // trying fix double run // unlock $this->lock->unlock(); }
protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); $dateTimeFrom = new \DateTime($input->getArgument('dateFrom')); if ($input->getArgument('dateTo')) { $dateTimeTo = new \DateTime($input->getArgument('dateTo')); } else { $dateTimeTo = new \DateTime(); } /** * @var Elasticsearch\JobManagerFactory $jobManagerFactory */ $jobManagerFactory = $this->getContainer()->get('orchestrator.job_manager.factory'); /** * @var Rds\OrchestrationManager $orchestrationManager */ $orchestrationManager = $this->getContainer()->get('orchestrator.doctrine.orchestration_manager'); $orchestrations = $orchestrationManager->findOrchestrations(array()); foreach ($orchestrations as $orchestration) { // skip deleted and non-active orchestrations if ($orchestration->getDeleted() || !$orchestration->getActive()) { continue; } $jobClient = new Client(array('token' => $orchestration->getToken(), 'url' => $this->getConfiguration()->getStorageApiUrl(), 'userAgent' => $this->appName)); try { $pingClient = new StorageApi\PingClient($jobClient, $this->logger); if (!$pingClient->ping()) { $output->writeln(sprintf('<comment>(%s) %s - (%s) %s</comment>', $orchestration->getProjectId(), $orchestration->getTokenOwnerName(), $orchestration->getId(), $orchestration->getName())); $output->writeln(sprintf('<error>Project disabled</error>')); continue; } } catch (\Exception $e) { $output->writeln(sprintf('<comment>(%s) %s - (%s) %s</comment>', $orchestration->getProjectId(), $orchestration->getTokenOwnerName(), $orchestration->getId(), $orchestration->getName())); $output->writeln(sprintf('<error>Could not verify token</error>')); continue; } $jobEsManager = $jobManagerFactory->createJobManager(new StorageApi\Token($jobClient)); $from = clone $dateTimeFrom; $to = clone $dateTimeTo; if (!$orchestration->getCrontabRecord()) { $output->writeln(sprintf('<comment>(%s) %s - (%s) %s</comment>', $orchestration->getProjectId(), $orchestration->getTokenOwnerName(), $orchestration->getId(), $orchestration->getName())); $output->writeln(sprintf('<info>Crontab does not specified</info>')); continue; } $cron = CronExpression::factory($orchestration->getCrontabRecord()); $output->writeln(sprintf('<comment>(%s) %s - (%s) %s</comment>', $orchestration->getProjectId(), $orchestration->getTokenOwnerName(), $orchestration->getId(), $orchestration->getName())); $jobs = $jobEsManager->findJobs(array('params.orchestration.id' => $orchestration->getId())); /** @var \DateTime $oldRun */ $oldRun = null; while ($from < $to) { $nextRun = $cron->getNextRunDate($from, 0, true); if ($nextRun->getTimestamp() >= $orchestration->getCreatedTime()->getTimestamp()) { if (!$oldRun || $nextRun->getTimestamp() != $oldRun->getTimestamp()) { if ($nextRun > $to) { break; } $jobCreated = false; foreach ($jobs as $job) { if ($job->getCreatedTime()->format('Y-m-d H:i') == $nextRun->format('Y-m-d H:i')) { $output->writeln(sprintf('<info>%s</info>', $nextRun->format('Y-m-d H:i'))); $jobCreated = true; } } if (!$jobCreated) { $output->writeln(sprintf('<error>%s</error>', $nextRun->format('Y-m-d H:i'))); } $oldRun = clone $nextRun; } } $from->modify('+1 minute'); } unset($from, $to, $oldRun, $cron, $jobs); } }