/**
  * Tests the lock.
  */
 public function testLock()
 {
     $lock = new LockHandler('contao:symlinks');
     $lock->lock();
     $command = new SymlinksCommand('contao:symlinks');
     $tester = new CommandTester($command);
     $code = $tester->execute([]);
     $this->assertEquals(1, $code);
     $this->assertContains('The command is already running in another process.', $tester->getDisplay());
     $lock->release();
 }
 protected function execute(InputInterface $input, OutputInterface $outputInterface)
 {
     $lockHandler = new LockHandler($this->getName());
     if (!$lockHandler->lock()) {
         echo "Jiná instance commandu ještě běží!";
         return false;
     }
     $filesystem = new Filesystem();
     $files = ["http://placekitten.com/408/287", "http://placekitten.com/300/128", "http://placekitten.com/123/456", "http://placekitten.com/54/68", "http://foo.bar/123"];
     foreach ($files as $key => $file) {
         try {
             $targetDir = "tmp/" . $key;
             $filesystem->mkdir($targetDir);
             $targetFile = $targetDir . "/" . $key . ".jpg";
             $outputInterface->write("kopíruji " . $file . " do " . $targetFile . " - ");
             $filesystem->copy($file, $targetFile);
         } catch (IOException $e) {
             $outputInterface->writeln("Chyba " . $e->getMessage());
             continue;
         }
         $outputInterface->writeln("OK!");
         //Pro další příklad si ještě upravíme čas přístupu
         $accessDate = new DateTime();
         $accessDate->sub(new DateInterval("P" . $key . "D"));
         $filesystem->touch($targetFile, $accessDate->format("U"), $accessDate->format("U"));
     }
 }
Exemple #3
0
 /**
  * @inheritdoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new SymfonyStyle($input, $output);
     $lockHandler = new LockHandler('app:sync.lock');
     if (!$lockHandler->lock()) {
         $io->warning('Sync process already running');
         return;
     }
     $service = $this->getContainer()->get('app.sync');
     switch ($input->getOption('type')) {
         case 'users':
             $service->syncUsers();
             break;
         case 'groups':
             $service->syncGroups();
             break;
         case 'grouphub':
             $service->syncGrouphubGroups();
             break;
         case 'queue':
             $service->syncGrouphubGroupsFromQueue();
             break;
         default:
             $service->sync();
     }
     $io->success('Done!');
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param int|null        $exitCode
  */
 public function postRun(InputInterface $input, OutputInterface $output, $exitCode)
 {
     // Release lock if set
     if (!is_null($this->lockHandler)) {
         $this->lockHandler->release();
     }
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler('import:all_galleries');
     if (!$lock->lock()) {
         $output->writeln('The import command is already running in another process.');
         return 0;
     }
     $this->db = new \DB();
     $commandProcess = new CommandProcess();
     $num = $this->db->count('import_galleries', 'is_added=false');
     $start = 0;
     $results = 100;
     $pages = ceil($num / $results);
     for ($i = 1; $i <= $pages; $i++) {
         $sql = "SELECT * FROM import_galleries WHERE is_added=false LIMIT {$start}, {$results}";
         $rows = $this->db->exec($sql);
         //            print_r($rows);
         foreach ($rows as $key) {
             // process line here
             if ($commandProcess->processDbLine($key, $output)) {
                 if ($this->db->link->query("UPDATE " . IMPORT_TABLE . " SET `is_added`=true WHERE `id`='{$key->id}'")) {
                     $output->writeln("Query updated! Url Added: " . $key->gallery_url);
                 } else {
                     $output->writeln($this->db->link->error);
                 }
             }
         }
         $start += $results;
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $force = (bool) $input->getOption('force');
     $verbose = (bool) $input->getOption('verbose');
     $deployLock = $this->getContainer()->getParameter('kernel.cache_dir') . '/deploy.globallock';
     if (file_exists($deployLock)) {
         if ($verbose) {
             $output->writeln('Aborting, ' . $deployLock . ' file present');
         }
         return;
     }
     $doctrine = $this->getContainer()->get('doctrine');
     if ($force) {
         $packages = $doctrine->getManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
     } else {
         $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForDumping();
     }
     $ids = array();
     foreach ($packages as $package) {
         $ids[] = $package['id'];
     }
     $lock = new LockHandler('packagist_package_dumper');
     ini_set('memory_limit', -1);
     gc_enable();
     // another dumper is still active
     if (!$lock->lock()) {
         if ($verbose) {
             $output->writeln('Aborting, another dumper is still active');
         }
         return;
     }
     $result = $this->getContainer()->get('packagist.package_dumper')->dump($ids, $force, $verbose);
     $lock->release();
     return $result ? 0 : 1;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lockHandler = new LockHandler('lock:test:run.lock', CACHE_PATH);
     var_dump($lockHandler->lock());
     sleep(10);
     $lockHandler->release();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeln('The command is already running in another process.');
         return 0;
     }
     $repositoryName = $input->getArgument('repository');
     if ($repositoryName) {
         $repository = $this->repositoryStorage->get($repositoryName);
         if ($repository === null) {
             throw new \InvalidArgumentException('Unknown repository ' . $repositoryName);
         }
         $repositories = [$repository];
     } else {
         $repositories = $this->getRepositoriesToUpdate();
     }
     foreach ($repositories as $repository) {
         $output->writeln(sprintf('Updating <info>%s</info>', $repository->getName()));
         $timer = microtime(true);
         $this->update($repository, $output);
         $output->writeln(sprintf('Took %ds', microtime(true) - $timer));
     }
     $lock->release();
     return 0;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lockHandler = new LockHandler('sendDeferredWelcome.lock');
     if ($lockHandler->lock()) {
         $this->getContainer()->get('nyrodev')->increasePhpLimits();
         $this->getContainer()->enterScope('request');
         $this->getContainer()->set('request', new Request(), 'request');
         $context = $this->getContainer()->get('router')->getContext();
         $context->setScheme($this->getContainer()->getParameter('nyroCms.email.router_scheme'));
         $context->setHost($this->getContainer()->getParameter('nyroCms.email.router_host'));
         $context->setBaseUrl($this->getContainer()->getParameter('nyroCms.email.router_base_url'));
         $users = $this->getContainer()->get('nyrocms_db')->getUserRepository()->getForWelcomeEmails();
         $nbUsers = count($users);
         $output->writeln($nbUsers . ' are activated or has as password key which ends today.');
         if ($nbUsers > 0) {
             foreach ($users as $user) {
                 $this->getContainer()->get('nyrocms_user')->sendWelcomeEmail($user);
             }
         }
         $output->writeln('End of welcome email sending.');
         $lockHandler->release();
     } else {
         $output->writeln('sendDeferredWelcome command is locked.');
     }
 }
