45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using MongoDB.Entities;
|
|||
|
|
|
||
|
|
using PrivaPub.Infrastructure.Jobs;
|
||
|
|
using PrivaPub.Models.Jobs;
|
||
|
|
using PrivaPub.Models.Media;
|
||
|
|
|
||
|
|
namespace PrivaPub.Domain.Media
|
||
|
|
{
|
||
|
|
// Audio and video sent to /api/v2/media, processed off the request (its lease renewed while ffmpeg runs): playable, or
|
||
|
|
// marked failed with why. One at a time, since a transcode takes the machine's cores.
|
||
|
|
public class ProcessMediaJob : IJobHandler
|
||
|
|
{
|
||
|
|
readonly IMediaService _media;
|
||
|
|
readonly ILogger<ProcessMediaJob> _logger;
|
||
|
|
|
||
|
|
public ProcessMediaJob(IMediaService media, ILogger<ProcessMediaJob> logger)
|
||
|
|
{
|
||
|
|
_media = media;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
public JobKind Kind => JobKind.ProcessMedia;
|
||
|
|
public int Concurrency => 1;
|
||
|
|
public int MaxAttempts => 3;
|
||
|
|
public int PerHostLimit => 1;
|
||
|
|
|
||
|
|
public static Job JobFor(MediaAttachment pending, string host) => new()
|
||
|
|
{
|
||
|
|
Kind = JobKind.ProcessMedia,
|
||
|
|
Payload = pending.ID,
|
||
|
|
Host = host,
|
||
|
|
DedupeKey = $"media|{pending.ID}"
|
||
|
|
};
|
||
|
|
|
||
|
|
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||
|
|
{
|
||
|
|
var pending = await DB.Default.Find<MediaAttachment>().Match(m => m.ID == job.Payload && m.ProcessingState == "pending" && m.TrashedAt == null).ExecuteFirstAsync(token);
|
||
|
|
if (pending == default)
|
||
|
|
return JobOutcome.Done;//processed already, or trashed meanwhile
|
||
|
|
if (!await _media.ProcessPending(pending, token))
|
||
|
|
_logger.LogInformation("Upload {Id} could not be processed", pending.ID);
|
||
|
|
return JobOutcome.Done;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|