aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/command/cron/run.php
blob: 701f0f02fbd30ab92238914ac246aa5f61c49d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited 
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\console\command\cron;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class run extends \phpbb\console\command\command
{
	/** @var \phpbb\cron\manager */
	protected $cron_manager;

	/** @var \phpbb\lock\db */
	protected $lock_db;

	/** @var \phpbb\user */
	protected $user;

	/**
	* Construct method
	*
	* @param \phpbb\cron\manager $cron_manager The cron manager containing
	*							the cron tasks to be executed.
	* @param \phpbb\lock\db $lock_db The lock for accessing database.
	* @param \phobb\user $user The user object (used to get language information)
	*/
	public function __construct(\phpbb\cron\manager $cron_manager, \phpbb\lock\db $lock_db, \phpbb\user $user)
	{
		$this->cron_manager = $cron_manager;
		$this->lock_db = $lock_db;
		$this->user = $user;
		parent::__construct();
	}

	/**
	* Sets the command name and description
	*
	* @return null
	*/
	protected function configure()
	{
		$this
			->setName('cron:run')
			->setDescription($this->user->lang('CLI_DESCR_CRON_RUN'))
			->addArgument('name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCR_CRON_ARG_RUN_1'));
		;
	}

	/**
	* Executes the function.
	*
	* Tries to acquire the cron lock, then if no argument has been given runs all ready cron tasks.
	* If the cron lock can not be obtained, an error message is printed
	*		and the exit status is set to 1.
	* If the verbose option is specified, each start of a task is printed.
	*		Otherwise there is no output.
	* If an argument is given to the command, only the task whose name matches the 
	*		argument will be started. If none exists, an error message is
	*		printed and theexit status is set to -1. Verbose option does nothing in 
	*		this case.
	*
	* @param InputInterface $input The input stream, unused here
	* @param OutputInterface $output The output stream, used for printig verbose-mode and error information.
	*
	* @return int 0 if all is ok, 1 if a lock error occured and -1 if no task matching the argument was found
	*/
	protected function execute(InputInterface $input, OutputInterface $output)
	{
		if ($this->lock_db->acquire())
		{
			if ($task_name = $input->getArgument('name'))
			{
				if ($task = $this->cron_manager->find_task($task_name))
				{
					$task->run();
					return 0;
				}
				else
				{
					$output->writeln('<error>' . $this->user->lang('CRON_NO_TASK') . '</error>');
					return -1;
				}
			}
			else
			{
				$run_tasks = $this->cron_manager->find_all_ready_tasks();

				foreach ($run_tasks as $task)
				{
					if ($input->getOption('verbose'))
					{
						$output->writeln($this->user->lang('RUNNING_TASK', $task->get_name()));
					}

					$task->run();
				}
				$this->lock_db->release();

				return 0;
			}
		}
		else
		{
			$output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>');
			return 1;
		}
	}
}