Browse Source

add scheduled playout rebuild (#376)

* configure scheduled playout rebuild

* implement scheduled playout rebuild

* remove variable
pull/377/head
Jason Dove 4 years ago committed by GitHub
parent
commit
944f1e4307
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 10
      ErsatzTV.Application/Playouts/Commands/UpdatePlayout.cs
  3. 65
      ErsatzTV.Application/Playouts/Commands/UpdatePlayoutHandler.cs
  4. 8
      ErsatzTV.Application/Playouts/PlayoutNameViewModel.cs
  5. 9
      ErsatzTV.Application/Playouts/Queries/GetAllPlayoutsHandler.cs
  6. 4
      ErsatzTV.Core/Domain/Playout.cs
  7. 4
      ErsatzTV.Core/Scheduling/PlayoutBuilder.cs
  8. 3153
      ErsatzTV.Infrastructure/Migrations/20210918114317_Add_PlayoutDailyRebuildTime.Designer.cs
  9. 24
      ErsatzTV.Infrastructure/Migrations/20210918114317_Add_PlayoutDailyRebuildTime.cs
  10. 5
      ErsatzTV.Infrastructure/Migrations/TvContextModelSnapshot.cs
  11. 22
      ErsatzTV/Pages/Playouts.razor
  12. 54
      ErsatzTV/Services/SchedulerService.cs
  13. 2
      ErsatzTV/Shared/CopyFFmpegProfileDialog.razor
  14. 2
      ErsatzTV/Shared/CopyWatermarkDialog.razor
  15. 81
      ErsatzTV/Shared/SchedulePlayoutRebuild.razor

2
CHANGELOG.md

@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `Movie Metadata` checks whether all movies have metadata (fallback metadata counts as metadata)
- `Episode Metadata` checks whether all episodes have metadata (fallback metadata counts as metadata)
- `Zero Duration` checks whether all movies and episodes have a valid (non-zero) duration
- Add setting to each playout to schedule an automatic daily rebuild
- This is useful if the playout uses a smart collection with `released_onthisday`
### Fixed
- Fix docker vaapi support for newer Intel platforms (Broadwell and things that end in Lake)

10
ErsatzTV.Application/Playouts/Commands/UpdatePlayout.cs

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
using System;
using ErsatzTV.Core;
using LanguageExt;
using MediatR;
namespace ErsatzTV.Application.Playouts.Commands
{
public record UpdatePlayout
(int PlayoutId, Option<TimeSpan> DailyRebuildTime) : IRequest<Either<BaseError, PlayoutNameViewModel>>;
}

65
ErsatzTV.Application/Playouts/Commands/UpdatePlayoutHandler.cs

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using LanguageExt;
using MediatR;
using Microsoft.EntityFrameworkCore;
using static LanguageExt.Prelude;
namespace ErsatzTV.Application.Playouts.Commands
{
public class UpdatePlayoutHandler : IRequestHandler<UpdatePlayout, Either<BaseError, PlayoutNameViewModel>>
{
private readonly IDbContextFactory<TvContext> _dbContextFactory;
public UpdatePlayoutHandler(IDbContextFactory<TvContext> dbContextFactory) =>
_dbContextFactory = dbContextFactory;
public async Task<Either<BaseError, PlayoutNameViewModel>> Handle(
UpdatePlayout request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
Validation<BaseError, Playout> validation = await Validate(dbContext, request);
return await validation.Apply(playout => ApplyUpdateRequest(dbContext, request, playout));
}
private static async Task<PlayoutNameViewModel> ApplyUpdateRequest(
TvContext dbContext,
UpdatePlayout request,
Playout playout)
{
playout.DailyRebuildTime = null;
foreach (TimeSpan dailyRebuildTime in request.DailyRebuildTime)
{
playout.DailyRebuildTime = dailyRebuildTime;
}
await dbContext.SaveChangesAsync();
return new PlayoutNameViewModel(
playout.Id,
playout.Channel.Name,
playout.Channel.Number,
playout.ProgramSchedule.Name,
Optional(playout.DailyRebuildTime));
}
private static Task<Validation<BaseError, Playout>> Validate(TvContext dbContext, UpdatePlayout request) =>
PlayoutMustExist(dbContext, request);
private static Task<Validation<BaseError, Playout>> PlayoutMustExist(
TvContext dbContext,
UpdatePlayout updatePlayout) =>
dbContext.Playouts
.Include(p => p.Channel)
.Include(p => p.ProgramSchedule)
.SelectOneAsync(p => p.Id, p => p.Id == updatePlayout.PlayoutId)
.Map(o => o.ToValidation<BaseError>("Playout does not exist."));
}
}

8
ErsatzTV.Application/Playouts/PlayoutNameViewModel.cs

@ -1,8 +1,12 @@ @@ -1,8 +1,12 @@
namespace ErsatzTV.Application.Playouts
using System;
using LanguageExt;
namespace ErsatzTV.Application.Playouts
{
public record PlayoutNameViewModel(
int PlayoutId,
string ChannelName,
string ChannelNumber,
string ScheduleName);
string ScheduleName,
Option<TimeSpan> DailyRebuildTime);
}