Exemple #10
0
 protected function lockFile($filename, $nbTries = 0)
 {
     if (!$this->lockHandler->lock()) {
         if ($nbTries >= CsvBase::MAX_LOCK_TRIES) {
             throw new DataStoreException('Reach max retry for locking queue file ' . $filename);
         }
         usleep(10);
         return $this->lockFile($filename, $nbTries + 1);
     }
 }
Exemple #11
0
 public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
 {
     $command = new \FooLockCommand();
     $lock = new LockHandler($command->getName());
     $lock->lock();
     $tester = new CommandTester($command);
     $this->assertSame(1, $tester->execute(array()));
     $lock->release();
     $this->assertSame(2, $tester->execute(array()));
 }
 public function testLockIsReleased()
 {
     $name = 'symfony-test-filesystem.lock';
     $l1 = new LockHandler($name);
     $l2 = new LockHandler($name);
     $this->assertTrue($l1->lock());
     $this->assertFalse($l2->lock());
     $l1 = null;
     $this->assertTrue($l2->lock());
     $l2->release();
 }
 /**
  * Tests the lock.
  */
 public function testLock()
 {
     $lock = new LockHandler('contao:filesync');
     $lock->lock();
     $command = new FilesyncCommand('contao:filesync');
     $command->setFramework($this->mockContaoFramework());
     $tester = new CommandTester($command);
     $code = $tester->execute([]);
     $this->assertEquals(1, $code);
     $this->assertContains('The command is already running in another process.', $tester->getDisplay());
     $lock->release();
 }
