1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\Scheduler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use Task\Builder\TaskBuilderFactoryInterface; |
16
|
|
|
use Task\Event\Events; |
17
|
|
|
use Task\Event\TaskEvent; |
18
|
|
|
use Task\Event\TaskExecutionEvent; |
19
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
20
|
|
|
use Task\Storage\TaskRepositoryInterface; |
21
|
|
|
use Task\TaskInterface; |
22
|
|
|
use Task\TaskStatus; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Scheduler creates and manages tasks. |
26
|
|
|
*/ |
27
|
|
|
class TaskScheduler implements TaskSchedulerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var TaskBuilderFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $factory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TaskRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $taskRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TaskExecutionRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $taskExecutionRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EventDispatcherInterface |
46
|
|
|
*/ |
47
|
|
|
private $eventDispatcher; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param TaskBuilderFactoryInterface $factory |
51
|
|
|
* @param TaskRepositoryInterface $taskRepository |
52
|
|
|
* @param TaskExecutionRepositoryInterface $taskExecutionRepository |
53
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
54
|
|
|
*/ |
55
|
3 |
|
public function __construct( |
56
|
|
|
TaskBuilderFactoryInterface $factory, |
57
|
|
|
TaskRepositoryInterface $taskRepository, |
58
|
|
|
TaskExecutionRepositoryInterface $taskExecutionRepository, |
59
|
|
|
EventDispatcherInterface $eventDispatcher |
60
|
|
|
) { |
61
|
3 |
|
$this->factory = $factory; |
62
|
3 |
|
$this->taskRepository = $taskRepository; |
63
|
3 |
|
$this->taskExecutionRepository = $taskExecutionRepository; |
64
|
3 |
|
$this->eventDispatcher = $eventDispatcher; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function createTask($handlerClass, $workload = null) |
71
|
|
|
{ |
72
|
1 |
|
return $this->factory->createTaskBuilder($this->taskRepository->create($handlerClass, $workload)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function addTask(TaskInterface $task) |
79
|
|
|
{ |
80
|
1 |
|
$this->eventDispatcher->dispatch(Events::TASK_CREATE, new TaskEvent($task)); |
81
|
|
|
|
82
|
1 |
|
$this->taskRepository->persist($task); |
83
|
1 |
|
$this->taskRepository->flush(); |
84
|
|
|
|
85
|
1 |
|
$this->scheduleTask($task); |
86
|
1 |
|
$this->taskExecutionRepository->flush(); |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function scheduleTasks() |
95
|
|
|
{ |
96
|
1 |
|
$tasks = $this->taskRepository->findEndBeforeNow(); |
97
|
1 |
|
foreach ($tasks as $task) { |
98
|
1 |
|
$this->scheduleTask($task); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
$this->taskExecutionRepository->flush(); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Schedule execution for given task. |
106
|
|
|
* |
107
|
|
|
* @param TaskInterface $task |
108
|
|
|
*/ |
109
|
2 |
|
protected function scheduleTask(TaskInterface $task) |
110
|
|
|
{ |
111
|
2 |
|
$scheduleTime = $task->getInterval() ? $task->getInterval()->getNextRunDate() : $task->getFirstExecution(); |
112
|
|
|
|
113
|
2 |
|
if (null === $this->taskExecutionRepository->findByStartTime($task, $scheduleTime)) { |
114
|
2 |
|
$execution = $this->taskExecutionRepository->create($task, $scheduleTime); |
115
|
2 |
|
$execution->setStatus(TaskStatus::PLANNED); |
116
|
|
|
|
117
|
2 |
|
$this->eventDispatcher->dispatch( |
118
|
2 |
|
Events::TASK_EXECUTION_CREATE, |
119
|
2 |
|
new TaskExecutionEvent($task, $execution) |
120
|
2 |
|
); |
121
|
|
|
|
122
|
2 |
|
$this->taskExecutionRepository->persist($execution); |
123
|
2 |
|
} |
124
|
2 |
|
} |
125
|
|
|
} |
126
|
|
|
|