Sets the current progress.
public setProgress ( integer $step ) | ||
$step | integer | The current progress |
/** * @return bool */ public function download() { if ($this->isDownloaded()) { return true; } $httpClient = new Client(); $request = new Request('GET', $this->getUrl()); $response = $httpClient->send($request, [RequestOptions::PROGRESS => function ($total, $current) { if ($total <= 0 || !$this->output) { return; } if (!$this->progressBar) { $this->progressBar = new ProgressBar($this->output, $total); $this->progressBar->setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); $this->progressBar->setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getProgress()), 11, ' ', STR_PAD_LEFT); }); } $this->progressBar->setProgress($current); }]); $this->filesystem->dumpFile($this->getDestinationFile(), $response->getBody()->getContents()); $this->downloaded = true; return true; }
/** * {@inheritdoc} */ protected function fire() { $offset = $this->argument('offset'); // Get all the element IDs ever $result = $this->craft->db->createCommand()->select('id, type')->from('elements')->offset($offset)->queryAll(); $progressBar = new ProgressBar($this->output, count($result) + $offset); foreach ($result as $i => $row) { // Get the element type $elementType = $this->craft->elements->getElementType($row['type']); if (!$elementType) { $progressBar->setProgress($offset + $i + 1); continue; } // delete existing indexes $this->craft->db->createCommand()->delete('searchindex', 'elementId = :elementId', array(':elementId' => $row['id'])); if ($elementType->isLocalized()) { $localeIds = $this->craft->i18n->getSiteLocaleIds(); } else { $localeIds = array($this->craft->i18n->getPrimarySiteLocaleId()); } $criteria = $this->craft->elements->getCriteria($row['type'], array('id' => $row['id'], 'status' => null, 'localeEnabled' => null)); foreach ($localeIds as $localeId) { $criteria->locale = $localeId; $element = $criteria->first(); if (!$element) { continue; } $this->craft->search->indexElementAttributes($element); if (!$elementType->hasContent()) { continue; } $fieldLayout = $element->getFieldLayout(); $keywords = array(); foreach ($fieldLayout->getFields() as $fieldLayoutField) { $field = $fieldLayoutField->getField(); if (!$field) { continue; } $fieldType = $field->getFieldType(); if (!$fieldType) { continue; } $fieldType->element = $element; $handle = $field->handle; // Set the keywords for the content's locale $fieldSearchKeywords = $fieldType->getSearchKeywords($element->getFieldValue($handle)); $keywords[$field->id] = $fieldSearchKeywords; $this->craft->search->indexElementFields($element->id, $localeId, $keywords); } } $progressBar->setProgress($offset + $i + 1); } $progressBar->finish(); $this->line(''); $this->info('Search indexes have been rebuilt.'); }
/** * @phpcsSuppress SlevomatCodingStandard.Typehints.TypeHintDeclaration.missingParameterTypeHint * @param int $step */ public function progressAdvance($step = 1) { if ($this->output->isDecorated() && $step > 0) { $stepTime = (time() - $this->progressBar->getStartTime()) / $step; if ($stepTime > 0 && $stepTime < 1) { $this->progressBar->setRedrawFrequency(1 / $stepTime); } else { $this->progressBar->setRedrawFrequency(1); } } $this->progressBar->setProgress($this->progressBar->getProgress() + $step); }
/** * @param InputInterface $input * @param OutputInterface $output * @return null|int null or 0 if everything went fine, or an error code */ protected function execute(InputInterface $input, OutputInterface $output) { $config = Config::getInstance(); $noProgress = $input->getOption('no-progress'); $query = $input->getArgument('query'); $provider = $config->getQuery($query); if (empty($provider)) { $output->writeln("<error>cannot found query \"{$query}\"</error>"); $output->writeln("<info>you can execute \"</info>ipv4 edit<info>\" to configure the query</info>"); return 1; } $type = $input->getOption('type'); $filename = $config->getFilename($input->getArgument('filename')); $export = ExportQuery::create($type, $filename); if (empty($export)) { $output->writeln("<error>cannot found export query \"{$type}\"</error>"); return 2; } $export->setProviders([$provider]); $encoding = $input->getOption('encoding'); if (!empty($encoding)) { $export->setEncoding($encoding); } $ecdz = $input->getOption('ecdz'); if ($ecdz && method_exists($export, 'setEcdz')) { $export->setEcdz($ecdz); } $remove_ip_in_recode = $input->getOption('remove-ip-in-recode'); if ($remove_ip_in_recode && method_exists($export, 'setRemoveIpInRecode')) { $export->setRemoveIpInRecode($remove_ip_in_recode); } $output->writeln("<info>export \"{$query}\" to \"{$type}\" filename \"{$filename}\":</info>"); if (!$noProgress) { $export->init(function ($code, $n) use($output) { switch ($code) { case 0: $this->progress = new ProgressBar($output, $n); $this->progress->start(); break; case 1: $this->progress->setProgress($n); break; case 2: $this->progress->finish(); break; } }); } else { $export->init(); } $output->writeln('<info> completed!</info>'); return 0; }
private function setDownloadWithProgressBar() { $emitter = $this->httpClient->getEmitter(); $emitter->on('before', function (\GuzzleHttp\Event\BeforeEvent $event) { echo $event->getRequest(); }); $emitter->once('progress', function (\GuzzleHttp\Event\ProgressEvent $event) { $this->progressBar->start($event->downloadSize); }); $emitter->on('progress', function (\GuzzleHttp\Event\ProgressEvent $event) { $this->progressBar->setProgress($event->downloaded); }); }
/** * Wait for multiple activities to complete. * * @param Activity[] $activities * @param OutputInterface $output */ public static function waitMultiple(array $activities, OutputInterface $output) { $count = count($activities); if ($count <= 0) { return; } $complete = 0; $output->writeln("Waiting..."); $bar = new ProgressBar($output); $bar->start($count); $bar->setFormat('verbose'); while ($complete < $count) { sleep(1); foreach ($activities as $activity) { if (!$activity->isComplete()) { $activity->refresh(); } else { $complete++; } } $bar->setProgress($complete); } $bar->finish(); $output->writeln(''); }
/** * @param OutputInterface $output * @param Query $query * @param string $name * @param bool $force * @param bool $noProgress * @return void * @throws \Exception */ protected function generate(OutputInterface $output, Query $query, $name, $force, $noProgress) { $use = implode(', ', $query->getProviders()); if (!$force && $query->exists()) { $output->writeln("<comment>use exist {$name} table.</comment>", OutputInterface::VERBOSITY_VERBOSE); } else { $output->writeln("<info>generate {$name} table with {$use}:</info>"); if (!$noProgress) { $query->init(function ($code, $n) use($output) { switch ($code) { case 0: $this->progress = new ProgressBar($output, $n); $this->progress->start(); break; case 1: $this->progress->setProgress($n); break; case 2: $this->progress->finish(); break; } }); } else { $query->init(); } $output->writeln('<info> completed!</info>'); } }
protected function execute(InputInterface $input, OutputInterface $output) { $desde = (int) $input->getArgument('desde'); $output->writeln('Importando actividades...'); $cantidad = 100; $progress = null; $importador = new ImportadorActividades($this->getContainer(), $this->getContainer()->get('doctrine')->getManager()); $importador->Inicializar(); $progress = new ProgressBar($output, $importador->ObtenerCantidadTotal()); $progress->start(); $ResultadoFinal = new ResultadoImportacion($importador); while (true) { $resultado = $importador->Importar($desde, $cantidad); $ResultadoFinal->AgregarContadoresLote($resultado); $progress->setProgress($resultado->PosicionCursor()); if (!$resultado->HayMasRegistros()) { break; } $desde += $cantidad; } $progress->finish(); $output->writeln(''); $importador->RecalcularParent($output); $output->writeln(' Se importaron ' . $ResultadoFinal->RegistrosNuevos . ' registros nuevos.'); $output->writeln(' Se actualizaron ' . $ResultadoFinal->RegistrosActualizados . ' registros.'); $output->writeln(' Se ignoraron ' . $ResultadoFinal->RegistrosIgnorados . ' registros.'); $output->writeln('Importación finalizada, se procesaron ' . $ResultadoFinal->TotalRegistrosProcesados() . ' registros.'); }
/** * Download */ protected function download(OutputInterface &$output, $from, $to) { $output->writeln('Download ' . $from); $progress = new ProgressBar($output); $progress->setFormat('normal_nomax'); $step = 0; $ctx = stream_context_create(array(), array('notification' => function ($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) use($output, $progress, &$step) { switch ($notification_code) { case STREAM_NOTIFY_FILE_SIZE_IS: $progress->start(100); break; case STREAM_NOTIFY_PROGRESS: $newStep = round($bytes_transferred / $bytes_max * 100); if ($newStep > $step) { $step = $newStep; $progress->setProgress($step); } break; } })); $file = file_get_contents($from, false, $ctx); $progress->finish(); file_put_contents($to, $file); $output->writeln(''); return $to; }
/** * Executes the current command. * * This method is not abstract because you can use this class * as a concrete class. In this case, instead of defining the * execute() method, you set the code to execute by passing * a Closure to the setCode() method. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return null|int null or 0 if everything went fine, or an error code * * @throws \LogicException When this abstract method is not implemented * * @see setCode() */ protected function execute(InputInterface $input, OutputInterface $output) { /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->getContainer()->get('doctrine.orm.' . $this->getContainer()->getParameter('geonames.entity_manager')); $connection = $em->getConnection(); $population = $this->getContainer()->getParameter('geonames.cities_population'); $quote = function ($value) use($connection) { return $connection->quote($value); }; // Disable SQL logger $connection->getConfiguration()->setSQLLogger(null); $output->writeln('<info>> Start update Geonames list</info>'); if (!in_array($population, [1000, 5000, 15000])) { $output->writeln('<error>False population value set in config "geonames.cities_population". Value must be one of 1000|5000|15000</error>'); } else { $fileName = 'cities' . $population . '.zip'; $file = $this->download($output, 'http://download.geonames.org/export/dump/' . $fileName, $this->getTempDir('/' . $fileName)); $output->writeln('Clear Geonames table'); $connection->query('SET FOREIGN_KEY_CHECKS=0'); $connection->executeUpdate($connection->getDatabasePlatform()->getTruncateTableSql('timiki_geonames')); $connection->query('SET FOREIGN_KEY_CHECKS=1'); $output->writeln('Load new Geonames list to table...wait'); $output->writeln('Processing downloaded data...'); // Prepare $handler = fopen('zip://' . $file . '#cities' . $population . '.txt', 'r'); $count = 0; while (!feof($handler)) { fgets($handler); $count++; } // rewind fclose($handler); $handler = fopen('zip://' . $file . '#cities' . $population . '.txt', 'r'); $progress = new ProgressBar($output); $progress->setFormat('normal_nomax'); $step = 0; $sql = ''; $progress->start($count); // Load to db while (!feof($handler)) { $step++; $line = fgets($handler); $explode = explode("\t", $line); if (count($explode) > 1) { $sql .= $connection->createQueryBuilder()->insert('timiki_geonames')->values(['geoname_id' => $quote(array_key_exists(0, $explode) ? $explode[0] : null), 'name' => $quote(array_key_exists(1, $explode) ? $explode[1] : null), 'ascii_name' => $quote(array_key_exists(2, $explode) ? $explode[2] : null), 'alternate_names' => $quote(array_key_exists(3, $explode) ? $explode[3] : null), 'latitude' => $quote(array_key_exists(4, $explode) ? $explode[4] : null), 'longitude' => $quote(array_key_exists(5, $explode) ? $explode[5] : null), 'feature_class' => $quote(array_key_exists(6, $explode) ? $explode[6] : null), 'feature_code' => $quote(array_key_exists(7, $explode) ? $explode[7] : null), 'country_code' => $quote(array_key_exists(8, $explode) ? $explode[8] : null), 'cc2' => $quote(array_key_exists(9, $explode) ? $explode[9] : null), 'admin1_code' => $quote(array_key_exists(10, $explode) ? $explode[10] : null), 'admin2_code' => $quote(array_key_exists(11, $explode) ? $explode[11] : null), 'admin3_code' => $quote(array_key_exists(12, $explode) ? $explode[12] : null), 'admin4_code' => $quote(array_key_exists(13, $explode) ? $explode[13] : null), 'population' => $quote(array_key_exists(14, $explode) ? $explode[14] : 0), 'elevation' => $quote(array_key_exists(15, $explode) ? $explode[15] : 0), 'dem' => $quote(array_key_exists(16, $explode) ? $explode[16] : 0), 'timezone' => $quote(array_key_exists(17, $explode) ? $explode[17] : null), 'modification_date' => $quote(array_key_exists(18, $explode) ? $explode[18] : null)])->getSQL() . ';'; if ($step % 1000 === 0) { $progress->setProgress($step); $connection->exec($sql); $sql = ''; } } } $progress->setProgress($step); $connection->exec($sql); fclose($handler); $output->writeln(''); $output->writeln('Done!'); } }
/** * Executes the current command. * * This method is not abstract because you can use this class * as a concrete class. In this case, instead of defining the * execute() method, you set the code to execute by passing * a Closure to the setCode() method. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return null|int null or 0 if everything went fine, or an error code * * @throws \LogicException When this abstract method is not implemented * * @see setCode() */ protected function execute(InputInterface $input, OutputInterface $output) { /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->getContainer()->get('doctrine.orm.' . $this->getContainer()->getParameter('geonames.entity_manager')); $connection = $em->getConnection(); $quote = function ($value) use($connection) { return $connection->quote($value); }; // Disable SQL logger $connection->getConfiguration()->setSQLLogger(null); $output->writeln('<info>> Start update Geonames alternate names list</info>'); $file = $this->download($output, 'http://download.geonames.org/export/dump/alternateNames.zip', $this->getTempDir('/alternateNames.zip')); $output->writeln('Clear Geonames alternate names table'); $connection->query('SET FOREIGN_KEY_CHECKS=0'); $connection->executeUpdate($connection->getDatabasePlatform()->getTruncateTableSql('timiki_geonames_alternate_names')); $connection->query('SET FOREIGN_KEY_CHECKS=1'); $output->writeln('Load new Geonames alternate names list to table...wait'); $output->writeln('Processing downloaded data...'); // Prepare $handler = fopen('zip://' . $file . '#alternateNames.txt', 'r'); $count = 0; while (!feof($handler)) { fgets($handler); $count++; } // rewind fclose($handler); $handler = fopen('zip://' . $file . '#alternateNames.txt', 'r'); $progress = new ProgressBar($output); $progress->setFormat('normal_nomax'); $step = 0; $sql = ''; $progress->start($count); // Output one line until end-of-file while (!feof($handler)) { $step++; $line = fgets($handler); $explode = explode("\t", $line); if (count($explode) > 1) { $isoLanguage = array_key_exists(2, $explode) ? $explode[2] : null; $isHistoric = array_key_exists(7, $explode) ? $explode[7] : null; $isShortName = array_key_exists(5, $explode) ? $explode[5] : null; // Not load not valid data if (!empty($isoLanguage) && $isoLanguage !== 'link' && $isoLanguage !== 'post' && $isoLanguage !== 'iata' && $isoLanguage !== 'icao' && $isoLanguage !== 'faac' && $isoLanguage !== 'fr_1793' && $isoLanguage !== 'abbr' && $isHistoric !== 1 && $isShortName !== 1) { $sql .= $connection->createQueryBuilder()->insert('timiki_geonames_alternate_names')->values(['alternate_name_id' => $quote(array_key_exists(0, $explode) ? $explode[0] : null), 'geoname_id' => $quote(array_key_exists(1, $explode) ? $explode[1] : null), 'iso_language' => $quote(array_key_exists(2, $explode) ? $explode[2] : null), 'alternate_name' => $quote(array_key_exists(3, $explode) ? $explode[3] : null), 'is_preferred_name' => $quote(array_key_exists(4, $explode) ? $explode[4] : null), 'is_short_name' => $quote(array_key_exists(5, $explode) ? $explode[5] : null), 'is_colloquial' => $quote(array_key_exists(6, $explode) ? $explode[6] : null), 'is_historic' => $quote(array_key_exists(7, $explode) ? $explode[7] : null)])->getSQL() . ';'; if ($step % 1000 === 0) { $progress->setProgress($step); $connection->exec($sql); $sql = ''; } } } } $progress->setProgress($step); $connection->exec($sql); fclose($handler); $output->writeln(''); $output->writeln('Done!'); }
/** * onCollectionBefore. * * @param CollectionEvent $event */ public function onCollectionBefore(CollectionEvent $event) { $target = $event->getTarget(); $this->output->writeln(sprintf('<info>[START]</info> Migrating %s to <comment>%s</comment>:', $event->getOptions()->isDirectionUp() ? 'up' : 'down', $target->getId())); if ($this->trackProgress) { $this->progress = new ProgressBar($this->output, $event->getProgress()->getTotal()); $this->progress->setFormat('verbose'); $this->progress->setProgress(0); } }
/** * {@inheritdoc} */ public function set_progress($task_lang_key, $task_number) { parent::set_progress($task_lang_key, $task_number); if ($this->progress_bar !== null) { $this->progress_bar->setProgress($this->current_task_progress); $this->progress_bar->setMessage($this->current_task_name); } else { $this->output->writeln(sprintf('[%3d/%-3d] %s', $this->current_task_progress, $this->task_progress_count, $this->current_task_name)); } }
/** * {@inheritdoc} */ protected function fire() { $tasks = $this->craft->tasks->getAllTasks(); if (!$tasks) { $this->info('No pending tasks.'); return; } foreach ($tasks as $task) { if ($task->status === TaskStatus::Running) { if ($this->option('reset-running')) { $this->resetTask($task); } else { continue; } } if ($task->status === TaskStatus::Error) { if ($this->option('reset-failed')) { $this->resetTask($task); } else { continue; } } try { $taskRecord = TaskRecord::model()->findById($task->id); $taskType = $task->getTaskType(); if (!$taskType) { throw new Exception('Could not find task type for task ID ' . $task->id); } $task->totalSteps = $taskType->getTotalSteps(); $task->status = TaskStatus::Running; $progressBar = new ProgressBar($this->output, $task->totalSteps); $this->info($task->description); for ($step = 0; $step < $task->totalSteps; $step++) { $task->currentStep = $step + 1; $this->craft->tasks->saveTask($task); $result = $taskType->runStep($step); if ($result !== true) { $error = is_string($result) ? $result : 'Unknown error'; $progressBar->finish(); $this->line(''); throw new Exception($error); } $progressBar->setProgress($task->currentStep); } $taskRecord->deleteNode(); $progressBar->finish(); $this->line(''); } catch (Exception $e) { $this->failTask($task); $this->error($e->getMessage()); } } $this->info('All tasks finished.'); }
/** * ProgressBar callback * @param $notificationCode * @param $severity * @param $message * @param $messageCode * @param $bytesTransferred * @param $bytesMax * @return void */ protected function showDownloadProgress($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) { switch ($notificationCode) { case STREAM_NOTIFY_FILE_SIZE_IS: if ($this->output) { $this->progressBar = new ProgressBar($this->output, $bytesMax); } break; case STREAM_NOTIFY_PROGRESS: if ($this->progressBar) { $this->progressBar->setProgress($bytesTransferred); } break; } }
/** * Watch for update progress and advance a progress bar. * * @param OutputInterface $output * @return void */ protected function watchProgress(OutputInterface $output) { $progress = new ProgressBar($output); $progress->setFormat('%bar% (%percent%%)'); $this->manager->on('request.start', function ($url, $bytes) use($progress, $output) { $output->writeln('<comment>Downloading ' . basename($url) . '</comment>'); $progress->start($bytes); }); $this->manager->on('progress', function ($transferred) use($progress) { $progress->setProgress($transferred); }); $this->manager->on('complete', function () use($progress, $output) { $progress->finish(); $output->writeln(''); }); }
/** * Download a file. Writes a progress bar to output * @param string $zipFile * @param OutputInterface $output * @return $this */ protected function download($zipFile, OutputInterface $output) { $client = new Client(array('base_url' => 'https://api.github.com')); $request = $client->createRequest('GET', '/repos/feejin/Silverstripe-CleanInstall/zipball/master'); $progressBar = new ProgressBar($output, 100); $progressBar->setFormat('<info>[%bar%]</info> <comment>%percent%%</comment>'); // Progress event - update the progress bar $request->getEmitter()->on('progress', function (ProgressEvent $event) use($progressBar) { if ($event->downloaded && $event->downloadSize) { $percent = round($event->downloaded / $event->downloadSize * 100); $progressBar->setProgress($percent); } }); $response = $client->send($request); $progressBar->finish(); file_put_contents($zipFile, $response->getBody()); return $this; }
protected function execute(InputInterface $input, OutputInterface $output) { $uuid = $input->getArgument('uuid'); $app = $this->getSilexApplication(); if ($uuid != null) { $job = $app['gearman.jobs']->job($uuid); } else { $helper = $this->getHelper('question'); $jobs = []; $opportunities = []; foreach ($app['gearman.jobs']->jobs(JobService::REFRESH) as $key => $job) { if ($job->getStatus()['known']) { $jobs[] = $job; $opportunities[] = '<comment>' . $job->getUUID() . '</comment> - <info>' . $job->getTask() . '</info> ' . $job->getWorkload(Job::WORKLOAD_JSON) . ($job->getResult() ? ' : <question>' . $job->getResult() . '</question>' : null); } } if (!$opportunities) { throw new Exception('No jobs running to watch'); } $whichJob = new ChoiceQuestion('Which job do you want to watch ?', $opportunities, null); $answer = $helper->ask($input, $output, $whichJob); $key = array_search($answer, $opportunities); $job = $jobs[$key]; } $output->writeln('Watching job <info>[' . $job->getUUID() . ']</info> :'); $progress = new ProgressBar($output, 100); $progress->start(); do { $job = $app['gearman.jobs']->refresh($job); $status = $job->getStatus(); if ($status['denominator'] != 0) { $multiplacator = 100 / $status['denominator']; $percent = $status['numerator'] * $multiplacator; $progress->setProgress($percent); } sleep(1); } while ($status['known']); $progress->finish(); $output->writeln(""); }
public function testNonDecoratedOutputWithClear() { $bar = new ProgressBar($output = $this->getOutputStream(false), 50); $bar->start(); $bar->setProgress(25); $bar->clear(); $bar->setProgress(50); $bar->finish(); rewind($output->getStream()); $this->assertEquals(' 0/50 [>---------------------------] 0%' . PHP_EOL . ' 25/50 [==============>-------------] 50%' . PHP_EOL . ' 50/50 [============================] 100%', stream_get_contents($output->getStream())); }
/** * Execute the upgrade command * * @param InputInterface $input input interface * @param OutputInterface $output output interface */ protected function execute(InputInterface $input, OutputInterface $output) { $simulateStepEnabled = true; $updateStepEnabled = true; $skip3rdPartyAppsDisable = false; if ($input->getOption('skip-migration-test')) { $simulateStepEnabled = false; } if ($input->getOption('dry-run')) { $updateStepEnabled = false; } if ($input->getOption('no-app-disable')) { $skip3rdPartyAppsDisable = true; } if (!$simulateStepEnabled && !$updateStepEnabled) { $output->writeln('<error>Only one of "--skip-migration-test" or "--dry-run" ' . 'can be specified at a time.</error>'); return self::ERROR_INVALID_ARGUMENTS; } if (\OC::checkUpgrade(false)) { if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { // Prepend each line with a little timestamp $timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter()); $output->setFormatter($timestampFormatter); } $self = $this; $updater = new Updater($this->config, \OC::$server->getIntegrityCodeChecker(), $this->logger); $updater->setSimulateStepEnabled($simulateStepEnabled); $updater->setUpdateStepEnabled($updateStepEnabled); $updater->setSkip3rdPartyAppsDisable($skip3rdPartyAppsDisable); $dispatcher = \OC::$server->getEventDispatcher(); $progress = new ProgressBar($output); $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%"); $listener = function ($event) use($progress, $output) { if ($event instanceof GenericEvent) { $message = $event->getSubject(); if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { $output->writeln(' Checking table ' . $message); } else { if (strlen($message) > 60) { $message = substr($message, 0, 57) . '...'; } $progress->setMessage($message); if ($event[0] === 1) { $output->writeln(''); $progress->start($event[1]); } $progress->setProgress($event[0]); if ($event[0] === $event[1]) { $progress->setMessage('Done'); $progress->finish(); $output->writeln(''); } } } }; $repairListener = function ($event) use($progress, $output) { if (!$event instanceof GenericEvent) { return; } switch ($event->getSubject()) { case '\\OC\\Repair::startProgress': $progress->setMessage('Starting ...'); $output->writeln($event->getArgument(1)); $output->writeln(''); $progress->start($event->getArgument(0)); break; case '\\OC\\Repair::advance': $desc = $event->getArgument(1); if (!empty($desc)) { $progress->setMessage($desc); } $progress->advance($event->getArgument(0)); break; case '\\OC\\Repair::finishProgress': $progress->setMessage('Done'); $progress->finish(); $output->writeln(''); break; case '\\OC\\Repair::step': if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { $output->writeln('<info>Repair step: ' . $event->getArgument(0) . '</info>'); } break; case '\\OC\\Repair::info': if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { $output->writeln('<info>Repair info: ' . $event->getArgument(0) . '</info>'); } break; case '\\OC\\Repair::warning': $output->writeln('<error>Repair warning: ' . $event->getArgument(0) . '</error>'); break; case '\\OC\\Repair::error': $output->writeln('<error>Repair error: ' . $event->getArgument(0) . '</error>'); break; } }; $dispatcher->addListener('\\OC\\DB\\Migrator::executeSql', $listener); $dispatcher->addListener('\\OC\\DB\\Migrator::checkTable', $listener); $dispatcher->addListener('\\OC\\Repair::startProgress', $repairListener); $dispatcher->addListener('\\OC\\Repair::advance', $repairListener); $dispatcher->addListener('\\OC\\Repair::finishProgress', $repairListener); $dispatcher->addListener('\\OC\\Repair::step', $repairListener); $dispatcher->addListener('\\OC\\Repair::info', $repairListener); $dispatcher->addListener('\\OC\\Repair::warning', $repairListener); $dispatcher->addListener('\\OC\\Repair::error', $repairListener); $updater->listen('\\OC\\Updater', 'maintenanceEnabled', function () use($output) { $output->writeln('<info>Turned on maintenance mode</info>'); }); $updater->listen('\\OC\\Updater', 'maintenanceDisabled', function () use($output) { $output->writeln('<info>Turned off maintenance mode</info>'); }); $updater->listen('\\OC\\Updater', 'maintenanceActive', function () use($output) { $output->writeln('<info>Maintenance mode is kept active</info>'); }); $updater->listen('\\OC\\Updater', 'updateEnd', function ($success) use($output, $updateStepEnabled, $self) { $mode = $updateStepEnabled ? 'Update' : 'Update simulation'; if ($success) { $message = "<info>{$mode} successful</info>"; } else { $message = "<error>{$mode} failed</error>"; } $output->writeln($message); }); $updater->listen('\\OC\\Updater', 'dbUpgradeBefore', function () use($output) { $output->writeln('<info>Updating database schema</info>'); }); $updater->listen('\\OC\\Updater', 'dbUpgrade', function () use($output) { $output->writeln('<info>Updated database</info>'); }); $updater->listen('\\OC\\Updater', 'dbSimulateUpgradeBefore', function () use($output) { $output->writeln('<info>Checking whether the database schema can be updated (this can take a long time depending on the database size)</info>'); }); $updater->listen('\\OC\\Updater', 'dbSimulateUpgrade', function () use($output) { $output->writeln('<info>Checked database schema update</info>'); }); $updater->listen('\\OC\\Updater', 'incompatibleAppDisabled', function ($app) use($output) { $output->writeln('<info>Disabled incompatible app: ' . $app . '</info>'); }); $updater->listen('\\OC\\Updater', 'thirdPartyAppDisabled', function ($app) use($output) { $output->writeln('<info>Disabled 3rd-party app: ' . $app . '</info>'); }); $updater->listen('\\OC\\Updater', 'upgradeAppStoreApp', function ($app) use($output) { $output->writeln('<info>Update 3rd-party app: ' . $app . '</info>'); }); $updater->listen('\\OC\\Updater', 'appUpgradeCheckBefore', function () use($output) { $output->writeln('<info>Checking updates of apps</info>'); }); $updater->listen('\\OC\\Updater', 'appSimulateUpdate', function ($app) use($output) { $output->writeln("<info>Checking whether the database schema for <{$app}> can be updated (this can take a long time depending on the database size)</info>"); }); $updater->listen('\\OC\\Updater', 'appUpgradeCheck', function () use($output) { $output->writeln('<info>Checked database schema update for apps</info>'); }); $updater->listen('\\OC\\Updater', 'appUpgradeStarted', function ($app, $version) use($output) { $output->writeln("<info>Updating <{$app}> ...</info>"); }); $updater->listen('\\OC\\Updater', 'appUpgrade', function ($app, $version) use($output) { $output->writeln("<info>Updated <{$app}> to {$version}</info>"); }); $updater->listen('\\OC\\Updater', 'failure', function ($message) use($output, $self) { $output->writeln("<error>{$message}</error>"); }); $updater->listen('\\OC\\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use($output) { $output->writeln("<info>Set log level to debug</info>"); }); $updater->listen('\\OC\\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($output) { $output->writeln("<info>Reset log level</info>"); }); $updater->listen('\\OC\\Updater', 'startCheckCodeIntegrity', function () use($output) { $output->writeln("<info>Starting code integrity check...</info>"); }); $updater->listen('\\OC\\Updater', 'finishedCheckCodeIntegrity', function () use($output) { $output->writeln("<info>Finished code integrity check</info>"); }); $success = $updater->upgrade(); $this->postUpgradeCheck($input, $output); if (!$success) { return self::ERROR_FAILURE; } return self::ERROR_SUCCESS; } else { if ($this->config->getSystemValue('maintenance', false)) { //Possible scenario: ownCloud core is updated but an app failed $output->writeln('<warning>ownCloud is in maintenance mode</warning>'); $output->write('<comment>Maybe an upgrade is already in process. Please check the ' . 'logfile (data/owncloud.log). If you want to re-run the ' . 'upgrade procedure, remove the "maintenance mode" from ' . 'config.php and call this script again.</comment>', true); return self::ERROR_MAINTENANCE_MODE; } else { $output->writeln('<info>ownCloud is already latest version</info>'); return self::ERROR_UP_TO_DATE; } } }
/** * Show progress bar and run the loop * * @param string $name * @param int $total * @param int $stepSize * @param \Closure $callback */ protected function _progressBar($name, $total, $stepSize, $callback) { $this->_('Current progress of ' . $name . ' (Wait! or `Ctrl+C` to cancel):'); $progressBar = new ProgressBar($this->_out, $total); $progressBar->display(); $progressBar->setRedrawFrequency(1); for ($currentStep = 0; $currentStep <= $total; $currentStep += $stepSize) { $callbackResult = $callback($currentStep, $stepSize); if ($callbackResult === false) { break; } $progressBar->setProgress($currentStep); } $progressBar->finish(); $this->_(''); // Progress bar hack for rendering }
protected function finishProgressBar() { $this->progressBar->setProgress(100); $this->progressBar->finish(); }
/** * Download the temporary Zip to the given file. * * @param string $zipFile * @return $this */ protected function download($zipFile) { $uploadProgress = function () { }; $progressBar = new ProgressBar($this->output, 100); $progressBar->start(); $downloadProgress = function ($expected, $total, $client, $request, $res) use($progressBar) { $progressBar->setProgress(floor(100 * ($total / $expected))); }; $progress = new Progress($uploadProgress, $downloadProgress); $response = (new Client())->get('https://wordpress.org/latest.zip', ['subscribers' => [$progress]]); $progressBar->finish(); $this->output->writeln(''); file_put_contents($zipFile, $response->getBody()); return $this; }
/** * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the * available operating system uncompressing commands and the enabled PHP extensions * and it downloads the file. * * @throws \RuntimeException if the ProcessWire archive could not be downloaded */ private function download() { $this->output->writeln("\n Downloading ProcessWire Version " . $this->branch['version'] . "..."); $distill = new Distill(); $pwArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($this->branch['zipURL'])->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar) { // Don't initialize the progress bar for redirects as the size is much smaller if ($response->getStatusCode() >= 300) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getStep()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($this->output, $size); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($size / 1000))); $progressBar->setBarWidth(60); if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 $progressBar->setProgressCharacter(''); $progressBar->setBarCharacter('▓'); // dark shade character \u2593 } $progressBar->start(); } $progressBar->setProgress($downloaded); }; $client = new Client(); $client->getEmitter()->attach(new Progress(null, $downloadCallback)); // store the file in a temporary hidden directory with a random name $this->compressedFilePath = getcwd() . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'pw.' . pathinfo($pwArchiveFile, PATHINFO_EXTENSION); try { $response = $client->get($pwArchiveFile); } catch (ClientException $e) { if ($e->getCode() === 403 || $e->getCode() === 404) { throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Try the special \"latest\" version to install the latest stable ProcessWire release:\n" . '%s %s %s latest', $this->version, $_SERVER['PHP_SELF'], $this->getName(), $this->projectDir)); } else { throw new \RuntimeException(sprintf("The selected version (%s) couldn't be downloaded because of the following error:\n%s", $this->version, $e->getMessage())); } } $this->fs->dumpFile($this->compressedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $this->output->writeln("\n"); } return $this; }
/** * Find and trigger the negative events, i.e. the events with a no decision path * * @param Campaign $campaign * @param int $totalEventCount * @param int $limit * @param bool $max * @param OutputInterface $output * @param bool|false $returnCounts If true, returns array of counters * * @return int */ public function triggerNegativeEvents($campaign, &$totalEventCount = 0, $limit = 100, $max = false, OutputInterface $output = null, $returnCounts = false) { defined('MAUTIC_CAMPAIGN_SYSTEM_TRIGGERED') or define('MAUTIC_CAMPAIGN_SYSTEM_TRIGGERED', 1); $logger = $this->factory->getLogger(); $logger->debug('CAMPAIGN: Triggering negative events'); $campaignId = $campaign->getId(); $campaignName = $campaign->getName(); $repo = $this->getRepository(); $campaignRepo = $this->getCampaignRepository(); /** @var \Mautic\CampaignBundle\Model\CampaignModel $campaignModel */ $campaignModel = $this->factory->getModel('campaign'); /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */ $leadModel = $this->factory->getModel('lead'); // Get events to avoid large number of joins $campaignEvents = $repo->getCampaignEvents($campaignId); // Get an array of events that are non-action based $nonActionEvents = array(); $actionEvents = array(); foreach ($campaignEvents as $id => $e) { if (!empty($e['decisionPath']) && !empty($e['parent_id']) && $campaignEvents[$e['parent_id']]['eventType'] != 'condition') { if ($e['decisionPath'] == 'no') { $nonActionEvents[$e['parent_id']][$id] = $e; } elseif ($e['decisionPath'] == 'yes') { $actionEvents[$e['parent_id']][] = $id; } } } $logger->debug('CAMPAIGN: Processing the children of the following events: ' . implode(', ', array_keys($nonActionEvents))); if (empty($nonActionEvents)) { // No non-action events associated with this campaign unset($campaignEvents); return 0; } // Get a count $leadCount = $campaignRepo->getCampaignLeadCount($campaignId); if ($output) { $output->writeln($this->translator->trans('mautic.campaign.trigger.lead_count_analyzed', array('%leads%' => $leadCount, '%batch%' => $limit))); } $start = $leadProcessedCount = $lastRoundPercentage = $executedEventCount = $evaluatedEventCount = $negativeExecutedCount = $negativeEvaluatedCount = 0; $nonActionEventCount = $leadCount * count($nonActionEvents); $eventSettings = $campaignModel->getEvents(); $maxCount = $max ? $max : $nonActionEventCount; // Try to save some memory gc_enable(); if ($leadCount) { if ($output) { $progress = new ProgressBar($output, $maxCount); $progress->start(); if ($max) { $progress->advance($totalEventCount); } } $sleepBatchCount = 0; $batchDebugCounter = 1; while ($start <= $leadCount) { $logger->debug('CAMPAIGN: Batch #' . $batchDebugCounter); // Get batched campaign ids $campaignLeads = $campaignRepo->getCampaignLeads($campaignId, $start, $limit, array('cl.lead_id, cl.date_added')); $campaignLeadIds = array(); $campaignLeadDates = array(); foreach ($campaignLeads as $r) { $campaignLeadIds[] = $r['lead_id']; $campaignLeadDates[$r['lead_id']] = $r['date_added']; } unset($campaignLeads); $logger->debug('CAMPAIGN: Processing the following contacts: ' . implode(', ', $campaignLeadIds)); foreach ($nonActionEvents as $parentId => $events) { // Just a check to ensure this is an appropriate action if ($campaignEvents[$parentId]['eventType'] == 'action') { $logger->debug('CAMPAIGN: Parent event ID #' . $parentId . ' is an action.'); continue; } // Get only leads who have had the action prior to the decision executed $grandParentId = $campaignEvents[$parentId]['parent_id']; // Get the lead log for this batch of leads limiting to those that have already triggered // the decision's parent and haven't executed this level in the path yet if ($grandParentId) { $logger->debug('CAMPAIGN: Checking for contacts based on grand parent execution.'); $leadLog = $repo->getEventLog($campaignId, $campaignLeadIds, array($grandParentId), array_keys($events), true); $applicableLeads = array_keys($leadLog); } else { $logger->debug('CAMPAIGN: Checking for contacts based on exclusion due to being at root level'); // The event has no grandparent (likely because the decision is first in the campaign) so find leads that HAVE // already executed the events in the root level and exclude them $havingEvents = isset($actionEvents[$parentId]) ? array_merge($actionEvents[$parentId], array_keys($events)) : array_keys($events); $leadLog = $repo->getEventLog($campaignId, $campaignLeadIds, $havingEvents); $unapplicableLeads = array_keys($leadLog); // Only use leads that are not applicable $applicableLeads = array_diff($campaignLeadIds, $unapplicableLeads); unset($unapplicableLeads); } if (empty($applicableLeads)) { $logger->debug('CAMPAIGN: No events are applicable'); continue; } $logger->debug('CAMPAIGN: These contacts have have not gone down the positive path: ' . implode(', ', $applicableLeads)); // Get the leads $leads = $leadModel->getEntities(array('filter' => array('force' => array(array('column' => 'l.id', 'expr' => 'in', 'value' => $applicableLeads))), 'orderBy' => 'l.id', 'orderByDir' => 'asc')); if (!count($leads)) { // Just a precaution in case non-existent leads are lingering in the campaign leads table $logger->debug('CAMPAIGN: No contact entities found.'); continue; } // Loop over the non-actions and determine if it has been processed for this lead $leadDebugCounter = 1; /** @var \Mautic\LeadBundle\Entity\Lead $lead */ foreach ($leads as $lead) { $negativeEvaluatedCount++; // Set lead for listeners $leadModel->setSystemCurrentLead($lead); $logger->debug('CAMPAIGN: contact ID #' . $lead->getId() . '; #' . $leadDebugCounter . ' in batch #' . $batchDebugCounter); // Prevent path if lead has already gone down this path if (!isset($leadLog[$lead->getId()]) || !array_key_exists($parentId, $leadLog[$lead->getId()])) { // Get date to compare against $utcDateString = $grandParentId ? $leadLog[$lead->getId()][$grandParentId]['date_triggered'] : $campaignLeadDates[$lead->getId()]; // Convert to local DateTime $grandParentDate = $this->factory->getDate($utcDateString, 'Y-m-d H:i:s', 'UTC')->getLocalDateTime(); // Non-decision has not taken place yet, so cycle over each associated action to see if timing is right $eventTiming = array(); $executeAction = false; foreach ($events as $id => $e) { if ($sleepBatchCount == $limit) { // Keep CPU down $this->batchSleep(); $sleepBatchCount = 0; } else { $sleepBatchCount++; } if (isset($leadLog[$lead->getId()]) && array_key_exists($id, $leadLog[$lead->getId()])) { $logger->debug('CAMPAIGN: Event (ID #' . $id . ') has already been executed'); unset($e); continue; } if (!isset($eventSettings[$e['eventType']][$e['type']])) { $logger->debug('CAMPAIGN: Event (ID #' . $id . ') no longer exists'); unset($e); continue; } // First get the timing for all the 'non-decision' actions $eventTiming[$id] = $this->checkEventTiming($e, $grandParentDate, true); if ($eventTiming[$id] === true) { // Includes events to be executed now then schedule the rest if applicable $executeAction = true; } unset($e); } if (!$executeAction) { $negativeEvaluatedCount += count($nonActionEvents); // Timing is not appropriate so move on to next lead unset($eventTiming); continue; } if ($max && $totalEventCount + count($nonActionEvents) >= $max) { // Hit the max or will hit the max while mid-process for the lead if ($output) { $progress->finish(); $output->writeln(''); } $counts = array('events' => $nonActionEventCount, 'evaluated' => $negativeEvaluatedCount, 'executed' => $negativeExecutedCount, 'totalEvaluated' => $evaluatedEventCount, 'totalExecuted' => $executedEventCount); $logger->debug('CAMPAIGN: Counts - ' . var_export($counts, true)); return $returnCounts ? $counts : $executedEventCount; } $decisionLogged = false; // Execute or schedule events $logger->debug('CAMPAIGN: Processing the following events for contact ID# ' . $lead->getId() . ': ' . implode(', ', array_keys($eventTiming))); foreach ($eventTiming as $id => $eventTriggerDate) { // Set event $event = $events[$id]; $event['campaign'] = array('id' => $campaignId, 'name' => $campaignName); // Set lead in case this is triggered by the system $leadModel->setSystemCurrentLead($lead); if ($this->executeEvent($event, $campaign, $lead, $eventSettings, false, null, $eventTriggerDate, false, $evaluatedEventCount, $executedEventCount, $totalEventCount)) { if (!$decisionLogged) { // Log the decision $log = $this->getLogEntity($parentId, $campaign, $lead, null, true); $log->setDateTriggered(new \DateTime()); $log->setNonActionPathTaken(true); $repo->saveEntity($log); $this->em->detach($log); unset($log); $decisionLogged = true; } $negativeExecutedCount++; } unset($utcDateString, $grandParentDate); } } else { $logger->debug('CAMPAIGN: Decision has already been executed.'); } $currentCount = $max ? $totalEventCount : $negativeEvaluatedCount; if ($output && $currentCount < $maxCount) { $progress->setProgress($currentCount); } $leadDebugCounter++; // Save RAM $this->em->detach($lead); unset($lead); } } // Next batch $start += $limit; // Save RAM $this->em->clear('Mautic\\LeadBundle\\Entity\\Lead'); $this->em->clear('Mautic\\UserBundle\\Entity\\User'); unset($leads, $campaignLeadIds, $leadLog); $currentCount = $max ? $totalEventCount : $negativeEvaluatedCount; if ($output && $currentCount < $maxCount) { $progress->setProgress($currentCount); } // Free some memory gc_collect_cycles(); $batchDebugCounter++; } if ($output) { $progress->finish(); $output->writeln(''); } } $counts = array('events' => $nonActionEventCount, 'evaluated' => $negativeEvaluatedCount, 'executed' => $negativeExecutedCount, 'totalEvaluated' => $evaluatedEventCount, 'totalExecuted' => $executedEventCount); $logger->debug('CAMPAIGN: Counts - ' . var_export($counts, true)); return $returnCounts ? $counts : $executedEventCount; }
/** * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the * available operating system uncompressing commands and the enabled PHP extensions * and it downloads the file. * * @return $this * * @throws \RuntimeException If the Symfony archive could not be downloaded */ protected function download() { $this->output->writeln(sprintf("\n Downloading %s...\n", $this->getDownloadedApplicationType())); // decide which is the best compressed version to download $distill = new Distill(); $symfonyArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFilesWithDifferentExtensions($this->getRemoteFileUrl(), ['tgz', 'zip'])->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function (ProgressEvent $event) use(&$progressBar) { $downloadSize = $event->downloadSize; $downloaded = $event->downloaded; // progress bar is only displayed for files larger than 1MB if ($downloadSize < 1 * 1024 * 1024) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getProgress()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($this->output, $downloadSize); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($downloadSize / 1000))); $progressBar->setBarWidth(60); if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 $progressBar->setProgressCharacter(''); $progressBar->setBarCharacter('▓'); // dark shade character \u2593 } $progressBar->start(); } $progressBar->setProgress($downloaded); }; $client = $this->getGuzzleClient(); // store the file in a temporary hidden directory with a random name $this->downloadedFilePath = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'symfony.' . pathinfo($symfonyArchiveFile, PATHINFO_EXTENSION); try { $request = $client->createRequest('GET', $symfonyArchiveFile); $request->getEmitter()->on('progress', $downloadCallback); $response = $client->send($request); } catch (ClientException $e) { if ('new' === $this->getName() && ($e->getCode() === 403 || $e->getCode() === 404)) { throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Execute the following command to install the latest stable Symfony release:\n" . '%s new %s', $this->version, $_SERVER['PHP_SELF'], str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $this->projectDir))); } else { throw new \RuntimeException(sprintf("There was an error downloading %s from symfony.com server:\n%s", $this->getDownloadedApplicationType(), $e->getMessage()), null, $e); } } $this->fs->dumpFile($this->downloadedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $this->output->writeln("\n"); } return $this; }
protected function execute(InputInterface $input, OutputInterface $output) { if (posix_geteuid() != 0) { $output->writeln("<error>" . _("You need to be root to run this command") . "</error>"); exit(1); } $options = $input->getOptions(); $args = $input->getArgument('args'); $pre = $this->preAsteriskHooks($output, false); $post = $this->postAsteriskHooks($output, false); $aststat = $this->asteriskProcess(); $asteriskrunning = $aststat[0]; $bmo = \FreePBX::create(); $maxwait = (int) $options['maxwait']; if ($maxwait < 5) { $maxwait = 30; } if ($maxwait > 3600) { // 1 hour $maxwait = 3600; } // We were asked to only run the pre-stop hooks? if ($options['pre']) { // Note: Do not i18n. $output->writeln("Only running pre-hooks"); $runpre = true; $stopasterisk = false; $runpost = false; } elseif ($options['post']) { // Note: Do not i18n. $output->writeln("Only running post-hooks"); $runpre = false; $stopasterisk = false; $runpost = true; } else { // Run both $runpre = true; $stopasterisk = true; $runpost = true; } // Do we have any params? if ($args) { // We do. Create a temporary array with our hooks, using the ones // we've been asked to do. $stopasterisk = false; $newpre = array(); $newpost = array(); foreach ($args as $v) { if ($runpre) { foreach ($pre as $pri => $data) { if (strtolower($data['module']) == strtolower($v)) { $newpre[$pri] = $data; } } } if ($runpost) { foreach ($post as $pri => $data) { if (strtolower($data['module']) == strtolower($v)) { $newpost[$pri] = $data; } } } } // And overwrite our hooks to run later $pre = $newpre; $post = $newpost; } if ($stopasterisk && !$asteriskrunning) { $output->writeln("<error>Asterisk not currently running</error>"); $stopasterisk = false; } // Now we're ready to go. $brand = \FreePBX::Config()->get("DASHBOARD_FREEPBX_BRAND"); $output->writeln(sprintf(_('Running %s shutdown...'), $brand)); $output->writeln(''); if ($runpre) { foreach ($pre as $pri => $data) { $bmo->{$data}['module']->{$data}['method']($output); } } if ($stopasterisk) { $astman = \FreePBX::create()->astman; $astman->disconnect(); if ($options['immediate']) { $output->writeln(_('Shutting down Asterisk Immediately...')); $this->stopAsterisk($output, 'now'); } else { $output->writeln(sprintf(_('Shutting down Asterisk Gracefully. Will forcefully kill after %s seconds.'), $maxwait)); // Let people force the shutdown if they want $output->writeln(sprintf(_('Press %s to Cancel'), 'C')); $output->writeln(sprintf(_('Press %s to shut down NOW'), 'N')); // Wait for up to $maxwait before killing it hard $killafter = time() + $maxwait; $starttime = time(); // Seconds may have ticked over between the two time() calls, which is why // we recalculate. $pct = 100 / ($killafter - $starttime); if (!$output->isQuiet()) { stream_set_blocking(STDIN, 0); $term = `stty -g`; system("stty -icanon -echo"); } $progress = new ProgressBar($output, 0); $progress->setFormat('[%bar%] %elapsed%'); $this->stopAsterisk($output, 'gracefully'); $progress->start(); $isrunning = true; while (time() < $killafter) { if (!$output->isQuiet()) { $res = fread(STDIN, 1); if ($res) { if (strtolower($res) === "c") { $progress->finish(); print "\n"; $output->writeln(_('Aborting Shutdown. Asterisk is still running')); $this->abortShutdown($output); system("stty {$term}"); exit(1); } elseif (strtolower($res) === "n") { print "\n"; $output->writeln(_('Killing asterisk forcefully.')); $this->stopAsterisk($output, 'now'); } } } $current = (int) (time() - $starttime) * $pct; $progress->setProgress($current); $aststat = $this->asteriskProcess(); $asteriskrunning = $aststat[0]; if (!$asteriskrunning) { $progress->setProgress(100); $isrunning = false; break; } fflush(STDOUT); usleep(10000); } $progress->finish(); // Re-block the stream if (!$output->isQuiet()) { stream_set_blocking(STDIN, 1); system("stty {$term}"); } if ($isrunning) { $output->writeln(""); $output->writeln(_('Killing asterisk forcefully.')); $this->stopAsterisk($output, 'now'); } } } $output->writeln(""); if ($runpost) { foreach ($post as $pri => $data) { $bmo->{$data}['module']->{$data}['method']($output); } } }
/** * {@inheritdoc} */ protected function fire() { $indexes = array(); $sessionId = craft()->assetIndexing->getIndexingSessionId(); $this->info('Fetching sources...'); $sources = craft()->assetSources->getAllSources(); $sourceIds = $this->argument('sourceIds'); if ($sourceIds) { $sources = array_filter($sources, function ($source) use($sourceIds) { return in_array($source->id, $sourceIds); }); } else { $sourceIds = array_map(function ($source) { return $source->id; }, $sources); } $missingFolders = array(); $grandTotal = 0; $this->info('Fetching indexes...'); foreach ($sourceIds as $sourceId) { // Get the indexing list $indexList = craft()->assetIndexing->getIndexListForSource($sessionId, $sourceId); if (!empty($indexList['error'])) { $this->error($indexList['error']); return; } if (isset($indexList['missingFolders'])) { $missingFolders += $indexList['missingFolders']; } $grandTotal += $indexList['total']; $indexes[$sourceId] = $indexList['total']; } $this->info('Updating indexes...'); $progressBar = new ProgressBar($this->output, $grandTotal); $count = 0; // Index the file foreach ($indexes as $sourceId => $total) { for ($i = 0; $i < $total; $i++) { craft()->assetIndexing->processIndexForSource($sessionId, $i, $sourceId); $progressBar->setProgress(++$count); } } $progressBar->finish(); $this->line(''); $this->info('Deleting stale index data...'); // Clean up stale indexing data (all sessions that have all recordIds set) $sessionsInProgress = craft()->db->createCommand()->select('sessionId')->from('assetindexdata')->where('recordId IS NULL')->group('sessionId')->queryScalar(); if (empty($sessionsInProgress)) { craft()->db->createCommand()->delete('assetindexdata'); } else { craft()->db->createCommand()->delete('assetindexdata', array('not in', 'sessionId', $sessionsInProgress)); } if ($this->option('delete-missing-files')) { $this->info('Deleting missing files...'); $missingFiles = craft()->assetIndexing->getMissingFiles($sourceIds, $sessionId); if ($missingFiles) { craft()->assetIndexing->removeObsoleteFileRecords(array_keys($missingFiles)); } } if ($missingFolders && $this->option('delete-missing-folders')) { $this->info('Deleting missing folders...'); craft()->assetIndexing->removeObsoleteFolderRecords(array_keys($missingFolders)); } $this->info('Asset indexes have been updated.'); }
/** * Rebuild lead lists * * @param LeadList $entity * @param int $limit * @param bool $maxLeads * @param OutputInterface $output * * @return int */ public function rebuildListLeads(LeadList $entity, $limit = 1000, $maxLeads = false, OutputInterface $output = null) { defined('MAUTIC_REBUILDING_LEAD_LISTS') or define('MAUTIC_REBUILDING_LEAD_LISTS', 1); $id = $entity->getId(); $list = array('id' => $id, 'filters' => $entity->getFilters()); $dtHelper = $this->factory->getDate(); $batchLimiters = array('dateTime' => $dtHelper->toUtcString()); $localDateTime = $dtHelper->getLocalDateTime(); // Get a count of leads to add $newLeadsCount = $this->getLeadsByList($list, true, array('countOnly' => true, 'newOnly' => true, 'batchLimiters' => $batchLimiters)); // Ensure the same list is used each batch $batchLimiters['maxId'] = (int) $newLeadsCount[$id]['maxId']; // Number of total leads to process $leadCount = (int) $newLeadsCount[$id]['count']; if ($output) { $output->writeln($this->translator->trans('mautic.lead.list.rebuild.to_be_added', array('%leads%' => $leadCount, '%batch%' => $limit))); } // Handle by batches $start = $lastRoundPercentage = $leadsProcessed = 0; // Try to save some memory gc_enable(); if ($leadCount) { $maxCount = $maxLeads ? $maxLeads : $leadCount; if ($output) { $progress = new ProgressBar($output, $maxCount); $progress->start(); } // Add leads while ($start < $leadCount) { // Keep CPU down for large lists; sleep per $limit batch $this->batchSleep(); $newLeadList = $this->getLeadsByList($list, true, array('newOnly' => true, 'limit' => $limit, 'batchLimiters' => $batchLimiters)); if (empty($newLeadList[$id])) { // Somehow ran out of leads so break out break; } foreach ($newLeadList[$id] as $l) { $this->addLead($l, $entity, false, true, -1, $localDateTime); unset($l); $leadsProcessed++; if ($output && $leadsProcessed < $maxCount) { $progress->setProgress($leadsProcessed); } if ($maxLeads && $leadsProcessed >= $maxLeads) { break; } } $start += $limit; // Dispatch batch event if ($this->dispatcher->hasListeners(LeadEvents::LEAD_LIST_BATCH_CHANGE)) { $event = new ListChangeEvent($newLeadList[$id], $entity, true); $this->dispatcher->dispatch(LeadEvents::LEAD_LIST_BATCH_CHANGE, $event); unset($event); } unset($newLeadList); // Free some memory gc_collect_cycles(); if ($maxLeads && $leadsProcessed >= $maxLeads) { if ($output) { $progress->finish(); $output->writeln(''); } return $leadsProcessed; } } if ($output) { $progress->finish(); $output->writeln(''); } } // Get a count of leads to be removed $removeLeadCount = $this->getLeadsByList($list, true, array('countOnly' => true, 'nonMembersOnly' => true, 'batchLimiters' => $batchLimiters)); // Restart batching $start = $lastRoundPercentage = 0; $leadCount = $removeLeadCount[$id]['count']; if ($output) { $output->writeln($this->translator->trans('mautic.lead.list.rebuild.to_be_removed', array('%leads%' => $leadCount, '%batch%' => $limit))); } if ($leadCount) { $maxCount = $maxLeads ? $maxLeads : $leadCount; if ($output) { $progress = new ProgressBar($output, $maxCount); $progress->start(); } // Remove leads while ($start < $leadCount) { // Keep CPU down for large lists; sleep per $limit batch $this->batchSleep(); $removeLeadList = $this->getLeadsByList($list, true, array('limit' => $limit, 'nonMembersOnly' => true, 'batchLimiters' => $batchLimiters)); if (empty($removeLeadList[$id])) { // Somehow ran out of leads so break out break; } foreach ($removeLeadList[$id] as $l) { $this->removeLead($l, $entity, false, true, true); $leadsProcessed++; if ($output && $leadsProcessed < $maxCount) { $progress->setProgress($leadsProcessed); } if ($maxLeads && $leadsProcessed >= $maxLeads) { break; } } // Dispatch batch event if ($this->dispatcher->hasListeners(LeadEvents::LEAD_LIST_BATCH_CHANGE)) { $event = new ListChangeEvent($removeLeadList[$id], $entity, false); $this->dispatcher->dispatch(LeadEvents::LEAD_LIST_BATCH_CHANGE, $event); unset($event); } $start += $limit; unset($removeLeadList); // Free some memory gc_collect_cycles(); if ($maxLeads && $leadsProcessed >= $maxLeads) { if ($output) { $progress->finish(); $output->writeln(''); } return $leadsProcessed; } } if ($output) { $progress->finish(); $output->writeln(''); } } return $leadsProcessed; }
/** * @param TransactionDatabase $db * @param string $type * * @return bool */ private function loadTable(TransactionDatabase $db, string $type) : bool { $type = strtolower($type); $insertSize = 5000; $insertTotal = sizeof($this->{$type}); $this->output->writeln('Loading ' . $type); $sql = 'INSERT INTO tbl_geo_' . strtolower($type); $sql .= '(' . implode(', ', constant('self::' . strtoupper($type) . '_COLUMNS')) . ')'; $sql .= ' VALUES '; $rowFormat = function (&$col) { if (is_null($col)) { $col = 'NULL'; } elseif (!is_numeric($col)) { $col = "'" . str_replace("'", "\\'", $col) . "'"; } }; $progress = new ProgressBar($this->output, 100); $progress->setMessage(0, 'currentsize'); $progress->setMessage($insertTotal, 'totalsize'); $progress->setFormat('<comment>[%bar%]</comment> %currentsize:6s% mo/%totalsize:6s% mo ' . '<info>%percent:3s%%</info> %elapsed:6s%/%estimated:-6s%'); if ($this->output->isVerbose()) { $progress->start(); } foreach (array_chunk($this->{$type}, $insertSize) as $i => $blocks) { $chunkSql = $sql; foreach ($blocks as $row) { array_walk($row, $rowFormat); $chunkSql .= '(' . implode(', ', $row) . '),'; } if ($this->output->isVerbose()) { $progress->setMessage($insertSize * $i, 'currentsize'); $progress->setProgress(floor($insertSize * $i * 100 / $insertTotal)); } $db->query(substr($chunkSql, 0, -1)); } if ($this->output->isVerbose()) { $progress->finish(); $this->output->write("\n"); } return true; }