1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use Zenstruck\ScheduleBundle\EventListener\ScheduleConsoleOutputSubscriber; |
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleRunner; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Kevin Bond <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class ScheduleRunCommand extends Command |
18
|
|
|
{ |
19
|
|
|
protected static $defaultName = 'schedule:run'; |
20
|
|
|
|
21
|
|
|
/** @var ScheduleRunner */ |
22
|
|
|
private $scheduleRunner; |
23
|
|
|
|
24
|
12 |
|
/** @var EventDispatcherInterface */ |
25
|
|
|
private $dispatcher; |
26
|
12 |
|
|
27
|
12 |
|
public function __construct(ScheduleRunner $scheduleRunner, EventDispatcherInterface $dispatcher) |
28
|
|
|
{ |
29
|
12 |
|
$this->scheduleRunner = $scheduleRunner; |
30
|
12 |
|
$this->dispatcher = $dispatcher; |
31
|
|
|
|
32
|
12 |
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
protected function configure(): void |
36
|
12 |
|
{ |
37
|
12 |
|
$this |
38
|
12 |
|
->setDescription('Runs scheduled tasks that are due') |
39
|
|
|
->addArgument('id', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, '(optional) Task ID\'s to "force" run') |
40
|
|
|
->setHelp(<<<EOF |
41
|
|
|
If no arguments are passed, all the tasks currently due are run. Pass one or |
42
|
|
|
more Task ID's to "force" run these even if they are not due (only these are |
43
|
|
|
run). |
44
|
|
|
|
45
|
|
|
Exit code 0: no tasks ran, schedule skipped, or all tasks run were successful. |
46
|
|
|
Exit code 1: one or more tasks failed. |
47
|
|
|
|
48
|
|
|
Add this command as a Cron job to your production server(s) running every minute: |
49
|
|
|
|
50
|
|
|
* * * * * cd /path-to-your-project && php bin/console schedule:run >> /dev/null 2>&1 |
51
|
12 |
|
EOF |
52
|
|
|
) |
53
|
12 |
|
; |
54
|
|
|
} |
55
|
12 |
|
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
57
|
12 |
|
{ |
58
|
|
|
$io = new SymfonyStyle($input, $output); |
59
|
12 |
|
|
60
|
|
|
$this->dispatcher->addSubscriber(new ScheduleConsoleOutputSubscriber($io)); |
61
|
|
|
|
62
|
|
|
return ($this->scheduleRunner)(...$input->getArgument('id'))->isSuccessful() ? 0 : 1; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|