9
ErsatzTV.Application/Playouts/Queries/GetAllPlayoutsHandler.cs

@ -4,6 +4,7 @@ using System.Threading.Tasks; @@ -4,6 +4,7 @@ using System.Threading.Tasks;
using ErsatzTV.Infrastructure.Data;
using MediatR;
using Microsoft.EntityFrameworkCore;
using static LanguageExt.Prelude;
namespace ErsatzTV.Application.Playouts.Queries
{
@ -21,7 +22,13 @@ namespace ErsatzTV.Application.Playouts.Queries @@ -21,7 +22,13 @@ namespace ErsatzTV.Application.Playouts.Queries
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
return await dbContext.Playouts
.Filter(p => p.Channel != null && p.ProgramSchedule != null)
.Map(p => new PlayoutNameViewModel(p.Id, p.Channel.Name, p.Channel.Number, p.ProgramSchedule.Name))
.Map(
p => new PlayoutNameViewModel(
p.Id,
p.Channel.Name,
p.Channel.Number,
p.ProgramSchedule.Name,
Optional(p.DailyRebuildTime)))
.ToListAsync(cancellationToken);
}
}

4
ErsatzTV.Core/Domain/Playout.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace ErsatzTV.Core.Domain
{
@ -13,5 +14,6 @@ namespace ErsatzTV.Core.Domain @@ -13,5 +14,6 @@ namespace ErsatzTV.Core.Domain
public List<PlayoutItem> Items { get; set; }
public PlayoutAnchor Anchor { get; set; }
public List<PlayoutProgramScheduleAnchor> ProgramScheduleAnchors { get; set; }
public TimeSpan? DailyRebuildTime { get; set; }
}
}

4
ErsatzTV.Core/Scheduling/PlayoutBuilder.cs

@ -103,9 +103,9 @@ namespace ErsatzTV.Core.Scheduling @@ -103,9 +103,9 @@ namespace ErsatzTV.Core.Scheduling
var collectionMediaItems = Map.createRange(tuples);
// using IDisposable scope = _logger.BeginScope(new { PlayoutId = playout.Id });
_logger.LogDebug(
$"{(rebuild ? "Rebuilding" : "Building")} playout {{PlayoutId}} for channel {{ChannelNumber}} - {{ChannelName}}",
"{Action} playout {PlayoutId} for channel {ChannelNumber} - {ChannelName}",
rebuild ? "Rebuilding" : "Building",
playout.Id,
playout.Channel.Number,
playout.Channel.Name);

3153
ErsatzTV.Infrastructure/Migrations/20210918114317_Add_PlayoutDailyRebuildTime.Designer.cs generated

File diff suppressed because it is too large Load Diff

24
ErsatzTV.Infrastructure/Migrations/20210918114317_Add_PlayoutDailyRebuildTime.cs

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ErsatzTV.Infrastructure.Migrations
{
public partial class Add_PlayoutDailyRebuildTime : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<TimeSpan>(
name: "DailyRebuildTime",
table: "Playout",
type: "TEXT",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DailyRebuildTime",
table: "Playout");
}
}
}

5
ErsatzTV.Infrastructure/Migrations/TvContextModelSnapshot.cs