Exemple #14
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     //Проверим запущена ли команда уже, если да то выходим
     $lockHandler = new LockHandler('xml.generate.lock');
     if (!$lockHandler->lock()) {
         $output->writeln('Command is locked');
         return 0;
     }
     $email = $input->getArgument('email');
     if ($email && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
         throw new InvalidArgumentException('The argument must be valid email');
     }
     $container = $this->getContainer();
     $em = $container->get('doctrine.orm.entity_manager');
     $persons = $em->getRepository('AppBundle:Person')->findAll();
     $serializer = SerializerBuilder::create()->build();
     //ищем файл XML для экспорта, если нет - создаем
     $fs = new Filesystem();
     $path = $container->get('kernel')->getRootDir() . '/data/result.xml';
     if ($fs->exists($path)) {
         $content = file_get_contents($path);
         $xml = new \SimpleXMLElement($content);
     } else {
         $xml = new \SimpleXMLElement('<persons/>');
         $xml->asXML($path);
     }
     //идем по всем Person и ищем соответствующий айди в XML
     foreach ($persons as $person) {
         $serialized = $serializer->serialize($person, 'xml');
         $t = $xml->xpath(sprintf('//person[@id="%d"]', $person->getId()));
         if ($t) {
             //если находим - удаляем нод
             $dom = dom_import_simplexml($t[0]);
             $dom->parentNode->removeChild($dom);
         }
         //вставляем новый нод
         $target = $xml->xpath('/persons');
         $dom = dom_import_simplexml($target[0]);
         $insertDom = $dom->ownerDocument->importNode(dom_import_simplexml(new \SimpleXMLElement($serialized)), true);
         $dom->appendChild($insertDom);
     }
     $xml->asXML($path);
     $publicPath = $container->get('kernel')->getRootDir() . '/../web/' . XmlCommand::XML_PATH;
     $fs->copy($path, $publicPath);
     $timeFinished = new \DateTime();
     if ($email) {
         $context = $container->get('router')->getContext();
         $message = \Swift_Message::newInstance()->setSubject('Task finished')->setFrom('noreply@' . $context->getHost())->setTo($email)->setBody($container->get('templating')->render('emails/xmlFinished.html.twig', ['taskName' => XmlCommand::TASK_NAME, 'time' => $timeFinished, 'link' => $context->getHost() . '/' . XmlCommand::XML_PATH]), 'text/html');
         $container->get('mailer')->send($message);
     }
     return 0;
 }
 /**
  * {@inheritdoc}
  */
 protected final function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeln('The command is already running in another process.');
         return 1;
     }
     if (($errorCode = $this->executeLocked($input, $output)) > 0) {
         $lock->release();
         return $errorCode;
     }
     $lock->release();
     return 0;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeLn('<error>The command is already running in another process.</error>');
         return 0;
     }
     // ask user as it will remove all items from its database
     if (0 >= $input->getOption('max')) {
         $dialog = $this->getHelperSet()->get('dialog');
         if (!$dialog->askConfirmation($output, '<question>You will remove ALL items, are your sure?</question>', false)) {
             return $output->writeLn('<comment>You *almost* remove everything from your database, pfiou !</comment> Be sure to define a <comment>max</comment> option greater than 0.');
         }
     }
     $container = $this->getContainer();
     $dm = $container->get('doctrine.odm.mongodb.document_manager');
     $feedRepo = $dm->getRepository('Api43FeedBundle:Feed');
     $feedItemRepo = $dm->getRepository('Api43FeedBundle:FeedItem');
     // retrieve feed to work on
     if ($slug = $input->getOption('slug')) {
         $feed = $feedRepo->findOneBySlug($slug);
         if (!$feed) {
             return $output->writeLn('<error>Unable to find Feed document:</error> <comment>' . $slug . '</comment>');
         }
         $feeds = array($feed);
     } else {
         $feeds = $feedRepo->findAll();
     }
     if ($input->getOption('with-trace')) {
         $output->writeLn('<info>Feeds</info>: <comment>' . count($feeds) . '</comment>');
     }
     $totalRemoved = 0;
     foreach ($feeds as $feed) {
         $items = $feedItemRepo->findOldItemsByFeedId($feed->getId(), $input->getOption('max'));
         // manual remove. I can't find a way to perform a remove + skip in one query, it doesn't work :-/
         $removed = 0;
         foreach ($items as $item) {
             $dm->remove($item);
             ++$removed;
         }
         $totalRemoved += $removed;
         if ($input->getOption('with-trace')) {
             $output->writeLn('<info>' . $feed->getName() . '</info>: <comment>' . $removed . '</comment> removed.');
         }
     }
     $dm->flush();
     $dm->clear();
     $output->writeLn('<comment>' . $totalRemoved . '</comment> items removed.');
 }
 /**
  * Tests the lock.
  */
 public function testLock()
 {
     $lock = new LockHandler('contao:automator');
     $lock->lock();
     $command = new AutomatorCommand('contao:automator');
     $command->setApplication($this->getApplication());
     $command->setFramework($this->mockContaoFramework());
     $tester = new CommandTester($command);
     /** @var QuestionHelper $helper */
     $helper = $command->getHelper('question');
     $helper->setInputStream($this->getStreamFromInput("\n"));
     $code = $tester->execute(['command' => $command->getName()]);
     $this->assertEquals(1, $code);
     $this->assertEquals("The command is already running in another process.\n", $tester->getDisplay());
     $lock->release();
 }
