|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basebuilder\Scheduling\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Basebuilder\Scheduling\Event\Process; |
|
6
|
|
|
use Basebuilder\Scheduling\Schedule; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This command runs all scheduled tasks that are due for execution, replacing the need for multiple CRON jobs |
|
13
|
|
|
*/ |
|
14
|
|
|
class Run extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Schedule |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $schedule; |
|
20
|
|
|
|
|
21
|
|
|
protected $minimumVerbosity = OutputInterface::VERBOSITY_VERBOSE; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Schedule $schedule |
|
25
|
|
|
* @param string|null $name |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Schedule $schedule, $name = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->schedule = $schedule; |
|
30
|
|
|
parent::__construct($name); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setName('scheduler:run'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
45
|
|
|
{ |
|
46
|
|
|
$schedule = $this->schedule; |
|
47
|
|
|
$events = $schedule->dueEvents(); |
|
48
|
|
|
|
|
49
|
|
|
if ($output->getVerbosity() >= $this->minimumVerbosity) { |
|
50
|
|
|
$output->writeln( |
|
51
|
|
|
'======================================================' . PHP_EOL . |
|
52
|
|
|
'# Running schedule for <info>' . $schedule->getName() . '</info>' . PHP_EOL . |
|
53
|
|
|
'======================================================' |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
foreach ($events as $event) { |
|
58
|
|
|
if ($output->getVerbosity() >= $this->minimumVerbosity) { |
|
59
|
|
|
$output->writeln('Running event <info>"' . (string)$event . '"</info>'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$result = $event->run(); |
|
63
|
|
|
if ($event instanceof Process) { |
|
64
|
|
|
$this->handleProcessOutput($result, $output); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function handleProcessOutput(\Symfony\Component\Process\Process $process, OutputInterface $output) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($process->isTerminated() && $output->getVerbosity() >= $this->minimumVerbosity) { |
|
72
|
|
|
$tag = $process->isSuccessful() ? 'info' : 'error'; |
|
73
|
|
|
$output->writeln('Exit code: <' . $tag . '>' . $process->getExitCode() . '</' . $tag . '>'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|