diff --git a/src/Console/Command/Sync/NowPlayingCommand.php b/src/Console/Command/Sync/NowPlayingCommand.php index 28ad5a922..12a393c6f 100644 --- a/src/Console/Command/Sync/NowPlayingCommand.php +++ b/src/Console/Command/Sync/NowPlayingCommand.php @@ -64,6 +64,17 @@ final class NowPlayingCommand extends CommandAbstract $timeout = (int)$input->getOption('timeout'); + $delay = $this->environment->getNowPlayingDelayTime(); + + // If delay is default value, auto-scale it with station count. + if ($delay < 1) { + $numStations = $this->stationRepo->getActiveCount(); + $delay = (int)min( + 4 + round($numStations / 5), + 20 + ); + } + $this->logger->pushProcessor( function (LogRecord $record) use ($station) { $record->extra['station'] = [ @@ -76,7 +87,7 @@ final class NowPlayingCommand extends CommandAbstract $this->logger->info('Starting Now Playing sync task.'); - $this->loop($station, $timeout, $this->environment->getNowPlayingDelayTime()); + $this->loop($station, $timeout, $delay); $this->logger->info('Now Playing sync task complete.'); $this->logger->popProcessor(); diff --git a/src/Entity/Repository/StationRepository.php b/src/Entity/Repository/StationRepository.php index 3d307168f..7c9c8c764 100644 --- a/src/Entity/Repository/StationRepository.php +++ b/src/Entity/Repository/StationRepository.php @@ -39,8 +39,15 @@ final class StationRepository extends Repository : $this->repository->findOneBy(['short_name' => $identifier]); } - /** - */ + public function getActiveCount(): int + { + return $this->em->createQuery( + <<<'DQL' + SELECT COUNT(s.id) FROM App\Entity\Station s WHERE s.is_enabled = 1 + DQL + )->getSingleScalarResult(); + } + public function fetchAll(): mixed { return $this->em->createQuery( diff --git a/src/Environment.php b/src/Environment.php index 287c839bb..d580ddbbe 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -83,7 +83,7 @@ final class Environment self::SYNC_SHORT_EXECUTION_TIME => 600, self::SYNC_LONG_EXECUTION_TIME => 1800, - self::NOW_PLAYING_DELAY_TIME => 5, + self::NOW_PLAYING_DELAY_TIME => 0, self::PROFILING_EXTENSION_ENABLED => 0, self::PROFILING_EXTENSION_ALWAYS_ON => 0,