4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-13 20:56:36 +00:00

Implement lazy-loading service locator for Webhook and Sync locators.

This commit is contained in:
Buster "Silver Eagle" Neece 2020-08-28 05:32:33 -05:00
parent 93ff68881f
commit a5a0149d26
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
7 changed files with 109 additions and 78 deletions

View File

@ -377,44 +377,44 @@ return [
},
// Synchronized (Cron) Tasks
App\Sync\TaskCollection::class => function (ContainerInterface $di) {
return new App\Sync\TaskCollection([
App\Sync\TaskCollection::SYNC_NOWPLAYING => [
$di->get(App\Sync\Task\BuildQueue::class),
$di->get(App\Sync\Task\NowPlaying::class),
$di->get(App\Sync\Task\ReactivateStreamer::class),
App\Sync\TaskLocator::class => function (ContainerInterface $di) {
return new App\Sync\TaskLocator($di, [
App\Sync\TaskLocator::SYNC_NOWPLAYING => [
App\Sync\Task\BuildQueue::class,
App\Sync\Task\NowPlaying::class,
App\Sync\Task\ReactivateStreamer::class,
],
App\Sync\TaskCollection::SYNC_SHORT => [
$di->get(App\Sync\Task\RadioRequests::class),
$di->get(App\Sync\Task\Backup::class),
$di->get(App\Sync\Task\RelayCleanup::class),
App\Sync\TaskLocator::SYNC_SHORT => [
App\Sync\Task\RadioRequests::class,
App\Sync\Task\Backup::class,
App\Sync\Task\RelayCleanup::class,
],
App\Sync\TaskCollection::SYNC_MEDIUM => [
$di->get(App\Sync\Task\Media::class),
$di->get(App\Sync\Task\FolderPlaylists::class),
$di->get(App\Sync\Task\CheckForUpdates::class),
App\Sync\TaskLocator::SYNC_MEDIUM => [
App\Sync\Task\Media::class,
App\Sync\Task\FolderPlaylists::class,
App\Sync\Task\CheckForUpdates::class,
],
App\Sync\TaskCollection::SYNC_LONG => [
$di->get(App\Sync\Task\Analytics::class),
$di->get(App\Sync\Task\RadioAutomation::class),
$di->get(App\Sync\Task\HistoryCleanup::class),
$di->get(App\Sync\Task\RotateLogs::class),
$di->get(App\Sync\Task\UpdateGeoLiteDatabase::class),
App\Sync\TaskLocator::SYNC_LONG => [
App\Sync\Task\Analytics::class,
App\Sync\Task\RadioAutomation::class,
App\Sync\Task\HistoryCleanup::class,
App\Sync\Task\RotateLogs::class,
App\Sync\Task\UpdateGeoLiteDatabase::class,
],
]);
},
// Web Hooks
App\Webhook\ConnectorCollection::class => function (
App\Webhook\ConnectorLocator::class => function (
ContainerInterface $di,
App\Config $config
) {
$webhooks = $config->get('webhooks');
$services = [];
foreach ($webhooks['webhooks'] as $webhook_key => $webhook_info) {
$services[$webhook_key] = $di->get($webhook_info['class']);
$services[$webhook_key] = $webhook_info['class'];
}
return new App\Webhook\ConnectorCollection($services);
return new App\Webhook\ConnectorLocator($di, $services);
},
];

View File

@ -18,13 +18,13 @@ class Runner
protected LockManager $lockManager;
protected TaskCollection $taskCollection;
protected TaskLocator $taskCollection;
public function __construct(
SettingsRepository $settingsRepo,
Logger $logger,
LockManager $lockManager,
TaskCollection $taskCollection
TaskLocator $taskCollection
) {
$this->settingsRepo = $settingsRepo;
$this->logger = $logger;
@ -41,7 +41,7 @@ class Runner
*/
public function syncNowplaying($force = false): void
{
$this->runSyncTask(TaskCollection::SYNC_NOWPLAYING);
$this->runSyncTask(TaskLocator::SYNC_NOWPLAYING);
}
/**
@ -52,7 +52,7 @@ class Runner
*/
public function syncShort($force = false): void
{
$this->runSyncTask(TaskCollection::SYNC_SHORT);
$this->runSyncTask(TaskLocator::SYNC_SHORT);
}
/**
@ -63,7 +63,7 @@ class Runner
*/
public function syncMedium($force = false): void
{
$this->runSyncTask(TaskCollection::SYNC_MEDIUM);
$this->runSyncTask(TaskLocator::SYNC_MEDIUM);
}
/**
@ -74,7 +74,7 @@ class Runner
*/
public function syncLong($force = false): void
{
$this->runSyncTask(TaskCollection::SYNC_LONG);
$this->runSyncTask(TaskLocator::SYNC_LONG);
}
public function runSyncTask(string $type, bool $force = false): void
@ -131,7 +131,7 @@ class Runner
$this->settingsRepo->clearCache();
$syncs = [
TaskCollection::SYNC_NOWPLAYING => [
TaskLocator::SYNC_NOWPLAYING => [
'name' => __('Now Playing Data'),
'contents' => [
__('Now Playing Data'),
@ -139,7 +139,7 @@ class Runner
'lastRunSetting' => Entity\Settings::NOWPLAYING_LAST_RUN,
'timeout' => 600,
],
TaskCollection::SYNC_SHORT => [
TaskLocator::SYNC_SHORT => [
'name' => __('1-Minute Sync'),
'contents' => [
__('Song Requests Queue'),
@ -147,7 +147,7 @@ class Runner
'lastRunSetting' => Entity\Settings::SHORT_SYNC_LAST_RUN,
'timeout' => 600,
],
TaskCollection::SYNC_MEDIUM => [
TaskLocator::SYNC_MEDIUM => [
'name' => __('5-Minute Sync'),
'contents' => [
__('Check Media Folders'),
@ -155,7 +155,7 @@ class Runner
'lastRunSetting' => Entity\Settings::MEDIUM_SYNC_LAST_RUN,
'timeout' => 600,
],
TaskCollection::SYNC_LONG => [
TaskLocator::SYNC_LONG => [
'name' => __('1-Hour Sync'),
'contents' => [
__('Analytics/Statistics'),

View File

@ -1,26 +0,0 @@
<?php
namespace App\Sync;
use App\Sync\Task\AbstractTask;
use Doctrine\Common\Collections\ArrayCollection;
class TaskCollection extends ArrayCollection
{
public const SYNC_NOWPLAYING = 'nowplaying';
public const SYNC_SHORT = 'short';
public const SYNC_MEDIUM = 'medium';
public const SYNC_LONG = 'long';
/**
* @param string $type
*
* @return AbstractTask[]
*/
public function getTasks(string $type): array
{
return $this->get($type);
}
}

46
src/Sync/TaskLocator.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace App\Sync;
use App\Sync\Task\AbstractTask;
use Psr\Container\ContainerInterface;
class TaskLocator
{
public const SYNC_NOWPLAYING = 'nowplaying';
public const SYNC_SHORT = 'short';
public const SYNC_MEDIUM = 'medium';
public const SYNC_LONG = 'long';
protected ContainerInterface $di;
protected array $tasks;
public function __construct(ContainerInterface $di, array $tasks)
{
$this->di = $di;
$this->tasks = $tasks;
}
/**
* @param string $type
*
* @return AbstractTask[]
*/
public function getTasks(string $type): array
{
if (!isset($this->tasks[$type])) {
throw new \InvalidArgumentException('Invalid task type specified.');
}
$taskClasses = $this->tasks[$type];
$tasks = [];
foreach ($taskClasses as $taskClass) {
$tasks[] = $this->di->get($taskClass);
}
return $tasks;
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Webhook;
use App\Webhook\Connector\ConnectorInterface;
use Doctrine\Common\Collections\ArrayCollection;
class ConnectorCollection extends ArrayCollection
{
public function getConnector(string $name): ConnectorInterface
{
if ($this->offsetExists($name)) {
return $this->get($name);
}
throw new \InvalidArgumentException('Invalid web hook connector type specified.');
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Webhook;
use App\Webhook\Connector\ConnectorInterface;
use Psr\Container\ContainerInterface;
class ConnectorLocator
{
protected ContainerInterface $di;
protected array $connectors;
public function __construct(ContainerInterface $di, array $connectors)
{
$this->di = $di;
$this->connectors = $connectors;
}
public function getConnector(string $name): ConnectorInterface
{
if (!isset($this->connectors[$name])) {
throw new \InvalidArgumentException('Invalid web hook connector type specified.');
}
$connectorClass = $this->connectors[$name];
return $this->di->get($connectorClass);
}
}

View File

@ -21,7 +21,7 @@ class Dispatcher implements EventSubscriberInterface
protected LocalWebhookHandler $localHandler;
protected ConnectorCollection $connectors;
protected ConnectorLocator $connectors;
protected ApiUtilities $apiUtils;
@ -33,7 +33,7 @@ class Dispatcher implements EventSubscriberInterface
MessageBus $messageBus,
ApiUtilities $apiUtils,
LocalWebhookHandler $localHandler,
ConnectorCollection $connectors
ConnectorLocator $connectors
) {
$this->logger = $logger;
$this->em = $em;