Exemple #18
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Create the lock:
     $lock = new LockHandler('go');
     if (!$lock->lock()) {
         return 0xff;
     }
     $message = __METHOD__;
     $file = realpath(__DIR__ . '/../Resources/data/Siciarek_Jacek.vcf');
     $vcard = new Vcard($file);
     $data = $vcard->getData();
     $json = json_encode($data, JSON_PRETTY_PRINT);
     $output->writeln($json);
     // Release the lock:
     $lock->release();
 }
 protected function postRun(InputInterface $input, OutputInterface $output, $exitCode)
 {
     $this->getLoggingEnhancement()->postRun($input, $output, $exitCode);
     // Release lock if set
     if (!is_null($this->lockHandler)) {
         $this->lockHandler->release();
     }
 }
 /**
  *
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lockHandler = new LockHandler('crawler-pop.lock');
     if (!$lockHandler->lock()) {
         $output->writeln("<error>Crawler already running</error>");
         return 0;
     }
     $doctrine = $this->getContainer()->get('doctrine');
     $em = $doctrine->getManager();
     $pageRepository = $doctrine->getRepository('S2bCrawlerBundle:Page');
     $limit = $input->getOption('limit');
     // $parse = $input->getOption('parse');
     $output->writeln('');
     // if ($parse) $output->writeln('Running with automatic parsing mode');
     do {
         $page = $pageRepository->findOneNotCrawled();
         if (!$page) {
             $output->writeln("<error>Empty queue</error>");
             return;
         }
         $output->writeln('Found ' . $page->getUrl());
         $input = ['command' => 's2b:crawler:crawl', 'id' => $page->getId(), '--verbose' => $output->getVerbosity()];
         if ($output->isVerbose()) {
             $output->writeln('Running ' . $input['command'] . ' with id ' . $input['id']);
         }
         $this->getApplication()->find('s2b:crawler:crawl')->run(new ArrayInput($input), $output);
         // Parse crawled page
         // if ($parse) {
         //     $input = [
         //         'command' => 's2b:parser:parse',
         //         'id'=>$page->getId(),
         //         '--verbose'=>$output->getVerbosity()
         //     ];
         //     if ($output->isVerbose()) {
         //         $output->writeln('Running ' . $input['command'] . ' with id ' . $input['id']);
         //     }
         //     $this->getApplication()
         //         ->find('s2b:parser:parse')
         //         ->run(new ArrayInput($input), $output);
         // }
         $output->writeln('');
     } while (--$limit > 0);
     $output->writeln('Crawling finished');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler('app:sendcoupons');
     if (!$lock->lock()) {
         $output->writeln('The command is already running in another process.');
         return 0;
     }
     $em = $this->getContainer()->get('doctrine')->getManager();
     $repo = $em->getRepository('AppBundle\\Entity\\Customer');
     $customers = $repo->findBy(['isActivated' => true, 'couponsHaveBeenSent' => false], ['datetimeActivation' => 'ASC'], 100);
     foreach ($customers as $customer) {
         $mapped = $this->getContainer()->get('couponmapper')->mapNToCustomer(6, $customer);
         if (!$mapped) {
             $output->writeln('Could not map coupon codes to customer ' . $customer->getId());
             $lock->release();
             return 1;
         }
         $couponcodesData = [];
         foreach ($customer->getCouponcodes() as $couponcode) {
             ob_start();
             @QRcode::png($couponcode->getCode());
             $imageData = ob_get_contents();
             ob_end_clean();
             $couponcodesData[] = base64_encode($imageData);
         }
         $useRemoteFont = true;
         if ($this->getContainer()->get('kernel')->getEnvironment() === 'test') {
             $useRemoteFont = false;
             // This decouples test runs from Internet connectivity
         }
         $pdfData = $this->getContainer()->get('knp_snappy.pdf')->getOutputFromHtml($this->getContainer()->get('templating')->render('AppBundle:coupons:index.html.twig', array('customer' => $customer, 'couponcodesData' => $couponcodesData, 'useRemoteFont' => $useRemoteFont)));
         if ($this->getContainer()->get('kernel')->getEnvironment() === 'dev') {
             file_put_contents('/var/tmp/coupon.pdf', $pdfData);
         }
         $fileLocator = $this->getContainer()->get('file_locator');
         $brandsPdfPath = $fileLocator->locate('@AppBundle/Resources/other/Marken_Selbst_Vertragspartner_2015_09_24.pdf');
         $message = \Swift_Message::newInstance()->setSubject('Ihre Rabattcodes für die Good Buy METRO Sonderaktion')->setFrom('goodbuy-metro@jumpmail.galeria-kaufhof.de')->setTo($customer->getEmail())->setBody($this->getContainer()->get('templating')->render('Emails/couponCodes.html.twig', ['customer' => $customer]), 'text/html')->attach(\Swift_Attachment::newInstance($pdfData, 'Goodbye-Metro-Rabattcodes.pdf', 'application/pdf'))->attach(\Swift_Attachment::fromPath($brandsPdfPath, 'application/pdf'));
         $this->getContainer()->get('mailer')->send($message);
         $customer->setCouponsHaveBeenSent(true);
         $em->flush();
         $output->writeln($customer->getEmail());
     }
     $lock->release();
 }
 /**
  * (non-PHPdoc)
  * @see Symfony\Component\Console\Command.Command::execute()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // create the lock
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeln('The command is already running in another process.');
         return 0;
     }
     $failedAddresses = array();
     $output->writeln(date(\DateTime::RFC2822) . " : starting to send newsletter emails.");
     $sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNewsletter($failedAddresses);
     $output->writeln(date(\DateTime::RFC2822) . " : " . str_pad($sentMails, 4, " ", STR_PAD_LEFT) . " newsletter emails have been sent.");
     if (sizeof($failedAddresses) > 0) {
         $output->writeln(date(\DateTime::RFC2822) . " : " . "The following email-addresses failed:");
         foreach ($failedAddresses as $address) {
             $output->writeln("       " . $address);
         }
     }
 }
 /**
  * (non-PHPdoc)
  * @see Symfony\Component\Console\Command.Command::execute()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // create the lock
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeln('The command is already running in another process.');
         return 0;
     }
     $failedAddresses = array();
     $sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNotifications($failedAddresses);
     $output->writeln(date(\DateTime::RFC2822) . " : " . str_pad($sentMails, 4, " ", STR_PAD_LEFT) . " emails have been processed.");
     if (sizeof($failedAddresses) > 0) {
         $output->writeln(date(\DateTime::RFC2822) . " : " . "The following email-addresses failed:");
         foreach ($failedAddresses as $address) {
             $output->writeln("    " . $address);
         }
     }
     // (optional) release the lock (otherwise, PHP will do it for you automatically)
     $lock->release();
 }
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int
  */
 private function runInternal(InputInterface $input, OutputInterface $output)
 {
     try {
         $lock = new LockHandler('Integrated\\Bundle\\SolrBundle\\Command\\IndexerRunCommand');
         $attemps = 0;
         while (!$lock->lock()) {
             //retry for almost a minute, otherwise don't throw an error (after all another indexer is running)
             if ($attemps++ >= 10) {
                 return 0;
             }
             sleep(5);
         }
         /** @var IndexerInterface $indexer */
         $indexer = $this->getContainer()->get('integrated_solr.indexer');
         $indexer->execute();
     } catch (Exception $e) {
         $output->writeln("Aborting: " . $e->getMessage());
         return 1;
     }
     return 0;
 }
