1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Command; |
4
|
|
|
|
5
|
|
|
use Cron\CronExpression; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Task\Scheduler\TaskSchedulerInterface; |
10
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
11
|
|
|
use Task\TaskBundle\Builder\TaskBuilder; |
12
|
|
|
use Task\TaskBundle\Entity\TaskRepository; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TODO add description here |
16
|
|
|
*/ |
17
|
|
|
class ScheduleSystemTasksCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $systemTasks; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $storage; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TaskSchedulerInterface |
31
|
|
|
*/ |
32
|
|
|
private $scheduler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TaskRepository |
36
|
|
|
*/ |
37
|
|
|
private $taskRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TaskExecutionRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $taskExecutionRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
* @param array $systemTasks |
47
|
|
|
* @param string $storage |
48
|
|
|
* @param TaskSchedulerInterface $scheduler |
49
|
|
|
* @param TaskRepository $taskRepository |
50
|
|
|
* @param TaskExecutionRepositoryInterface $taskExecutionRepository |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
$name, |
54
|
|
|
array $systemTasks, |
55
|
|
|
$storage, |
56
|
|
|
TaskSchedulerInterface $scheduler, |
57
|
|
|
TaskRepository $taskRepository, |
58
|
|
|
TaskExecutionRepositoryInterface $taskExecutionRepository |
59
|
|
|
) { |
60
|
|
|
parent::__construct($name); |
61
|
|
|
|
62
|
|
|
$this->systemTasks = $systemTasks; |
63
|
|
|
$this->storage = $storage; |
64
|
|
|
$this->scheduler = $scheduler; |
65
|
|
|
$this->taskRepository = $taskRepository; |
66
|
|
|
$this->taskExecutionRepository = $taskExecutionRepository; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function configure() |
73
|
|
|
{ |
74
|
|
|
$this->setDescription('Schedule task')->setHelp( |
75
|
|
|
<<<'EOT' |
76
|
|
|
The <info>%command.name%</info> command schedules configured system tasks. |
77
|
|
|
|
78
|
|
|
$ %command.full_name% |
79
|
|
|
|
80
|
|
|
You can configure them by extending the <info>task.system_task</info> array in your config file. |
81
|
|
|
EOT |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
89
|
|
|
{ |
90
|
|
|
foreach ($this->systemTasks as $systemKey => $systemTask) { |
91
|
|
|
if (!$systemTask['enabled']) { |
92
|
|
|
if ($task = $this->taskRepository->findBySystemKey($systemKey)) { |
93
|
|
|
$task->setInterval($task->getInterval(), $task->getFirstExecution(), new \DateTime()); |
94
|
|
|
|
95
|
|
|
if ($execution = $this->taskExecutionRepository->findPending($task)) { |
96
|
|
|
$this->taskExecutionRepository->remove($execution); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($task = $this->taskRepository->findBySystemKey($systemKey)) { |
104
|
|
|
if ($task->getHandlerClass() !== $systemTask['handler_class'] |
105
|
|
|
|| $task->getWorkload() !== $systemTask['workload'] |
106
|
|
|
) { |
107
|
|
|
throw new \InvalidArgumentException( |
108
|
|
|
sprintf('No update of handle-class or workload is supported for system-task "%s".', $systemKey) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($task->getInterval() !== $systemTask['cron_expression']) { |
113
|
|
|
$task->setInterval(CronExpression::factory($systemTask['cron_expression'])); |
114
|
|
|
|
115
|
|
|
if ($execution = $this->taskExecutionRepository->findPending($task)) { |
116
|
|
|
$this->taskExecutionRepository->remove($execution); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->scheduler->scheduleTasks(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @var TaskBuilder $builder */ |
126
|
|
|
$builder = $this->scheduler->createTask($systemTask['handler_class'], $systemTask['workload']); |
127
|
|
|
$builder->setSystemKey($systemKey); |
128
|
|
|
if ($systemTask['cron_expression']) { |
129
|
|
|
$builder->cron($systemTask['cron_expression']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$builder->schedule(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function isEnabled() |
140
|
|
|
{ |
141
|
|
|
return $this->storage === 'doctrine'; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|