mirror of
https://github.com/AzuraCast/AzuraCast.git
synced 2024-06-13 20:56:36 +00:00
Make Sync (cron) tasks an event that plugins can attach to.
This commit is contained in:
parent
f8770145ec
commit
a69c802a78
|
@ -110,6 +110,7 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
App\Radio\AutoDJ\Annotations::class,
|
||||
App\Radio\Backend\Liquidsoap\ConfigWriter::class,
|
||||
App\Sync\Task\NowPlaying::class,
|
||||
App\Sync\TaskLocator::class,
|
||||
App\Webhook\Dispatcher::class,
|
||||
App\Controller\Api\NowplayingController::class,
|
||||
App\Notification\Manager::class,
|
||||
|
|
|
@ -383,22 +383,22 @@ return [
|
|||
// Synchronized (Cron) Tasks
|
||||
App\Sync\TaskLocator::class => function (ContainerInterface $di) {
|
||||
return new App\Sync\TaskLocator($di, [
|
||||
App\Sync\TaskLocator::SYNC_NOWPLAYING => [
|
||||
App\Event\GetSyncTasks::SYNC_NOWPLAYING => [
|
||||
App\Sync\Task\BuildQueue::class,
|
||||
App\Sync\Task\NowPlaying::class,
|
||||
App\Sync\Task\ReactivateStreamer::class,
|
||||
],
|
||||
App\Sync\TaskLocator::SYNC_SHORT => [
|
||||
App\Event\GetSyncTasks::SYNC_SHORT => [
|
||||
App\Sync\Task\RadioRequests::class,
|
||||
App\Sync\Task\Backup::class,
|
||||
App\Sync\Task\RelayCleanup::class,
|
||||
],
|
||||
App\Sync\TaskLocator::SYNC_MEDIUM => [
|
||||
App\Event\GetSyncTasks::SYNC_MEDIUM => [
|
||||
App\Sync\Task\Media::class,
|
||||
App\Sync\Task\FolderPlaylists::class,
|
||||
App\Sync\Task\CheckForUpdates::class,
|
||||
],
|
||||
App\Sync\TaskLocator::SYNC_LONG => [
|
||||
App\Event\GetSyncTasks::SYNC_LONG => [
|
||||
App\Sync\Task\Analytics::class,
|
||||
App\Sync\Task\RadioAutomation::class,
|
||||
App\Sync\Task\HistoryCleanup::class,
|
||||
|
|
51
src/Event/GetSyncTasks.php
Normal file
51
src/Event/GetSyncTasks.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
namespace App\Event;
|
||||
|
||||
use App\Sync\Task\AbstractTask;
|
||||
|
||||
class GetSyncTasks
|
||||
{
|
||||
public const SYNC_NOWPLAYING = 'nowplaying';
|
||||
public const SYNC_SHORT = 'short';
|
||||
public const SYNC_MEDIUM = 'medium';
|
||||
public const SYNC_LONG = 'long';
|
||||
|
||||
protected string $type;
|
||||
|
||||
protected array $tasks = [];
|
||||
|
||||
public function __construct(string $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractTask[]
|
||||
*/
|
||||
public function getTasks(): array
|
||||
{
|
||||
return $this->tasks;
|
||||
}
|
||||
|
||||
public function addTask(AbstractTask $task, ?string $key = null): void
|
||||
{
|
||||
if (null === $key) {
|
||||
$taskClassParts = explode("\\", get_class($task));
|
||||
$key = array_pop($taskClassParts);
|
||||
}
|
||||
|
||||
$this->tasks[$key] = $task;
|
||||
}
|
||||
|
||||
public function removeTask(string $key): void
|
||||
{
|
||||
if (isset($this->tasks[$key])) {
|
||||
unset($this->tasks[$key]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,8 @@ namespace App\Sync;
|
|||
|
||||
use App\Entity;
|
||||
use App\Entity\Repository\SettingsRepository;
|
||||
use App\Event\GetSyncTasks;
|
||||
use App\EventDispatcher;
|
||||
use App\Lock\LockManager;
|
||||
use App\Settings;
|
||||
use Monolog\Logger;
|
||||
|
@ -18,18 +20,18 @@ class Runner
|
|||
|
||||
protected LockManager $lockManager;
|
||||
|
||||
protected TaskLocator $taskCollection;
|
||||
protected EventDispatcher $eventDispatcher;
|
||||
|
||||
public function __construct(
|
||||
SettingsRepository $settingsRepo,
|
||||
Logger $logger,
|
||||
LockManager $lockManager,
|
||||
TaskLocator $taskCollection
|
||||
EventDispatcher $eventDispatcher
|
||||
) {
|
||||
$this->settingsRepo = $settingsRepo;
|
||||
$this->logger = $logger;
|
||||
$this->lockManager = $lockManager;
|
||||
$this->taskCollection = $taskCollection;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,7 +43,7 @@ class Runner
|
|||
*/
|
||||
public function syncNowplaying($force = false): void
|
||||
{
|
||||
$this->runSyncTask(TaskLocator::SYNC_NOWPLAYING);
|
||||
$this->runSyncTask(GetSyncTasks::SYNC_NOWPLAYING, $force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,7 +54,7 @@ class Runner
|
|||
*/
|
||||
public function syncShort($force = false): void
|
||||
{
|
||||
$this->runSyncTask(TaskLocator::SYNC_SHORT);
|
||||
$this->runSyncTask(GetSyncTasks::SYNC_SHORT, $force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,7 +65,7 @@ class Runner
|
|||
*/
|
||||
public function syncMedium($force = false): void
|
||||
{
|
||||
$this->runSyncTask(TaskLocator::SYNC_MEDIUM);
|
||||
$this->runSyncTask(GetSyncTasks::SYNC_MEDIUM, $force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,7 +76,7 @@ class Runner
|
|||
*/
|
||||
public function syncLong($force = false): void
|
||||
{
|
||||
$this->runSyncTask(TaskLocator::SYNC_LONG);
|
||||
$this->runSyncTask(GetSyncTasks::SYNC_LONG, $force);
|
||||
}
|
||||
|
||||
public function runSyncTask(string $type, bool $force = false): void
|
||||
|
@ -101,13 +103,13 @@ class Runner
|
|||
$lock = $this->lockManager->getLock('sync_' . $type, $syncInfo['timeout'], $force);
|
||||
|
||||
$lock->run(function () use ($syncInfo, $type, $force) {
|
||||
$tasks = $this->taskCollection->getTasks($type);
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
// Filter namespace name
|
||||
$timer_description_parts = explode("\\", get_class($task));
|
||||
$timer_description = array_pop($timer_description_parts);
|
||||
$event = new GetSyncTasks($type);
|
||||
$this->eventDispatcher->dispatch($event);
|
||||
|
||||
$tasks = $event->getTasks();
|
||||
|
||||
foreach ($tasks as $taskClass => $task) {
|
||||
$start_time = microtime(true);
|
||||
|
||||
$task->run($force);
|
||||
|
@ -117,7 +119,7 @@ class Runner
|
|||
|
||||
$this->logger->debug(sprintf(
|
||||
'Timer "%s" completed in %01.3f second(s).',
|
||||
$timer_description,
|
||||
$taskClass,
|
||||
round($time_diff, 3)
|
||||
));
|
||||
}
|
||||
|
@ -131,7 +133,7 @@ class Runner
|
|||
$this->settingsRepo->clearCache();
|
||||
|
||||
$syncs = [
|
||||
TaskLocator::SYNC_NOWPLAYING => [
|
||||
GetSyncTasks::SYNC_NOWPLAYING => [
|
||||
'name' => __('Now Playing Data'),
|
||||
'contents' => [
|
||||
__('Now Playing Data'),
|
||||
|
@ -139,7 +141,7 @@ class Runner
|
|||
'lastRunSetting' => Entity\Settings::NOWPLAYING_LAST_RUN,
|
||||
'timeout' => 600,
|
||||
],
|
||||
TaskLocator::SYNC_SHORT => [
|
||||
GetSyncTasks::SYNC_SHORT => [
|
||||
'name' => __('1-Minute Sync'),
|
||||
'contents' => [
|
||||
__('Song Requests Queue'),
|
||||
|
@ -147,7 +149,7 @@ class Runner
|
|||
'lastRunSetting' => Entity\Settings::SHORT_SYNC_LAST_RUN,
|
||||
'timeout' => 600,
|
||||
],
|
||||
TaskLocator::SYNC_MEDIUM => [
|
||||
GetSyncTasks::SYNC_MEDIUM => [
|
||||
'name' => __('5-Minute Sync'),
|
||||
'contents' => [
|
||||
__('Check Media Folders'),
|
||||
|
@ -155,7 +157,7 @@ class Runner
|
|||
'lastRunSetting' => Entity\Settings::MEDIUM_SYNC_LAST_RUN,
|
||||
'timeout' => 600,
|
||||
],
|
||||
TaskLocator::SYNC_LONG => [
|
||||
GetSyncTasks::SYNC_LONG => [
|
||||
'name' => __('1-Hour Sync'),
|
||||
'contents' => [
|
||||
__('Analytics/Statistics'),
|
||||
|
|
|
@ -1,19 +1,12 @@
|
|||
<?php
|
||||
namespace App\Sync;
|
||||
|
||||
use App\Sync\Task\AbstractTask;
|
||||
use App\Event\GetSyncTasks;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
class TaskLocator
|
||||
class TaskLocator implements EventSubscriberInterface
|
||||
{
|
||||
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;
|
||||
|
@ -24,23 +17,25 @@ class TaskLocator
|
|||
$this->tasks = $tasks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return AbstractTask[]
|
||||
*/
|
||||
public function getTasks(string $type): array
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
GetSyncTasks::class => [
|
||||
['assignTasks', 0],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function assignTasks(GetSyncTasks $event): void
|
||||
{
|
||||
$type = $event->getType();
|
||||
if (!isset($this->tasks[$type])) {
|
||||
throw new \InvalidArgumentException('Invalid task type specified.');
|
||||
return;
|
||||
}
|
||||
|
||||
$taskClasses = $this->tasks[$type];
|
||||
$tasks = [];
|
||||
foreach ($taskClasses as $taskClass) {
|
||||
$tasks[] = $this->di->get($taskClass);
|
||||
$event->addTask($this->di->get($taskClass));
|
||||
}
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user