|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asylamba\Classes\Scheduler; |
|
4
|
|
|
|
|
5
|
|
|
use Asylamba\Classes\DependencyInjection\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Asylamba\Classes\Task\TaskManager; |
|
8
|
|
|
use Asylamba\Classes\Process\LoadBalancer; |
|
9
|
|
|
use Asylamba\Classes\Process\ProcessGateway; |
|
10
|
|
|
|
|
11
|
|
|
class RealTimeActionScheduler |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Container **/ |
|
14
|
|
|
protected $container; |
|
15
|
|
|
/** @var TaskManager **/ |
|
16
|
|
|
protected $taskManager; |
|
17
|
|
|
/** @var LoadBalancer **/ |
|
18
|
|
|
protected $loadBalancer; |
|
19
|
|
|
/** @var ProcessGateway **/ |
|
20
|
|
|
protected $processGateway; |
|
21
|
|
|
/** @var array **/ |
|
22
|
|
|
protected $queue = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Container $container |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Container $container) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->container = $container; |
|
30
|
|
|
$this->taskManager = $container->get('task_manager'); |
|
31
|
|
|
$this->loadBalancer = $container->get('load_balancer'); |
|
32
|
|
|
$this->processGateway = $container->get('process_gateway'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function init() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->container->get('ares.commander_manager')->scheduleMovements(); |
|
38
|
|
|
$this->container->get('athena.building_queue_manager')->scheduleActions(); |
|
39
|
|
|
$this->container->get('athena.commercial_shipping_manager')->scheduleShippings(); |
|
40
|
|
|
$this->container->get('athena.recycling_mission_manager')->scheduleMissions(); |
|
41
|
|
|
$this->container->get('athena.ship_queue_manager')->scheduleActions(); |
|
42
|
|
|
$this->container->get('promethee.technology_queue_manager')->scheduleQueues(); |
|
43
|
|
|
$factionManager = $this->container->get('demeter.color_manager'); |
|
44
|
|
|
$factionManager->scheduleSenateUpdate(); |
|
45
|
|
|
$factionManager->scheduleCampaigns(); |
|
46
|
|
|
$factionManager->scheduleElections(); |
|
47
|
|
|
$factionManager->scheduleBallot(); |
|
48
|
|
|
$this->execute(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* This method is meant to register a new action to schedule |
|
53
|
|
|
* The action is put in the queue to be executed |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $manager |
|
56
|
|
|
* @param string $method |
|
57
|
|
|
* @param array $object |
|
58
|
|
|
* @param string $date |
|
59
|
|
|
* @param array $context |
|
60
|
|
|
*/ |
|
61
|
|
|
public function schedule($manager, $method, $object, $date, $context = null) |
|
62
|
|
|
{ |
|
63
|
|
|
if (P_TYPE === 'worker') { |
|
64
|
|
|
return $this->processGateway->writeToMaster([ |
|
65
|
|
|
'command' => 'schedule', |
|
66
|
|
|
'data' => [ |
|
67
|
|
|
'manager' => $manager, |
|
68
|
|
|
'method' => $method, |
|
69
|
|
|
'object_class' => get_class($object), |
|
70
|
|
|
'object_id' => $object->id, |
|
71
|
|
|
'date' => $date, |
|
72
|
|
|
'context' => $context |
|
73
|
|
|
] |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
$this->queue[$date][get_class($object) . '-' . $object->id] = $this->taskManager->createRealTimeTask( |
|
77
|
|
|
$manager, |
|
78
|
|
|
$method, |
|
79
|
|
|
$object->id, |
|
80
|
|
|
$date, |
|
81
|
|
|
null, |
|
82
|
|
|
$context |
|
83
|
|
|
); |
|
84
|
|
|
// Sort the queue by date |
|
85
|
|
|
ksort($this->queue); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $manager |
|
90
|
|
|
* @param string $method |
|
91
|
|
|
* @param string $objectClass |
|
92
|
|
|
* @param int $objectId |
|
93
|
|
|
* @param string $date |
|
94
|
|
|
* @param array $context |
|
95
|
|
|
*/ |
|
96
|
|
|
public function scheduleFromProcess($manager, $method, $objectClass, $objectId, $date, $context = null) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->queue[$date][$objectClass . '-' . $objectId] = $this->taskManager->createRealTimeTask($manager, $method, $objectId, $date, null, $context); |
|
99
|
|
|
// Sort the queue by date |
|
100
|
|
|
ksort($this->queue); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* This method is meant to executed the scheduled data if their date is passed |
|
105
|
|
|
* In case of cyclic actions, the scheduler will check the current hour and compare it to the last executed hour |
|
106
|
|
|
*/ |
|
107
|
|
|
public function execute() |
|
108
|
|
|
{ |
|
109
|
|
|
$now = new \DateTime(); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->queue as $date => $actions) { |
|
112
|
|
|
// If the action is to be executed later, we break the loop |
|
113
|
|
|
// This logic depends on the fact that the queue is key-sorted by date |
|
114
|
|
|
if ($now < new \DateTime($date)) { |
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
foreach ($actions as $task) { |
|
118
|
|
|
$this->loadBalancer->affectTask($task); |
|
119
|
|
|
} |
|
120
|
|
|
unset($this->queue[$date]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param object $object |
|
126
|
|
|
* @param string $date |
|
127
|
|
|
* @param string $oldDate |
|
128
|
|
|
*/ |
|
129
|
|
|
public function reschedule($object, $date, $oldDate) { |
|
130
|
|
|
$this->queue[$date][get_class($object) . '-' . $object->id] = $this->queue[$oldDate][get_class($object) . '-' . $object->id]; |
|
131
|
|
|
|
|
132
|
|
|
$this->cancel($object, $oldDate); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param object $object |
|
137
|
|
|
* @param string $date |
|
138
|
|
|
*/ |
|
139
|
|
|
public function cancel($object, $date) |
|
140
|
|
|
{ |
|
141
|
|
|
if (P_TYPE === 'worker') { |
|
142
|
|
|
return $this->processGateway->writeToMaster([ |
|
143
|
|
|
'command' => 'cancel', |
|
144
|
|
|
'data' => [ |
|
145
|
|
|
'object_class' => get_class($object), |
|
146
|
|
|
'object_id' => $object->id, |
|
147
|
|
|
'date' => $date |
|
148
|
|
|
] |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
unset($this->queue[$date][get_class($object) . '-' . $object->id]); |
|
152
|
|
|
|
|
153
|
|
|
if (empty($this->queue[$date])) { |
|
154
|
|
|
unset($this->queue[$date]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $class |
|
160
|
|
|
* @param int $id |
|
161
|
|
|
* @param string $date |
|
162
|
|
|
*/ |
|
163
|
|
|
public function cancelFromProcess($class, $id, $date) |
|
164
|
|
|
{ |
|
165
|
|
|
unset($this->queue[$date][$class . '-' . $id]); |
|
166
|
|
|
|
|
167
|
|
|
if (empty($this->queue[$date])) { |
|
168
|
|
|
unset($this->queue[$date]); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getQueue() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->queue; |
|
178
|
|
|
} |
|
179
|
|
|
} |