/
JobCommand.php
138 lines (126 loc) · 5.03 KB
/
JobCommand.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
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Console\ConsoleAwareInterface;
use Job;
use JobSet;
use RuntimeException;
use Concrete\Core\Console\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class JobCommand extends Command
{
protected function configure()
{
$errExitCode = static::RETURN_CODE_ON_FAILURE;
$this
->setName('c5:job')
->setDescription(t('Run a concrete5 job'))
->addEnvOption()
->addOption('set', null, InputOption::VALUE_NONE, t('Find jobs by set instead of job handle'))
->addOption('list', null, InputOption::VALUE_NONE, t('List available jobs'))
->addArgument(
'jobs',
InputArgument::IS_ARRAY,
t('Jobs to run (separate multiple jobs with a space)')
)
->setHelp(<<<EOT
Returns codes:
0 operation completed successfully
$errExitCode errors occurred
More info at http://documentation.concrete5.org/developers/appendix/cli-commands#c5-job
EOT
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$rc = 0;
$options = $input->getOptions();
$formatter = $this->getHelper('formatter');
if ($options['list']) {
$output->writeln(t('Available Jobs'));
$table = new Table($output);
$table->setHeaders([t('Job Handle'), t('Job Name')]);
foreach (Job::getList() as $job) {
$table->addRow([$job->getJobHandle(), $job->getJobName()]);
}
$table->render();
$output->writeln('');
$output->writeln(t('Available Job Sets'));
$table = new Table($output);
$table->setHeaders([t('Set Name'), t('Jobs')]);
foreach (JobSet::getList() as $jobSet) {
$jobsInSet = [];
foreach ($jobSet->getJobs() as $job) {
$jobsInSet[] = $job->getJobName();
}
$table->addRow([$jobSet->getJobSetName(), implode(', ', $jobsInSet)]);
}
$table->render();
} else {
$jobs = [];
$jobsArg = $input->getArgument('jobs');
if (empty($jobsArg)) {
throw new RuntimeException(t('At least one job must be provided'));
}
if ($options['set']) {
foreach ($jobsArg as $setName) {
$set = JobSet::getByName($setName);
if ($set) {
$jobs = array_merge($jobs, $set->getJobs());
} else {
$rc = 1;
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$output->writeln(
'<error>' . t('A job set with name "%s" was not found', $setName) . '</error>'
);
}
}
}
} else {
foreach ($jobsArg as $jobHandle) {
$job = Job::getByHandle($jobHandle);
if ($job) {
$jobs[] = $job;
} else {
$rc = 1;
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$output->writeln(
'<error>' . t('A job with handle "%s" was not found', $jobHandle) . '</error>'
);
}
}
}
}
if (!empty($jobs)) {
foreach ($jobs as $job) {
// Provide the console objects to objects that are aware of the console
if ($job instanceof ConsoleAwareInterface) {
$job->setConsole($this->getApplication(), $output, $input);
}
$result = $job->executeJob();
if ($result->isError()) {
$rc = 1;
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$output->writeln(
$formatter->formatSection(
$job->getJobHandle(), '<error>' . t('Job Failed') . '</error>'
)
);
}
break;
}
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$output->writeln(
$formatter->formatSection($job->getJobHandle(), $result->getResultMessage())
);
}
}
}
}
return $rc;
}
}