Browse Source

fix stuck playout builds (#235)

* fix stuck playout builds

* code cleanup
pull/236/head
Jason Dove 4 years ago committed by GitHub
parent
commit
9f575dbd94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      ErsatzTV.Core.Tests/Scheduling/ChronologicalContentTests.cs
  2. 10
      ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs
  3. 13
      ErsatzTV.Core.Tests/Scheduling/ShuffledContentTests.cs
  4. 7
      ErsatzTV.Core/Scheduling/ChronologicalMediaCollectionEnumerator.cs
  5. 6
      ErsatzTV.Core/Scheduling/ShuffledMediaCollectionEnumerator.cs
  6. 6
      ErsatzTV/Services/SchedulerService.cs

13
ErsatzTV.Core.Tests/Scheduling/ChronologicalContentTests.cs

@ -60,6 +60,19 @@ namespace ErsatzTV.Core.Tests.Scheduling @@ -60,6 +60,19 @@ namespace ErsatzTV.Core.Tests.Scheduling
}
}
[Test]
[Timeout(1000)]
public void State_Should_Reset_When_Invalid()
{
List<MediaItem> contents = Episodes(10);
var state = new CollectionEnumeratorState { Index = 10 };
var chronologicalContent = new ChronologicalMediaCollectionEnumerator(contents, state);
chronologicalContent.State.Index.Should().Be(0);
chronologicalContent.State.Seed.Should().Be(0);
}
private static List<MediaItem> Episodes(int count) =>
Range(1, count).Map(
i => (MediaItem) new Episode

10
ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs

@ -78,6 +78,16 @@ namespace ErsatzTV.Core.Tests.Scheduling @@ -78,6 +78,16 @@ namespace ErsatzTV.Core.Tests.Scheduling
}
}
[Test]
[Timeout(1000)]
public void State_Index_Should_Continue_Past_End_Of_Items()
{
List<MediaItem> contents = Episodes(10);
var state = new CollectionEnumeratorState { Index = 10, Seed = KnownSeed };
var _ = new RandomizedMediaCollectionEnumerator(contents, state);
}
private static List<MediaItem> Episodes(int count) =>
Range(1, count).Map(
i => (MediaItem) new Episode

13
ErsatzTV.Core.Tests/Scheduling/ShuffledContentTests.cs

@ -116,6 +116,19 @@ namespace ErsatzTV.Core.Tests.Scheduling @@ -116,6 +116,19 @@ namespace ErsatzTV.Core.Tests.Scheduling
}
}
[Test]
[Timeout(1000)]
public void State_Should_Reset_When_Invalid()
{
List<MediaItem> contents = Episodes(10);
var state = new CollectionEnumeratorState { Index = 10, Seed = MagicSeed };
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
shuffledContent.State.Index.Should().Be(0);
shuffledContent.State.Seed.Should().NotBe(MagicSeed);
}
private static List<MediaItem> Episodes(int count) =>
Range(1, count).Map(
i => (MediaItem) new Episode

7
ErsatzTV.Core/Scheduling/ChronologicalMediaCollectionEnumerator.cs

@ -18,6 +18,13 @@ namespace ErsatzTV.Core.Scheduling @@ -18,6 +18,13 @@ namespace ErsatzTV.Core.Scheduling
_sortedMediaItems = mediaItems.OrderBy(identity, new ChronologicalMediaComparer()).ToList();
State = new CollectionEnumeratorState { Seed = state.Seed };
if (state.Index >= _sortedMediaItems.Count)
{
state.Index = 0;
state.Seed = 0;
}
while (State.Index < state.Index)
{
MoveNext();

6
ErsatzTV.Core/Scheduling/ShuffledMediaCollectionEnumerator.cs

@ -26,6 +26,12 @@ namespace ErsatzTV.Core.Scheduling @@ -26,6 +26,12 @@ namespace ErsatzTV.Core.Scheduling
? MultiPartEpisodeGrouper.GroupMediaItems(mediaItems)
: mediaItems.Map(mi => new GroupedMediaItem(mi, null)).ToList();
if (state.Index >= _mediaItems.Count)
{
state.Index = 0;
state.Seed = new Random(state.Seed).Next();
}
_random = new Random(state.Seed);
_shuffled = Shuffle(_mediaItems, _random);

6
ErsatzTV/Services/SchedulerService.cs

@ -76,8 +76,10 @@ namespace ErsatzTV.Services @@ -76,8 +76,10 @@ namespace ErsatzTV.Services
using IServiceScope scope = _serviceScopeFactory.CreateScope();
TvContext dbContext = scope.ServiceProvider.GetRequiredService<TvContext>();
List<int> playoutIds = await dbContext.Playouts.Map(p => p.Id).ToListAsync(cancellationToken);
foreach (int playoutId in playoutIds)
List<Playout> playouts = await dbContext.Playouts
.Include(p => p.Channel)
.ToListAsync(cancellationToken);
foreach (int playoutId in playouts.OrderBy(p => decimal.Parse(p.Channel.Number)).Map(p => p.Id))
{
await _workerChannel.WriteAsync(new BuildPlayout(playoutId), cancellationToken);
}

Loading…
Cancel
Save