1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\TaskBundle\Synchronizer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Glooby\TaskBundle\Entity\Schedule; |
7
|
|
|
use Glooby\TaskBundle\Annotation\Schedule as Def; |
8
|
|
|
use Glooby\TaskBundle\Entity\ScheduleRepository; |
9
|
|
|
use Glooby\TaskBundle\Schedule\ScheduleRegistry; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Emil Kilhage |
13
|
|
|
*/ |
14
|
|
|
class ScheduleSynchronizer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ManagerRegistry |
18
|
|
|
*/ |
19
|
|
|
protected $doctrine; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param ManagerRegistry $doctrine |
23
|
|
|
*/ |
24
|
|
|
public function setDoctrine($doctrine) |
25
|
|
|
{ |
26
|
|
|
$this->doctrine = $doctrine; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ScheduleRegistry |
31
|
|
|
*/ |
32
|
|
|
private $scheduleRegistry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $force = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param boolean $force |
41
|
|
|
*/ |
42
|
|
|
public function setForce($force) |
43
|
|
|
{ |
44
|
|
|
$this->force = $force; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ScheduleRegistry $scheduleRegistry |
49
|
|
|
*/ |
50
|
|
|
public function setScheduleRegistry(ScheduleRegistry $scheduleRegistry) |
51
|
|
|
{ |
52
|
|
|
$this->scheduleRegistry = $scheduleRegistry; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function sync() |
59
|
|
|
{ |
60
|
|
|
/** @var ScheduleRepository $repo */ |
61
|
|
|
$repo = $this->doctrine->getManager() |
62
|
|
|
->getRepository('GloobyTaskBundle:Schedule'); |
63
|
|
|
|
64
|
|
|
$schedules = $this->scheduleRegistry->getSchedules(); |
65
|
|
|
|
66
|
|
|
foreach ($schedules as $id => $def) { |
67
|
|
|
$this->syncSchedule($id, $def); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($repo->findNotInNames(array_keys($schedules)) as $schedule) { |
71
|
|
|
$this->doctrine->getManager()->remove($schedule); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->doctrine->getManager()->flush(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Schedule $schedule |
79
|
|
|
* @param string $id |
80
|
|
|
* @param Def $def |
81
|
|
|
*/ |
82
|
|
|
private function update(Schedule $schedule, $id, Def $def) |
83
|
|
|
{ |
84
|
|
|
$schedule->setName($id); |
85
|
|
|
$schedule->setActive($def->active); |
86
|
|
|
$schedule->setRunEvery($def->runEvery); |
87
|
|
|
$schedule->setTimeout($def->timeout); |
88
|
|
|
$schedule->setParams($def->params); |
89
|
|
|
$schedule->setVersion($def->version); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $id |
94
|
|
|
* @param Def $def |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private function syncSchedule($id, Def $def) |
98
|
|
|
{ |
99
|
|
|
/** @var ScheduleRepository $repo */ |
100
|
|
|
$repo = $this->doctrine->getManager() |
101
|
|
|
->getRepository('GloobyTaskBundle:Schedule'); |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
/** @var Schedule $schedule */ |
105
|
|
|
$schedule = $repo->findByName($id); |
106
|
|
|
|
107
|
|
|
if ($this->force || $schedule->getVersion() !== $def->version) { |
108
|
|
|
$this->update($schedule, $id, $def); |
109
|
|
|
} |
110
|
|
|
} catch (\Exception $e) { |
111
|
|
|
$this->create($id, $def); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $id |
117
|
|
|
* @param Def $def |
118
|
|
|
*/ |
119
|
|
|
private function create($id, Def $def) |
120
|
|
|
{ |
121
|
|
|
$schedule = new Schedule(); |
122
|
|
|
$this->update($schedule, $id, $def); |
123
|
|
|
$this->doctrine->getManager()->persist($schedule); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|