Exemple #25
0
 /**
  * Release the lock.
  *
  * @return void
  */
 private function releaseLock()
 {
     $this->logger->info('Release lock file.');
     $this->lock->release();
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $verbose = $input->getOption('verbose');
     $force = $input->getOption('force');
     $indexAll = $input->getOption('all');
     $package = $input->getArgument('package');
     $deployLock = $this->getContainer()->getParameter('kernel.cache_dir') . '/deploy.globallock';
     if (file_exists($deployLock)) {
         if ($verbose) {
             $output->writeln('Aborting, ' . $deployLock . ' file present');
         }
         return;
     }
     $doctrine = $this->getContainer()->get('doctrine');
     $solarium = $this->getContainer()->get('solarium.client');
     $redis = $this->getContainer()->get('snc_redis.default');
     $downloadManager = $this->getContainer()->get('packagist.download_manager');
     $favoriteManager = $this->getContainer()->get('packagist.favorite_manager');
     $lock = new LockHandler('packagist_package_indexer');
     // another dumper is still active
     if (!$lock->lock()) {
         if ($verbose) {
             $output->writeln('Aborting, another indexer is still active');
         }
         return;
     }
     if ($package) {
         $packages = array(array('id' => $doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package)->getId()));
     } elseif ($force || $indexAll) {
         $packages = $doctrine->getManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
         $doctrine->getManager()->getConnection()->executeQuery('UPDATE package SET indexedAt = NULL');
     } else {
         $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForIndexing();
     }
     $ids = array();
     foreach ($packages as $row) {
         $ids[] = $row['id'];
     }
     // clear index before a full-update
     if ($force && !$package) {
         if ($verbose) {
             $output->writeln('Deleting existing index');
         }
         $update = $solarium->createUpdate();
         $update->addDeleteQuery('*:*');
         $update->addCommit();
         $solarium->update($update);
     }
     $total = count($ids);
     $current = 0;
     // update package index
     while ($ids) {
         $indexTime = new \DateTime();
         $idsSlice = array_splice($ids, 0, 50);
         $packages = $doctrine->getRepository('PackagistWebBundle:Package')->findById($idsSlice);
         $update = $solarium->createUpdate();
         $indexTimeUpdates = [];
         foreach ($packages as $package) {
             $current++;
             if ($verbose) {
                 $output->writeln('[' . sprintf('%' . strlen($total) . 'd', $current) . '/' . $total . '] Indexing ' . $package->getName());
             }
             try {
                 $document = $update->createDocument();
                 $tags = $doctrine->getManager()->getConnection()->fetchAll('SELECT t.name FROM package p
                         JOIN package_version pv ON p.id = pv.package_id
                         JOIN version_tag vt ON vt.version_id = pv.id
                         JOIN tag t ON t.id = vt.tag_id
                         WHERE p.id = :id
                         GROUP BY t.id, t.name', ['id' => $package->getId()]);
                 foreach ($tags as $idx => $tag) {
                     $tags[$idx] = $tag['name'];
                 }
                 $this->updateDocumentFromPackage($document, $package, $tags, $redis, $downloadManager, $favoriteManager);
                 $update->addDocument($document);
                 $indexTimeUpdates[$indexTime->format('Y-m-d H:i:s')][] = $package->getId();
             } catch (\Exception $e) {
                 $output->writeln('<error>Exception: ' . $e->getMessage() . ', skipping package ' . $package->getName() . '.</error>');
             }
             $providers = $doctrine->getManager()->getConnection()->fetchAll('SELECT lp.packageName
                     FROM package p
                     JOIN package_version pv ON p.id = pv.package_id
                     JOIN link_provide lp ON lp.version_id = pv.id
                     WHERE p.id = :id
                     AND pv.development = true
                     GROUP BY lp.packageName', ['id' => $package->getId()]);
             foreach ($providers as $provided) {
                 $provided = $provided['packageName'];
                 try {
                     $document = $update->createDocument();
                     $document->setField('id', $provided);
                     $document->setField('name', $provided);
                     $document->setField('package_name', '');
                     $document->setField('description', '');
                     $document->setField('type', 'virtual-package');
                     $document->setField('trendiness', 100);
                     $document->setField('repository', '');
                     $document->setField('abandoned', 0);
                     $document->setField('replacementPackage', '');
                     $update->addDocument($document);
                 } catch (\Exception $e) {
                     $output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . ', skipping package ' . $package->getName() . ':provide:' . $provided . '</error>');
                 }
             }
         }
         try {
             $update->addCommit();
             $solarium->update($update);
         } catch (\Exception $e) {
             $output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . ', occurred while processing packages: ' . implode(',', $idsSlice) . '</error>');
         }
         $doctrine->getManager()->clear();
         unset($packages);
         if ($verbose) {
             $output->writeln('Updating package index times');
         }
         foreach ($indexTimeUpdates as $dt => $idsToUpdate) {
             $retries = 5;
             // retry loop in case of a lock timeout
             while ($retries--) {
                 try {
                     $doctrine->getManager()->getConnection()->executeQuery('UPDATE package SET indexedAt=:indexed WHERE id IN (:ids)', ['ids' => $idsToUpdate, 'indexed' => $dt], ['ids' => Connection::PARAM_INT_ARRAY]);
                 } catch (\Exception $e) {
                     if (!$retries) {
                         throw $e;
                     }
                     sleep(2);
                 }
             }
         }
     }
     $lock->release();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $lock = new LockHandler($this->getName());
     if (!$lock->lock()) {
         $output->writeLn('<error>The command is already running in another process.</error>');
         return 0;
     }
     $feeds = array();
     $container = $this->getContainer();
     $dm = $container->get('doctrine.odm.mongodb.document_manager');
     // define host for generating route
     $context = $container->get('router')->getContext();
     $context->setHost($container->getParameter('domain'));
     $feedRepo = $dm->getRepository('Api43FeedBundle:Feed');
     $feedItemRepo = $dm->getRepository('Api43FeedBundle:FeedItem');
     $progress = $this->getHelperSet()->get('progress');
     // retrieve feed to work on
     if ($slug = $input->getOption('slug')) {
         $feed = $feedRepo->findOneBySlug($slug);
         if (!$feed) {
             return $output->writeLn('<error>Unable to find Feed document:</error> <comment>' . $slug . '</comment>');
         }
         $feeds = array($feed);
     } elseif (in_array($input->getOption('age'), array('new', 'old'))) {
         $feedsWithItems = $feedItemRepo->findAllFeedWithItems();
         // retrieve feed that HAVE items
         if ('old' == $input->getOption('age')) {
             $feeds = $feedRepo->findByIds($feedsWithItems, 'in');
         }
         // retrieve feeds that DOESN'T have items
         if ('new' == $input->getOption('age')) {
             $feeds = $feedRepo->findByIds($feedsWithItems, 'notIn');
         }
     } else {
         return $output->writeLn('<error>You must add some options to the task :</error> an <comment>age</comment> or a <comment>slug</comment>');
     }
     if ($input->getOption('with-trace')) {
         $output->writeln('<info>Feeds to check</info>: ' . count($feeds));
     }
     $totalCached = 0;
     $feedUpdated = array();
     foreach ($feeds as $feed) {
         if ($input->getOption('with-trace')) {
             $output->writeln('<info>Working on</info>: ' . $feed->getName() . ' (parser: <comment>' . $feed->getParser() . '</comment>)');
         }
         $rssFeed = $container->get('simple_pie_proxy')->setUrl($feed->getLink())->init();
         // update feed description, in case it was empty
         if (0 === strlen($feed->getDescription()) && 0 !== strlen($rssFeed->get_description())) {
             $feed->setDescription(html_entity_decode($rssFeed->get_description(), ENT_COMPAT, 'UTF-8'));
             $dm->persist($feed);
             $dm->flush($feed);
         }
         $parser = $container->get('content_extractor')->init($feed->getParser(), $feed, true);
         $cachedLinks = $feedItemRepo->getAllLinks($feed->getId());
         $cached = 0;
         // show progress bar in trace mode only
         if ($input->getOption('with-trace')) {
             $total = $rssFeed->get_item_quantity();
             $progress->start($output, $total);
         }
         foreach ($rssFeed->get_items() as $item) {
             // if an item already exists, we skip it
             // or if the item doesn't have a link, we won't cache it - will be useless
             if (isset($cachedLinks[$item->get_permalink()]) || null === $item->get_permalink()) {
                 continue;
             }
             $parsedContent = $parser->parseContent($item->get_permalink(), $item->get_description());
             // if readable content failed, use default one from feed item
             $content = $parsedContent->content;
             if (false === $content) {
                 $content = $item->get_content();
             }
             // if there is no date in the feed, we use the current one
             $date = $item->get_date();
             if (null === $date) {
                 $date = date('j F Y, g:i:s a');
             }
             $feedItem = new FeedItem();
             $feedItem->setTitle(html_entity_decode($item->get_title(), ENT_COMPAT, 'UTF-8'));
             $feedItem->setLink($parsedContent->url);
             $feedItem->setContent($content);
             $feedItem->setPermalink($item->get_permalink());
             $feedItem->setPublishedAt($date);
             $feedItem->setFeed($feed);
             $dm->persist($feedItem);
             ++$cached;
             if ($input->getOption('with-trace')) {
                 $progress->advance();
             }
         }
         if ($cached) {
             if ($input->getOption('with-trace')) {
                 $progress->finish();
             }
             // save the last time items where updated
             $feed->setLastItemCachedAt(date('j F Y, g:i:s a'));
             $dm->persist($feed);
             $totalCached += $cached;
             $feedLog = new FeedLog();
             $feedLog->setItemsNumber($cached);
             $feedLog->setFeed($feed);
             $dm->persist($feedLog);
             // store feed url updated, to ping hub later
             $feedUpdated[] = $feed->getSlug();
         }
         if ($input->getOption('with-trace')) {
             $output->writeln('<info>New cached items</info>: ' . $cached);
         }
         $dm->flush();
     }
     if (!empty($feedUpdated)) {
         if ($input->getOption('with-trace')) {
             $output->writeln('<info>Ping hubs...</info>');
         }
         // send an event about new feed updated
         $event = new FeedItemEvent($feedUpdated);
         $container->get('event_dispatcher')->dispatch(Api43FeedEvents::AFTER_ITEM_CACHED, $event);
     }
     $output->writeLn('<comment>' . $totalCached . '</comment> items cached.');
     // update nb items for each udpated feed
     foreach ($feedUpdated as $slug) {
         $feed = $feedRepo->findOneByslug($slug);
         $nbItems = $feedItemRepo->countByFeedId($feed->getId());
         $feed->setNbItems($nbItems);
         $dm->persist($feed);
         if ($input->getOption('with-trace')) {
             $output->writeln('<info>' . $feed->getName() . '</info> items updated: <comment>' . $nbItems . '</comment>');
         }
     }
     $dm->flush();
     $dm->clear();
 }
