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