|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Auryn\Injector; |
|
6
|
|
|
use Bramus\Monolog\Formatter\ColoredLineFormatter; |
|
7
|
|
|
use Jalle19\StatusManager\Configuration\Configuration; |
|
8
|
|
|
use Jalle19\StatusManager\Configuration\Parser as ConfigurationParser; |
|
9
|
|
|
use Jalle19\StatusManager\Configuration\Reader\YamlReader; |
|
10
|
|
|
use Jalle19\StatusManager\Event\Events; |
|
11
|
|
|
use Monolog\Handler\StreamHandler; |
|
12
|
|
|
use Monolog\Logger; |
|
13
|
|
|
use Monolog\Processor\PsrLogMessageProcessor; |
|
14
|
|
|
use Propel\Runtime\Connection\ConnectionManagerSingle; |
|
15
|
|
|
use Propel\Runtime\Propel; |
|
16
|
|
|
use Propel\Runtime\ServiceContainer\StandardServiceContainer; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use React\EventLoop\Factory as EventLoopFactory; |
|
19
|
|
|
use Symfony\Bridge\Monolog\Handler\ConsoleHandler; |
|
20
|
|
|
use Symfony\Component\Console\Command\Command; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class TvheadendStatusManagerCommand |
|
28
|
|
|
* @package Jalle19\StatusManager\Console\Command |
|
29
|
|
|
* @copyright Copyright © Sam Stenvall 2015- |
|
30
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class TvheadendStatusManagerCommand extends Command |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
const COMMAND_NAME = 'tvheadend-status-manager'; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function configure() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->setName(self::COMMAND_NAME); |
|
44
|
|
|
$this->setDescription('Aggregating status manager for tvheadend instances'); |
|
45
|
|
|
|
|
46
|
|
|
// Add arguments |
|
47
|
|
|
$this->addArgument('configFile', InputArgument::REQUIRED, 'The path to the configuration file'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
55
|
|
|
{ |
|
56
|
|
|
// Parse the configuration |
|
57
|
|
|
$configFile = $input->getArgument('configFile'); |
|
58
|
|
|
$configuration = ConfigurationParser::parseConfiguration(new YamlReader($configFile)); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
// Configure Propel and the logger |
|
61
|
|
|
$logger = $this->configureLogger($output, $configuration); |
|
62
|
|
|
$this->configurePropel($configuration, $logger); |
|
63
|
|
|
|
|
64
|
|
|
$injector = new Injector(); |
|
65
|
|
|
|
|
66
|
|
|
// Configure shared instances |
|
67
|
|
|
$eventLoop = EventLoopFactory::create(); |
|
68
|
|
|
$eventDispatcher = new EventDispatcher(); |
|
69
|
|
|
$aliases = [':logger' => $logger, ':loop' => $eventLoop]; |
|
70
|
|
|
|
|
71
|
|
|
$injector->share($configuration) |
|
72
|
|
|
->share($logger) |
|
73
|
|
|
->share($eventDispatcher) |
|
74
|
|
|
->share($eventLoop); |
|
75
|
|
|
|
|
76
|
|
|
// Create managers |
|
77
|
|
|
$statusManager = $injector->make('Jalle19\StatusManager\Manager\StatusManager', $aliases); |
|
78
|
|
|
$instanceStateManager = $injector->make('Jalle19\StatusManager\Manager\InstanceStateManager', $aliases); |
|
79
|
|
|
$webSocketManager = $injector->make('Jalle19\StatusManager\Manager\WebSocketManager', $aliases); |
|
80
|
|
|
$persistenceManager = $injector->make('Jalle19\StatusManager\Manager\PersistenceManager', $aliases); |
|
81
|
|
|
$statisticsManager = $injector->make('Jalle19\StatusManager\Manager\StatisticsManager', $aliases); |
|
82
|
|
|
$inputErrorManager = $injector->make('Jalle19\StatusManager\Manager\InputErrorManager', $aliases); |
|
83
|
|
|
$httpRequestManager = $injector->make('Jalle19\StatusManager\Manager\HttpRequestManager', $aliases); |
|
84
|
|
|
|
|
85
|
|
|
// Wire the event dispatcher |
|
86
|
|
|
$webSocketManager->registerMessageHandler($statisticsManager); |
|
87
|
|
|
$webSocketManager->registerMessageHandler($webSocketManager); |
|
88
|
|
|
|
|
89
|
|
|
$eventDispatcher->addSubscriber($statusManager); |
|
90
|
|
|
$eventDispatcher->addSubscriber($instanceStateManager); |
|
91
|
|
|
$eventDispatcher->addSubscriber($webSocketManager); |
|
92
|
|
|
$eventDispatcher->addSubscriber($persistenceManager); |
|
93
|
|
|
$eventDispatcher->addSubscriber($inputErrorManager); |
|
94
|
|
|
$eventDispatcher->addSubscriber($httpRequestManager); |
|
95
|
|
|
|
|
96
|
|
|
// Configure the event loop and start the application |
|
97
|
|
|
$eventLoop->addPeriodicTimer($configuration->getUpdateInterval(), function () use ($eventDispatcher) |
|
98
|
|
|
{ |
|
99
|
|
|
// Emit an event on each tick |
|
100
|
|
|
$eventDispatcher->dispatch(Events::MAIN_LOOP_TICK); |
|
101
|
|
|
}); |
|
102
|
|
|
|
|
103
|
|
|
$eventDispatcher->dispatch(Events::MAIN_LOOP_STARTING); |
|
104
|
|
|
$eventLoop->run(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Configures and returns the logger instance |
|
110
|
|
|
* |
|
111
|
|
|
* @param OutputInterface $output |
|
112
|
|
|
* @param Configuration $configuration |
|
113
|
|
|
* |
|
114
|
|
|
* @return Logger |
|
115
|
|
|
*/ |
|
116
|
|
|
private function configureLogger(OutputInterface $output, Configuration $configuration) |
|
117
|
|
|
{ |
|
118
|
|
|
$consoleHandler = new ConsoleHandler($output); |
|
119
|
|
|
$consoleHandler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %level_name%: %message%\n")); |
|
120
|
|
|
|
|
121
|
|
|
$logger = new Logger(self::COMMAND_NAME); |
|
122
|
|
|
$logger->pushHandler($consoleHandler); |
|
123
|
|
|
$logger->pushProcessor(new PsrLogMessageProcessor()); |
|
124
|
|
|
|
|
125
|
|
|
if ($configuration->getLogPath() !== null) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$fileHandler = new StreamHandler($configuration->getLogPath()); |
|
128
|
|
|
$logger->pushHandler($fileHandler); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $logger; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Configures the database |
|
137
|
|
|
* |
|
138
|
|
|
* @param Configuration $configuration |
|
139
|
|
|
* @param LoggerInterface $logger |
|
140
|
|
|
*/ |
|
141
|
|
|
private function configurePropel(Configuration $configuration, LoggerInterface $logger) |
|
142
|
|
|
{ |
|
143
|
|
|
$connectionName = 'tvheadend_status_manager'; |
|
144
|
|
|
|
|
145
|
|
|
/* @var StandardServiceContainer $serviceContainer */ |
|
146
|
|
|
$serviceContainer = Propel::getServiceContainer(); |
|
147
|
|
|
$serviceContainer->checkVersion('2.0.0-dev'); |
|
148
|
|
|
$serviceContainer->setAdapterClass($connectionName, 'sqlite'); |
|
149
|
|
|
$manager = new ConnectionManagerSingle(); |
|
150
|
|
|
$manager->setConfiguration([ |
|
151
|
|
|
'classname' => 'Propel\\Runtime\\Connection\\ConnectionWrapper', |
|
152
|
|
|
//'classname' => 'Propel\\Runtime\\Connection\\DebugPDO', |
|
153
|
|
|
'dsn' => 'sqlite:' . $configuration->getDatabasePath(), |
|
154
|
|
|
'user' => null, |
|
155
|
|
|
'password' => '', |
|
156
|
|
|
'attributes' => [ |
|
157
|
|
|
'ATTR_EMULATE_PREPARES' => false, |
|
158
|
|
|
], |
|
159
|
|
|
'settings' => [ |
|
160
|
|
|
'charset' => 'utf8', |
|
161
|
|
|
'queries' => [], |
|
162
|
|
|
], |
|
163
|
|
|
]); |
|
164
|
|
|
$manager->setName($connectionName); |
|
165
|
|
|
$serviceContainer->setConnectionManager($connectionName, $manager); |
|
166
|
|
|
$serviceContainer->setDefaultDatasource($connectionName); |
|
167
|
|
|
|
|
168
|
|
|
$serviceContainer->setLogger($connectionName, $logger); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|