Exemple #28
0
 /**
  * Creates cache file based on registered services
  *
  * If debug is false, it will only create the cache file if it doesn't already exist
  *
  * @return int Number of services cached
  */
 public function createCache()
 {
     $lockHandler = new LockHandler(self::RETROFIT_LOCK_FILE);
     $lockHandler->lock(true);
     // loop through registered services and write to file
     foreach ($this->services as $service) {
         $classMetaDataProvider = new ClassMetaDataProvider($service);
         $generatedClassMetaDataProvider = new GeneratedClassMetaDataProvider($classMetaDataProvider);
         $generatedClass = $this->restClientGenerator->generate($classMetaDataProvider, $generatedClassMetaDataProvider);
         $this->cacheWriter->write($generatedClassMetaDataProvider, $generatedClass);
     }
     $lockHandler->release();
     return count($this->services);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->initializeVariables();
     $this->io = new SymfonyStyle($input, $output);
     $this->io->title('CampaignChain Scheduler');
     // Prevent multiple console runs
     $lock = new LockHandler('campaignchain:scheduler');
     if (!$lock->lock()) {
         $this->io->error('The command is already in another process.');
         return 0;
     }
     $this->scheduler = $this->startScheduler();
     $this->logger->info(self::LOGGER_MSG_START);
     $this->logger->info('Scheduler with ID {id} started', ['id' => $this->scheduler->getId()]);
     $this->io->text('Running scheduler with:');
     $this->io->listing(['Scheduler ID: ' . $this->scheduler->getId(), 'Interval: ' . $this->scheduler->getPeriodInterval() . ' minute(s)', 'Period starts: ' . $this->scheduler->getPeriodStart()->format('Y-m-d H:i:s T'), 'Period ends: ' . $this->scheduler->getPeriodEnd()->format('Y-m-d H:i:s T')]);
     //Que jobs
     $this->gatherActionData();
     // Execute the scheduled report jobs.
     $this->prepareReportJobs();
     $this->executeJobs();
     // Scheduler is done, let's see how long it took.
     $stopwatchSchedulerEvent = $this->stopwatchScheduler->stop('scheduler');
     $this->scheduler->setDuration($stopwatchSchedulerEvent->getDuration());
     $this->scheduler->setExecutionEnd($this->now);
     $this->scheduler->setStatus(Scheduler::STATUS_CLOSED);
     $this->em->persist($this->scheduler);
     $this->em->flush();
     $this->io->success('Duration of scheduler: ' . $stopwatchSchedulerEvent->getDuration() . ' milliseconds');
     $this->logger->info(self::LOGGER_MSG_END);
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $verbose = $input->getOption('verbose');
     $force = $input->getOption('force');
     $indexAll = $input->getOption('all');
     $package = $input->getArgument('package');
     $deployLock = $this->getContainer()->getParameter('kernel.cache_dir') . '/deploy.globallock';
     if (file_exists($deployLock)) {
         if ($verbose) {
             $output->writeln('Aborting, ' . $deployLock . ' file present');
         }
         return;
     }
     $doctrine = $this->getContainer()->get('doctrine');
     $solarium = $this->getContainer()->get('solarium.client');
     $redis = $this->getContainer()->get('snc_redis.default');
     $downloadManager = $this->getContainer()->get('packagist.download_manager');
     $favoriteManager = $this->getContainer()->get('packagist.favorite_manager');
     $lock = new LockHandler('packagist_package_indexer');
     // another dumper is still active
     if (!$lock->lock()) {
         if ($verbose) {
             $output->writeln('Aborting, another indexer is still active');
         }
         return;
     }
     if ($package) {
         $packages = array(array('id' => $doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package)->getId()));
     } elseif ($force || $indexAll) {
         $packages = $doctrine->getManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
         $doctrine->getManager()->getConnection()->executeQuery('UPDATE package SET indexedAt = NULL');
     } else {
         $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForIndexing();
     }
     $ids = array();
     foreach ($packages as $row) {
         $ids[] = $row['id'];
     }
     // clear index before a full-update
     if ($force && !$package) {
         if ($verbose) {
             $output->writeln('Deleting existing index');
         }
         $update = $solarium->createUpdate();
         $update->addDeleteQuery('*:*');
         $update->addCommit();
         $solarium->update($update);
     }
     $total = count($ids);
     $current = 0;
     // update package index
     while ($ids) {
         $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getPackagesWithVersions(array_splice($ids, 0, 50));
         $update = $solarium->createUpdate();
         foreach ($packages as $package) {
             $current++;
             if ($verbose) {
                 $output->writeln('[' . sprintf('%' . strlen($total) . 'd', $current) . '/' . $total . '] Indexing ' . $package->getName());
             }
             try {
                 $document = $update->createDocument();
                 $this->updateDocumentFromPackage($document, $package, $redis, $downloadManager, $favoriteManager);
                 $update->addDocument($document);
                 $package->setIndexedAt(new \DateTime());
             } catch (\Exception $e) {
                 $output->writeln('<error>Exception: ' . $e->getMessage() . ', skipping package ' . $package->getName() . '.</error>');
             }
             foreach ($package->getVersions() as $version) {
                 // abort when a non-dev version shows up since dev ones are ordered first
                 if (!$version->isDevelopment()) {
                     break;
                 }
                 if (count($provide = $version->getProvide())) {
                     foreach ($version->getProvide() as $provide) {
                         try {
                             $document = $update->createDocument();
                             $document->setField('id', $provide->getPackageName());
                             $document->setField('name', $provide->getPackageName());
                             $document->setField('description', '');
                             $document->setField('type', 'virtual-package');
                             $document->setField('trendiness', 100);
                             $document->setField('repository', '');
                             $document->setField('abandoned', 0);
                             $document->setField('replacementPackage', '');
                             $update->addDocument($document);
                         } catch (\Exception $e) {
                             $output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . ', skipping package ' . $package->getName() . ':provide:' . $provide->getPackageName() . '</error>');
                         }
                     }
                 }
             }
         }
         $update->addCommit();
         $solarium->update($update);
         foreach ($packages as $package) {
             $doctrine->getManager()->flush($package);
         }
         $doctrine->getManager()->clear();
         unset($packages);
     }
     $lock->release();
 }