@ -14,7 +14,7 @@ namespace ErsatzTV.Infrastructure.Migrations @@ -14,7 +14,7 @@ namespace ErsatzTV.Infrastructure.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "5.0.9");
.HasAnnotation("ProductVersion", "5.0.10");
modelBuilder.Entity("ErsatzTV.Core.Domain.Actor", b =>
{
@ -1055,6 +1055,9 @@ namespace ErsatzTV.Infrastructure.Migrations @@ -1055,6 +1055,9 @@ namespace ErsatzTV.Infrastructure.Migrations
b.Property<int>("ChannelId")
.HasColumnType("INTEGER");
b.Property<TimeSpan?>("DailyRebuildTime")
.HasColumnType("TEXT");
b.Property<int>("ProgramScheduleId")
.HasColumnType("INTEGER");

22
ErsatzTV/Pages/Playouts.razor

@ -20,7 +20,7 @@ @@ -20,7 +20,7 @@
<ColGroup>
<col/>
<col/>
<col style="width: 120px;"/>
<col style="width: 180px;"/>
</ColGroup>
<HeaderContent>
<MudTh>
@ -47,6 +47,11 @@ @@ -47,6 +47,11 @@
OnClick="@(_ => RebuildPlayout(context))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Schedule Rebuild">
<MudIconButton Icon="@Icons.Material.Filled.Update"
OnClick="@(_ => ScheduleRebuild(context))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Delete Playout">
<MudIconButton Icon="@Icons.Material.Filled.Delete"
OnClick="@(_ => DeletePlayout(context))">
@ -140,6 +145,21 @@ @@ -140,6 +145,21 @@
}
}
private async Task ScheduleRebuild(PlayoutNameViewModel playout)
{
var parameters = new DialogParameters
{
{ "PlayoutId", playout.PlayoutId },
{ "ChannelName", playout.ChannelName },
{ "ScheduleName", playout.ScheduleName },
{ "DailyRebuildTime", playout.DailyRebuildTime }
};
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = _dialog.Show<SchedulePlayoutRebuild>("Schedule Playout Rebuild", parameters, options);
await dialog.Result;
}
private async Task<TableData<PlayoutNameViewModel>> ServerReload(TableState state)
{
await _mediator.Send(new SaveConfigElementByKey(ConfigElementKey.PlayoutsPageSize, state.PageSize.ToString()));

54
ErsatzTV/Services/SchedulerService.cs

@ -44,14 +44,35 @@ namespace ErsatzTV.Services @@ -44,14 +44,35 @@ namespace ErsatzTV.Services
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
DateTime firstRun = DateTime.Now;
// run once immediately at startup
if (!cancellationToken.IsCancellationRequested)
{
await DoWork(cancellationToken);
}
while (!cancellationToken.IsCancellationRequested)
{
int currentMinutes = DateTime.Now.TimeOfDay.Minutes;
int toWait = currentMinutes < 30 ? 30 - currentMinutes : 60 - currentMinutes;
_logger.LogDebug("Scheduler sleeping for {Minutes} minutes", toWait);
await Task.Delay(TimeSpan.FromMinutes(toWait), cancellationToken);
if (!cancellationToken.IsCancellationRequested)
{
await DoWork(cancellationToken);
var roundedMinute = (int)(Math.Round(DateTime.Now.Minute / 5.0) * 5);
if (roundedMinute % 30 == 0)
{
// check for playouts to rebuild every 30 minutes
await RebuildPlayouts(cancellationToken);
}
if (roundedMinute % 60 == 0 && DateTime.Now.Subtract(firstRun) > TimeSpan.FromHours(1))
{
// do other work every hour (on the hour)
await DoWork(cancellationToken);
}
}
await Task.Delay(TimeSpan.FromHours(1), cancellationToken);
}
}
@ -71,6 +92,33 @@ namespace ErsatzTV.Services @@ -71,6 +92,33 @@ namespace ErsatzTV.Services
}
}
private async Task RebuildPlayouts(CancellationToken cancellationToken)
{
try
{
using IServiceScope scope = _serviceScopeFactory.CreateScope();
TvContext dbContext = scope.ServiceProvider.GetRequiredService<TvContext>();
List<Playout> playouts = await dbContext.Playouts
.Filter(p => p.DailyRebuildTime != null)
.Include(p => p.Channel)
.ToListAsync(cancellationToken);
foreach (Playout playout in playouts.OrderBy(p => decimal.Parse(p.Channel.Number)))
{
if (DateTime.Now.Subtract(DateTime.Today.Add(playout.DailyRebuildTime ?? TimeSpan.FromDays(7))) <
TimeSpan.FromMinutes(5))
{
await _workerChannel.WriteAsync(new BuildPlayout(playout.Id, true), cancellationToken);
}
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Error during scheduler run");
}
}
private async Task BuildPlayouts(CancellationToken cancellationToken)
{
using IServiceScope scope = _serviceScopeFactory.CreateScope();

2
ErsatzTV/Shared/CopyFFmpegProfileDialog.razor

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
@using ErsatzTV.Application.FFmpegProfiles.Commands
@inject IMediator _mediator
@inject ISnackbar _snackbar
@inject ILogger<AddToCollectionDialog> _logger
@inject ILogger<CopyFFmpegProfileDialog> _logger
<MudDialog>
<DialogContent>

2
ErsatzTV/Shared/CopyWatermarkDialog.razor

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
@using ErsatzTV.Application.Watermarks.Commands
@inject IMediator _mediator
@inject ISnackbar _snackbar
@inject ILogger<AddToCollectionDialog> _logger
@inject ILogger<CopyWatermarkDialog> _logger
<MudDialog>
<DialogContent>

81
ErsatzTV/Shared/SchedulePlayoutRebuild.razor

@ -0,0 +1,81 @@ @@ -0,0 +1,81 @@
@using System.Globalization
@using ErsatzTV.Application.Playouts
@using ErsatzTV.Application.Playouts.Commands
@inject IMediator _mediator
@inject ISnackbar _snackbar
@inject ILogger<SchedulePlayoutRebuild> _logger
<MudDialog>
<DialogContent>
<EditForm Model="@_dummyModel" OnSubmit="@(_ => Submit())">
<MudContainer Class="mb-6">
<MudText>
@FormatText()
</MudText>
</MudContainer>
<MudSelect Class="mb-6 mx-4" Label="Daily Rebuild Time" @bind-Value="_rebuildTime">
<MudSelectItem Value="@(Option<TimeSpan>.None)">Do not automatically rebuild</MudSelectItem>
@for (var i = 1; i < 48; i++)
{
var time = TimeSpan.FromHours(i * 0.5);
string formatted = DateTime.Today.Add(time).ToShortTimeString();
<MudSelectItem Value="@(Option<TimeSpan>.Some(time))">@formatted</MudSelectItem>
}
</MudSelect>
</EditForm>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel" ButtonType="ButtonType.Reset">Cancel</MudButton>
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">
Save Changes
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
MudDialogInstance MudDialog { get; set; }
[Parameter]
public int PlayoutId { get; set; }
[Parameter]
public string ChannelName { get; set; }
[Parameter]
public string ScheduleName { get; set; }
[Parameter]
public Option<TimeSpan> DailyRebuildTime { get; set; }
private string FormatText() => $"Enter the time that the playout on channel {ChannelName} with schedule {ScheduleName} should rebuild every day";
private record DummyModel;
private readonly DummyModel _dummyModel = new();
private Option<TimeSpan> _rebuildTime;
protected override void OnParametersSet()
{
_rebuildTime = DailyRebuildTime;
}
private async Task Submit()
{
Either<BaseError, PlayoutNameViewModel> maybeResult =
await _mediator.Send(new UpdatePlayout(PlayoutId, _rebuildTime));
maybeResult.Match(
playout => { MudDialog.Close(DialogResult.Ok(playout)); },
error =>
{
_snackbar.Add(error.Value, Severity.Error);
_logger.LogError("Error updating Playout: {Error}", error.Value);
MudDialog.Close(DialogResult.Cancel());
});
}
private void Cancel(MouseEventArgs e) => MudDialog.Cancel();
}
Loading…
Cancel
Save