[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> TasksRunCommand.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System.
   5   *
   6   * @copyright  (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Console;
  11  
  12  use Joomla\Component\Scheduler\Administrator\Scheduler\Scheduler;
  13  use Joomla\Component\Scheduler\Administrator\Task\Status;
  14  use Joomla\Console\Command\AbstractCommand;
  15  use Symfony\Component\Console\Exception\InvalidArgumentException;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Input\InputOption;
  18  use Symfony\Component\Console\Output\OutputInterface;
  19  use Symfony\Component\Console\Style\SymfonyStyle;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('JPATH_PLATFORM') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Console command to run scheduled tasks.
  27   *
  28   * @since 4.1.0
  29   */
  30  class TasksRunCommand extends AbstractCommand
  31  {
  32      /**
  33       * The default command name
  34       *
  35       * @var    string
  36       * @since  4.1.0
  37       */
  38      protected static $defaultName = 'scheduler:run';
  39  
  40      /**
  41       * @var SymfonyStyle
  42       * @since  4.1.0
  43       */
  44      private $ioStyle;
  45  
  46      /**
  47       * @param   InputInterface   $input   The input to inject into the command.
  48       * @param   OutputInterface  $output  The output to inject into the command.
  49       *
  50       * @return integer The command exit code.
  51       *
  52       * @since 4.1.0
  53       * @throws \RunTimeException
  54       * @throws InvalidArgumentException
  55       */
  56      protected function doExecute(InputInterface $input, OutputInterface $output): int
  57      {
  58          /**
  59           * Not as a class constant because of some the autoload order doesn't let us
  60           * load the namespace when it's time to do that (why?)
  61           */
  62          static $outTextMap = [
  63              Status::OK          => 'Task#%1$02d \'%2$s\' processed in %3$.2f seconds.',
  64              Status::WILL_RESUME => '<notice>Task#%1$02d \'%2$s\' ran for %3$.2f seconds, will resume next time.</notice>',
  65              Status::NO_RUN      => '<warning>Task#%1$02d \'%2$s\' failed to run. Is it already running?</warning>',
  66              Status::NO_ROUTINE  => '<error>Task#%1$02d \'%2$s\' is orphaned! Visit the backend to resolve.</error>',
  67              'N/A'               => '<error>Task#%1$02d \'%2$s\' exited with code %4$d in %3$.2f seconds.</error>',
  68          ];
  69  
  70          $this->configureIo($input, $output);
  71          $this->ioStyle->title('Run Tasks');
  72  
  73          $scheduler = new Scheduler();
  74  
  75          $id    = $input->getOption('id');
  76          $all   = $input->getOption('all');
  77  
  78          if ($id) {
  79              $records[] = $scheduler->fetchTaskRecord($id);
  80          } else {
  81              $filters             = $scheduler::TASK_QUEUE_FILTERS;
  82              $listConfig          = $scheduler::TASK_QUEUE_LIST_CONFIG;
  83              $listConfig['limit'] = ($all ? null : 1);
  84  
  85              $records = $scheduler->fetchTaskRecords($filters, $listConfig);
  86          }
  87  
  88          if ($id && !$records[0]) {
  89              $this->ioStyle->writeln('<error>No matching task found!</error>');
  90  
  91              return Status::NO_TASK;
  92          } elseif (!$records) {
  93              $this->ioStyle->writeln('<error>No tasks due!</error>');
  94  
  95              return Status::NO_TASK;
  96          }
  97  
  98          $status    = ['startTime' => microtime(true)];
  99          $taskCount = \count($records);
 100          $exit      = Status::OK;
 101  
 102          foreach ($records as $record) {
 103              $cStart   = microtime(true);
 104              $task     = $scheduler->runTask(['id' => $record->id, 'allowDisabled' => true, 'allowConcurrent' => true]);
 105              $exit     = empty($task) ? Status::NO_RUN : $task->getContent()['status'];
 106              $duration = microtime(true) - $cStart;
 107              $key      = (\array_key_exists($exit, $outTextMap)) ? $exit : 'N/A';
 108              $this->ioStyle->writeln(sprintf($outTextMap[$key], $record->id, $record->title, $duration, $exit));
 109          }
 110  
 111          $netTime = round(microtime(true) - $status['startTime'], 2);
 112          $this->ioStyle->newLine();
 113          $this->ioStyle->writeln("<info>Finished running $taskCount tasks in $netTime seconds.</info>");
 114  
 115          return $taskCount === 1 ? $exit : Status::OK;
 116      }
 117  
 118      /**
 119       * Configure the IO.
 120       *
 121       * @param   InputInterface   $input   The input to inject into the command.
 122       * @param   OutputInterface  $output  The output to inject into the command.
 123       *
 124       * @return  void
 125       *
 126       * @since  4.1.0
 127       */
 128      private function configureIO(InputInterface $input, OutputInterface $output)
 129      {
 130          $this->ioStyle = new SymfonyStyle($input, $output);
 131      }
 132  
 133      /**
 134       * Configure the command.
 135       *
 136       * @return  void
 137       *
 138       * @since   4.1.0
 139       */
 140      protected function configure(): void
 141      {
 142          $this->addOption('id', 'i', InputOption::VALUE_REQUIRED, 'The id of the task to run.');
 143          $this->addOption('all', '', InputOption::VALUE_NONE, 'Run all due tasks. Note that this is overridden if --id is used.');
 144  
 145          $help = "<info>%command.name%</info> run scheduled tasks.
 146          \nUsage: <info>php %command.full_name% [flags]</info>";
 147  
 148          $this->setDescription('Run one or more scheduled tasks');
 149          $this->setHelp($help);
 150      }
 151  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer