[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/phpbb/console/command/cron/ -> run.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\console\command\cron;
  15  
  16  use phpbb\exception\runtime_exception;
  17  use Symfony\Component\Console\Input\InputInterface;
  18  use Symfony\Component\Console\Input\InputArgument;
  19  use Symfony\Component\Console\Output\OutputInterface;
  20  
  21  class run extends \phpbb\console\command\command
  22  {
  23      /** @var \phpbb\cron\manager */
  24      protected $cron_manager;
  25  
  26      /** @var \phpbb\lock\db */
  27      protected $lock_db;
  28  
  29      /**
  30      * Construct method
  31      *
  32      * @param \phpbb\user $user The user object (used to get language information)
  33      * @param \phpbb\cron\manager $cron_manager The cron manager containing
  34      *        the cron tasks to be executed.
  35      * @param \phpbb\lock\db $lock_db The lock for accessing database.
  36      */
  37  	public function __construct(\phpbb\user $user, \phpbb\cron\manager $cron_manager, \phpbb\lock\db $lock_db)
  38      {
  39          $this->cron_manager = $cron_manager;
  40          $this->lock_db = $lock_db;
  41          parent::__construct($user);
  42      }
  43  
  44      /**
  45      * Sets the command name and description
  46      *
  47      * @return null
  48      */
  49  	protected function configure()
  50      {
  51          $this
  52              ->setName('cron:run')
  53              ->setDescription($this->user->lang('CLI_DESCRIPTION_CRON_RUN'))
  54              ->setHelp($this->user->lang('CLI_HELP_CRON_RUN'))
  55              ->addArgument('name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_CRON_RUN_ARGUMENT_1'))
  56          ;
  57      }
  58  
  59      /**
  60      * Executes the command cron:run.
  61      *
  62      * Tries to acquire the cron lock, then if no argument has been given runs all ready cron tasks.
  63      * If the cron lock can not be obtained, an error message is printed
  64      *        and the exit status is set to 1.
  65      * If the verbose option is specified, each start of a task is printed.
  66      *        Otherwise there is no output.
  67      * If an argument is given to the command, only the task whose name matches the
  68      *        argument will be started. If verbose option is specified,
  69      *        an info message containing the name of the task is printed.
  70      * If no task matches the argument given, an error message is printed
  71      *        and the exit status is set to 2.
  72      *
  73      * @param InputInterface $input The input stream used to get the argument and verboe option.
  74      * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
  75      *
  76      * @return int 0 if all is ok, 1 if a lock error occurred and 2 if no task matching the argument was found.
  77      */
  78  	protected function execute(InputInterface $input, OutputInterface $output)
  79      {
  80          if ($this->lock_db->acquire())
  81          {
  82              $task_name = $input->getArgument('name');
  83              if ($task_name)
  84              {
  85                  $exit_status = $this->run_one($input, $output, $task_name);
  86              }
  87              else
  88              {
  89                  $exit_status = $this->run_all($input, $output);
  90              }
  91  
  92              $this->lock_db->release();
  93              return $exit_status;
  94          }
  95          else
  96          {
  97              throw new runtime_exception('CRON_LOCK_ERROR', array(), null, 1);
  98          }
  99      }
 100  
 101      /**
 102      * Executes all ready cron tasks.
 103      *
 104      * If verbose mode is set, an info message will be printed if there is no task to
 105      *        be run, or else for each starting task.
 106      *
 107      * @see execute
 108      * @param InputInterface $input The input stream used to get the argument and verbose option.
 109      * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
 110      * @return int 0
 111      */
 112  	protected function run_all(InputInterface $input, OutputInterface $output)
 113      {
 114          $run_tasks = $this->cron_manager->find_all_ready_tasks();
 115  
 116          if ($run_tasks)
 117          {
 118              foreach ($run_tasks as $task)
 119              {
 120                  if ($input->getOption('verbose'))
 121                  {
 122                      $output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task->get_name()) . '</info>');
 123                  }
 124  
 125                  $task->run();
 126              }
 127          }
 128          else
 129          {
 130              if ($input->getOption('verbose'))
 131              {
 132                  $output->writeln('<info>' . $this->user->lang('CRON_NO_TASK') . '</info>');
 133              }
 134          }
 135  
 136          return 0;
 137      }
 138  
 139      /**
 140      * Executes a given cron task, if it is ready.
 141      *
 142      * If there is a task whose name matches $task_name, it is run and 0 is returned.
 143      *        and if verbose mode is set, print an info message with the name of the task.
 144      * If there is no task matching $task_name, the function prints an error message
 145      *        and returns with status 2.
 146      *
 147      * @see execute
 148      *
 149      * @param InputInterface $input The input stream used to get the argument and verbose option.
 150      * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
 151      * @param string $task_name The name of the task that should be run.
 152      *
 153      * @return int 0 if all is well, 2 if no task matches $task_name.
 154      */
 155  	protected function run_one(InputInterface $input, OutputInterface $output, $task_name)
 156      {
 157          $task = $this->cron_manager->find_task($task_name);
 158          if ($task)
 159          {
 160              if ($input->getOption('verbose'))
 161              {
 162                  $output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task_name) . '</info>');
 163              }
 164  
 165              $task->run();
 166              return 0;
 167          }
 168          else
 169          {
 170              throw new runtime_exception('CRON_NO_SUCH_TASK', array( $task_name), null, 2);
 171          }
 172      }
 173  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1