1
|
|
|
<?php namespace Comodojo\Extender; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Extender\Workers\ScheduleWorker; |
4
|
|
|
use \Comodojo\Extender\Workers\QueueWorker; |
5
|
|
|
use \Comodojo\Extender\Task\Table as TasksTable; |
6
|
|
|
use \Comodojo\Extender\Task\Request; |
7
|
|
|
use \Comodojo\Extender\Task\TaskParameters; |
8
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
9
|
|
|
use \Comodojo\Extender\Traits\TasksTableTrait; |
10
|
|
|
use \Comodojo\Extender\Traits\EntityManagerTrait; |
11
|
|
|
use \Comodojo\Extender\Components\Database; |
12
|
|
|
use \Comodojo\Extender\Queue\Manager as QueueManager; |
13
|
|
|
use \Comodojo\Extender\Schedule\Manager as ScheduleManager; |
14
|
|
|
use \Comodojo\Extender\Orm\Entities\Schedule; |
15
|
|
|
use \Comodojo\Daemon\Daemon as AbstractDaemon; |
16
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
17
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
18
|
|
|
use \Comodojo\Dispatcher\Traits\CacheTrait; |
19
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
20
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
21
|
|
|
use \Comodojo\Foundation\Logging\Manager as LogManager; |
22
|
|
|
use \Comodojo\Foundation\Utils\ArrayOps; |
23
|
|
|
use \Comodojo\SimpleCache\Manager as SimpleCacheManager; |
24
|
|
|
use \Doctrine\ORM\EntityManager; |
25
|
|
|
use \Psr\Log\LoggerInterface; |
26
|
|
|
|
27
|
|
|
class ExtenderDaemon extends AbstractDaemon { |
28
|
|
|
|
29
|
|
|
use ConfigurationTrait; |
30
|
|
|
use EventsTrait; |
31
|
|
|
use LoggerTrait; |
32
|
|
|
use CacheTrait; |
33
|
|
|
use TasksTableTrait; |
34
|
|
|
use EntityManagerTrait; |
35
|
|
|
|
36
|
|
|
protected static $default_properties = array( |
37
|
|
|
'pidfile' => '', |
38
|
|
|
'socketfile' => '', |
39
|
|
|
'socketbuffer' => 8192, |
40
|
|
|
'sockettimeout' => 15, |
41
|
|
|
'niceness' => 0, |
42
|
|
|
'arguments' => '\\Comodojo\\Daemon\\Console\\DaemonArguments', |
43
|
|
|
'description' => 'Extender Daemon' |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
array $configuration, |
48
|
|
|
array $tasks, |
49
|
|
|
EventsManager $events = null, |
50
|
|
|
SimpleCacheManager $cache = null, |
51
|
|
|
LoggerInterface $logger = null |
52
|
|
|
) { |
53
|
|
|
|
54
|
|
|
$this->configuration = new Configuration(self::$default_properties); |
55
|
|
|
$this->configuration->merge($configuration); |
56
|
|
|
|
57
|
|
|
$run_path = $this->getRunPath(); |
58
|
|
|
|
59
|
|
|
if ( empty($this->configuration->get('socketfile')) ) { |
60
|
|
|
$this->configuration->set('socketfile', "unix://$run_path/extender.sock"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ( empty($this->configuration->get('pidfile')) ) { |
64
|
|
|
$this->configuration->set('pidfile', "$run_path/extender.pid"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$logger = is_null($logger) ? LogManager::createFromConfiguration($this->configuration)->getLogger() : $logger; |
68
|
|
|
$events = is_null($events) ? EventsManager::create($logger) : $events; |
69
|
|
|
|
70
|
|
|
parent::__construct(ArrayOps::replaceStrict(self::$default_properties, $this->configuration->get()), $logger, $events); |
71
|
|
|
|
72
|
|
|
$table = new TasksTable($this->configuration, $this->getLogger(), $this->getEvents()); |
73
|
|
|
$table->addBulk($tasks); |
74
|
|
|
$this->setTasksTable($table); |
75
|
|
|
|
76
|
|
|
$this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache); |
77
|
|
|
|
78
|
|
|
$this->setEntityManager(Database::init($this->configuration)->getEntityManager()); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setup() { |
83
|
|
|
|
84
|
|
|
$this->installWorkers(); |
85
|
|
|
|
86
|
|
|
$this->pushQueueCommands(); |
87
|
|
|
$this->pushScheduleCommands(); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function installWorkers() { |
92
|
|
|
|
93
|
|
|
// add workers |
94
|
|
|
$manager = $this->getWorkers(); |
95
|
|
|
|
96
|
|
|
$schedule_worker = new ScheduleWorker("scheduler"); |
97
|
|
|
$schedule_worker |
98
|
|
|
->setConfiguration($this->getConfiguration()) |
99
|
|
|
->setLogger($this->getLogger()) |
100
|
|
|
->setEvents($this->getEvents()) |
101
|
|
|
->setTasksTable($this->getTasksTable()) |
102
|
|
|
->setEntityManager($this->getEntityManager()); |
103
|
|
|
|
104
|
|
|
$queue_worker = new QueueWorker("queue"); |
105
|
|
|
$queue_worker |
106
|
|
|
->setConfiguration($this->getConfiguration()) |
107
|
|
|
->setLogger($this->getLogger()) |
108
|
|
|
->setEvents($this->getEvents()) |
109
|
|
|
->setTasksTable($this->getTasksTable()) |
110
|
|
|
->setEntityManager($this->getEntityManager()); |
111
|
|
|
|
112
|
|
|
$manager |
113
|
|
|
->install($schedule_worker, 1, true) |
114
|
|
|
->install($queue_worker, 1, true); |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function pushQueueCommands() { |
119
|
|
|
|
120
|
|
|
$this->getSocket()->getCommands() |
121
|
|
View Code Duplication |
->add('queue:add', function(Request $request, $daemon) { |
|
|
|
|
122
|
|
|
$manager = new QueueManager( |
123
|
|
|
$this->getConfiguration(), |
124
|
|
|
$this->getLogger(), |
125
|
|
|
$this->getEvents(), |
126
|
|
|
$this->getEntityManager() |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return $manager->add($name, $request); |
|
|
|
|
130
|
|
|
}) |
131
|
|
View Code Duplication |
->add('queue:addBulk', function(array $requests, $daemon) { |
|
|
|
|
132
|
|
|
|
133
|
|
|
$manager = new QueueManager( |
134
|
|
|
$this->getConfiguration(), |
135
|
|
|
$this->getLogger(), |
136
|
|
|
$this->getEvents(), |
137
|
|
|
$this->getEntityManager() |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return $manager->addBulk($requests); |
141
|
|
|
|
142
|
|
|
}); |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function pushScheduleCommands() { |
147
|
|
|
|
148
|
|
|
$this->getSocket()->getCommands() |
149
|
|
|
->add('scheduler:refresh', function($data, $daemon) { |
|
|
|
|
150
|
|
|
|
151
|
|
|
return $this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
152
|
|
|
|
153
|
|
|
}) |
154
|
|
View Code Duplication |
->add('scheduler:add', function(Schedule $data, $daemon) { |
|
|
|
|
155
|
|
|
|
156
|
|
|
$manager = new ScheduleManager( |
157
|
|
|
$this->getConfiguration(), |
158
|
|
|
$this->getLogger(), |
159
|
|
|
$this->getEvents(), |
160
|
|
|
$this->getEntityManager() |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$id = $manager->add($data); |
164
|
|
|
|
165
|
|
|
$this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
166
|
|
|
|
167
|
|
|
return $id; |
168
|
|
|
|
169
|
|
|
}) |
170
|
|
View Code Duplication |
->add('scheduler:get', function($id, $daemon) { |
|
|
|
|
171
|
|
|
|
172
|
|
|
$manager = new ScheduleManager( |
173
|
|
|
$this->getConfiguration(), |
174
|
|
|
$this->getLogger(), |
175
|
|
|
$this->getEvents(), |
176
|
|
|
$this->getEntityManager() |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
return $manager->get($id); |
180
|
|
|
|
181
|
|
|
}) |
182
|
|
View Code Duplication |
->add('scheduler:getByName', function($name, $daemon) { |
|
|
|
|
183
|
|
|
|
184
|
|
|
$manager = new ScheduleManager( |
185
|
|
|
$this->getConfiguration(), |
186
|
|
|
$this->getLogger(), |
187
|
|
|
$this->getEvents(), |
188
|
|
|
$this->getEntityManager() |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $manager->getByName($name); |
192
|
|
|
|
193
|
|
|
}) |
194
|
|
View Code Duplication |
->add('scheduler:edit', function(Schedule $data, $daemon) { |
|
|
|
|
195
|
|
|
|
196
|
|
|
$manager = new ScheduleManager( |
197
|
|
|
$this->getConfiguration(), |
198
|
|
|
$this->getLogger(), |
199
|
|
|
$this->getEvents(), |
200
|
|
|
$this->getEntityManager() |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$edit = $manager->edit($data); |
204
|
|
|
|
205
|
|
|
$this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
206
|
|
|
|
207
|
|
|
return $edit; |
208
|
|
|
|
209
|
|
|
}) |
210
|
|
View Code Duplication |
->add('scheduler:enable', function($name, $daemon) { |
|
|
|
|
211
|
|
|
|
212
|
|
|
$manager = new ScheduleManager( |
213
|
|
|
$this->getConfiguration(), |
214
|
|
|
$this->getLogger(), |
215
|
|
|
$this->getEvents(), |
216
|
|
|
$this->getEntityManager() |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
$edit = $manager->enable($name); |
220
|
|
|
|
221
|
|
|
$this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
222
|
|
|
|
223
|
|
|
return $edit; |
224
|
|
|
|
225
|
|
|
}) |
226
|
|
View Code Duplication |
->add('scheduler:edit', function($name, $daemon) { |
|
|
|
|
227
|
|
|
|
228
|
|
|
$manager = new ScheduleManager( |
229
|
|
|
$this->getConfiguration(), |
230
|
|
|
$this->getLogger(), |
231
|
|
|
$this->getEvents(), |
232
|
|
|
$this->getEntityManager() |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$edit = $manager->disable($name); |
236
|
|
|
|
237
|
|
|
$this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
238
|
|
|
|
239
|
|
|
return $edit; |
240
|
|
|
|
241
|
|
|
}); |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
private function getRunPath() { |
246
|
|
|
return $this->configuration->get('base-path')."/".$this->configuration->get('run-path'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.