-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
MaintenanceCommand.php
161 lines (136 loc) · 5.29 KB
/
MaintenanceCommand.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Bundle\CoreBundle\Command;
use Pimcore\Console\AbstractCommand;
use Pimcore\Event\System\MaintenanceEvent;
use Pimcore\Event\SystemEvents;
use Pimcore\Maintenance\CallableTask;
use Pimcore\Maintenance\ExecutorInterface;
use Pimcore\Model\Schedule;
use Pimcore\Model\Schedule\Maintenance\Job;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class MaintenanceCommand extends AbstractCommand
{
/**
* @var ExecutorInterface
*/
private $maintenanceExecutor;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param ExecutorInterface $maintenanceExecutor
* @param LoggerInterface $logger
*/
public function __construct(ExecutorInterface $maintenanceExecutor, LoggerInterface $logger)
{
$this->maintenanceExecutor = $maintenanceExecutor;
$this->logger = $logger;
parent::__construct();
}
protected function configure()
{
$description = 'Asynchronous maintenance jobs of pimcore (needs to be set up as cron job)';
$help = $description . '. Valid jobs are: ' . "\n\n";
$help .= ' <comment>*</comment> any bundle class name handling maintenance (e.g. <comment>PimcoreEcommerceFrameworkBundle</comment>)' . "\n";
foreach ($this->maintenanceExecutor->getTaskNames() as $taskName) {
$help .= ' <comment>*</comment> ' . $taskName . "\n";
}
$this
->setName('pimcore:maintenance')
->setAliases(['maintenance'])
->setDescription($description)
->setHelp($help)
->addOption(
'job',
'j',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Call just a specific job(s) (see <comment>--help</comment> for a list of valid jobs)'
)
->addOption(
'excludedJobs',
'J',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Exclude specific job(s) (see <comment>--help</comment> for a list of valid jobs)'
)
->addOption(
'force',
'f',
InputOption::VALUE_NONE,
'Run the jobs, regardless if they\'re locked or not'
)
;
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$validJobs = $this->getArrayOptionValue($input, 'job');
$excludedJobs = $this->getArrayOptionValue($input, 'excludedJobs');
$manager = $this->getContainer()->get(Schedule\Manager\Procedural::class);
$manager->setValidJobs($validJobs);
$manager->setExcludedJobs($excludedJobs);
$manager->setForce((bool) $input->getOption('force'));
$event = new MaintenanceEvent($manager);
\Pimcore::getEventDispatcher()->dispatch(SystemEvents::MAINTENANCE, $event);
foreach ($manager->getJobs() as $job) {
@trigger_error(
sprintf('Job with ID %s is registered using the deprecated %s Event, please use a service tag instead', $job->getId(), SystemEvents::MAINTENANCE),
E_USER_DEPRECATED
);
$trackerReflector = new \ReflectionClass(Job::class);
$callableProperty = $trackerReflector->getProperty('callable');
$callableProperty->setAccessible(true);
$argumentsProperty = $trackerReflector->getProperty('arguments');
$argumentsProperty->setAccessible(true);
$callable = $callableProperty->getValue($job);
$arguments = $argumentsProperty->getValue($job);
if (is_callable($callable)) {
$this->maintenanceExecutor->registerTask($job->getId(), CallableTask::fromCallable($callable, $arguments ?? []));
}
}
$this->maintenanceExecutor->executeMaintenance($validJobs, $excludedJobs, (bool) $input->getOption('force'));
$this->logger->info('All maintenance-jobs finished!');
}
/**
* Get an array option value, but still support the value being comma-separated for backwards compatibility
*
* @param InputInterface $input
* @param string $name
*
* @return array
*/
private function getArrayOptionValue(InputInterface $input, string $name): array
{
$value = $input->getOption($name);
$result = [];
if (!empty($value)) {
foreach ($value as $val) {
foreach (explode(',', $val) as $part) {
$part = trim($part);
if (!empty($part)) {
$result[] = $part;
}
}
}
}
$result = array_unique($result);
return $result;
}
}