|
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\Daemon\Daemon as AbstractDaemon; |
|
14
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
|
15
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
|
16
|
|
|
use \Comodojo\Dispatcher\Traits\CacheTrait; |
|
17
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
|
18
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
|
19
|
|
|
use \Comodojo\Foundation\Logging\Manager as LogManager; |
|
20
|
|
|
use \Comodojo\Foundation\Utils\ArrayOps; |
|
21
|
|
|
use \Comodojo\SimpleCache\Manager as SimpleCacheManager; |
|
22
|
|
|
use \Doctrine\ORM\EntityManager; |
|
23
|
|
|
use \Psr\Log\LoggerInterface; |
|
24
|
|
|
|
|
25
|
|
|
class ExtenderDaemon extends AbstractDaemon { |
|
26
|
|
|
|
|
27
|
|
|
use ConfigurationTrait; |
|
28
|
|
|
use EventsTrait; |
|
29
|
|
|
use LoggerTrait; |
|
30
|
|
|
use CacheTrait; |
|
31
|
|
|
use TasksTableTrait; |
|
32
|
|
|
use EntityManagerTrait; |
|
33
|
|
|
|
|
34
|
|
|
protected static $default_properties = array( |
|
35
|
|
|
'pidfile' => 'extender.pid', |
|
36
|
|
|
'socketfile' => 'unix://extender.sock', |
|
37
|
|
|
'socketbuffer' => 8192, |
|
38
|
|
|
'sockettimeout' => 15, |
|
39
|
|
|
'niceness' => 0, |
|
40
|
|
|
'arguments' => '\\Comodojo\\Daemon\\Console\\DaemonArguments', |
|
41
|
|
|
'description' => 'Extender Daemon' |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
array $configuration, |
|
46
|
|
|
array $tasks, |
|
47
|
|
|
EventsManager $events = null, |
|
48
|
|
|
SimpleCacheManager $cache = null, |
|
49
|
|
|
LoggerInterface $logger = null |
|
50
|
|
|
) { |
|
51
|
|
|
|
|
52
|
|
|
$this->configuration = new Configuration(self::$default_properties); |
|
53
|
|
|
$this->configuration->merge($configuration); |
|
54
|
|
|
|
|
55
|
|
|
parent::__construct(ArrayOps::replaceStrict(self::$default_properties, $this->configuration->get()), $logger, $events); |
|
56
|
|
|
|
|
57
|
|
|
$table = new TasksTable($this->configuration, $this->getLogger(), $this->getEvents()); |
|
58
|
|
|
$table->addBulk($tasks); |
|
59
|
|
|
$this->setTasksTable($table); |
|
60
|
|
|
|
|
61
|
|
|
$this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache); |
|
62
|
|
|
|
|
63
|
|
|
$this->setEntityManager(Database::init($this->configuration)->getEntityManager()); |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setup() { |
|
68
|
|
|
|
|
69
|
|
|
$this->installWorkers(); |
|
70
|
|
|
|
|
71
|
|
|
$this->pushQueueCommands(); |
|
72
|
|
|
$this->pushScheduleCommands(); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function installWorkers() { |
|
77
|
|
|
|
|
78
|
|
|
// add workers |
|
79
|
|
|
$manager = $this->getWorkers(); |
|
80
|
|
|
|
|
81
|
|
|
$schedule_worker = new ScheduleWorker("scheduler"); |
|
82
|
|
|
$schedule_worker |
|
83
|
|
|
->setConfiguration($this->getConfiguration()) |
|
84
|
|
|
->setLogger($this->getLogger()) |
|
85
|
|
|
->setEvents($this->getEvents()) |
|
86
|
|
|
->setTasksTable($this->getTasksTable()) |
|
87
|
|
|
->setEntityManager($this->getEntityManager()); |
|
88
|
|
|
|
|
89
|
|
|
$queue_worker = new QueueWorker("queue"); |
|
90
|
|
|
$queue_worker |
|
91
|
|
|
->setConfiguration($this->getConfiguration()) |
|
92
|
|
|
->setLogger($this->getLogger()) |
|
93
|
|
|
->setEvents($this->getEvents()) |
|
94
|
|
|
->setTasksTable($this->getTasksTable()) |
|
95
|
|
|
->setEntityManager($this->getEntityManager()); |
|
96
|
|
|
|
|
97
|
|
|
$manager |
|
98
|
|
|
->install($schedule_worker, 1, true) |
|
99
|
|
|
->install($queue_worker, 1, true); |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function pushQueueCommands() { |
|
104
|
|
|
|
|
105
|
|
|
$this->getSocket()->getCommands() |
|
106
|
|
View Code Duplication |
->add('queue:add', function(Request $request, $daemon) { |
|
|
|
|
|
|
107
|
|
|
$manager = new QueueManager( |
|
108
|
|
|
$this->getConfiguration(), |
|
109
|
|
|
$this->getLogger(), |
|
110
|
|
|
$this->getEvents(), |
|
111
|
|
|
$this->getEntityManager() |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
return $manager->add($name, $request); |
|
|
|
|
|
|
115
|
|
|
}) |
|
116
|
|
View Code Duplication |
->add('queue:addBulk', function(array $requests, $daemon) { |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
$manager = new QueueManager( |
|
119
|
|
|
$this->getConfiguration(), |
|
120
|
|
|
$this->getLogger(), |
|
121
|
|
|
$this->getEvents(), |
|
122
|
|
|
$this->getEntityManager() |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
return $manager->addBulk($requests); |
|
126
|
|
|
|
|
127
|
|
|
}); |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function pushScheduleCommands() { |
|
132
|
|
|
|
|
133
|
|
|
$this->getSocket()->getCommands()->add('scheduler:refresh', function($data, $daemon) { |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
return $this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh'); |
|
136
|
|
|
|
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.