Reset playlist queues upon service restart.
This commit is contained in:
parent
c66f85923a
commit
5c3241fd5c
|
@ -162,10 +162,7 @@ final class StationPlaylistMediaRepository extends Repository
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Entity\Api\StationPlaylistQueue[]
|
||||
*/
|
||||
public function resetQueue(Entity\StationPlaylist $playlist, CarbonInterface $now = null): array
|
||||
public function resetQueue(Entity\StationPlaylist $playlist, CarbonInterface $now = null): void
|
||||
{
|
||||
if (Entity\Enums\PlaylistSources::Songs !== $playlist->getSourceEnum()) {
|
||||
throw new InvalidArgumentException('Playlist must contain songs.');
|
||||
|
@ -219,8 +216,19 @@ final class StationPlaylistMediaRepository extends Repository
|
|||
$playlist->setQueueResetAt($now->getTimestamp());
|
||||
$this->em->persist($playlist);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
return $this->getQueue($playlist);
|
||||
public function resetAllQueues(Entity\Station $station): void
|
||||
{
|
||||
$now = CarbonImmutable::now($station->getTimezoneObject());
|
||||
|
||||
foreach ($station->getPlaylists() as $playlist) {
|
||||
if (Entity\Enums\PlaylistSources::Songs !== $playlist->getSourceEnum()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->resetQueue($playlist, $now);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -143,7 +143,7 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
['playlists' => $logPlaylists]
|
||||
);
|
||||
|
||||
$this->weightedShuffle($eligiblePlaylists);
|
||||
$eligiblePlaylists = $this->weightedShuffle($eligiblePlaylists);
|
||||
|
||||
// Loop through the playlists and attempt to play them with no duplicates first,
|
||||
// then loop through them again while allowing duplicates.
|
||||
|
@ -183,16 +183,16 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
* Based on: https://gist.github.com/savvot/e684551953a1716208fbda6c4bb2f344
|
||||
*
|
||||
* @param array $original
|
||||
* @return array
|
||||
*/
|
||||
private function weightedShuffle(array &$original): void
|
||||
private function weightedShuffle(array $original): array
|
||||
{
|
||||
$new = $original;
|
||||
|
||||
$max = 1.0 / mt_getrandmax();
|
||||
|
||||
array_walk(
|
||||
$new,
|
||||
static function (&$value, $key) use ($max): void {
|
||||
static function (&$value) use ($max): void {
|
||||
$value = (mt_rand() * $max) ** (1.0 / $value);
|
||||
}
|
||||
);
|
||||
|
@ -206,7 +206,7 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
}
|
||||
);
|
||||
|
||||
$original = $new;
|
||||
return $new;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -399,7 +399,8 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
): ?Entity\Api\StationPlaylistQueue {
|
||||
$mediaQueue = $this->spmRepo->getQueue($playlist);
|
||||
if (empty($mediaQueue)) {
|
||||
$mediaQueue = $this->spmRepo->resetQueue($playlist);
|
||||
$this->spmRepo->resetQueue($playlist);
|
||||
$mediaQueue = $this->spmRepo->getQueue($playlist);
|
||||
}
|
||||
|
||||
return array_shift($mediaQueue);
|
||||
|
@ -412,7 +413,8 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
): ?Entity\Api\StationPlaylistQueue {
|
||||
$mediaQueue = $this->spmRepo->getQueue($playlist);
|
||||
if (empty($mediaQueue)) {
|
||||
$mediaQueue = $this->spmRepo->resetQueue($playlist);
|
||||
$this->spmRepo->resetQueue($playlist);
|
||||
$mediaQueue = $this->spmRepo->getQueue($playlist);
|
||||
}
|
||||
|
||||
if (!$playlist->getAvoidDuplicates()) {
|
||||
|
@ -429,7 +431,8 @@ final class QueueBuilder implements EventSubscriberInterface
|
|||
'Duplicate prevention yielded no playable song; resetting song queue.'
|
||||
);
|
||||
|
||||
$mediaQueue = $this->spmRepo->resetQueue($playlist);
|
||||
$this->spmRepo->resetQueue($playlist);
|
||||
$mediaQueue = $this->spmRepo->getQueue($playlist);
|
||||
|
||||
return $this->duplicatePrevention->preventDuplicates($mediaQueue, $recentSongHistory, $allowDuplicates);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||
namespace App\Radio;
|
||||
|
||||
use App\Entity\Enums\PlaylistTypes;
|
||||
use App\Entity\Repository\StationPlaylistRepository;
|
||||
use App\Entity\Repository\StationPlaylistMediaRepository;
|
||||
use App\Entity\Station;
|
||||
use App\Entity\StationPlaylist;
|
||||
use App\Environment;
|
||||
|
@ -37,7 +37,8 @@ final class Configuration
|
|||
private readonly Adapters $adapters,
|
||||
private readonly SupervisorInterface $supervisor,
|
||||
private readonly Logger $logger,
|
||||
private readonly Environment $environment
|
||||
private readonly Environment $environment,
|
||||
private readonly StationPlaylistMediaRepository $spmRepo,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -77,6 +78,8 @@ final class Configuration
|
|||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
$this->spmRepo->resetAllQueues($station);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue