2026-10-07 10:48:56 +02:00
|
|
|
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.
|
2026-11-10 01:24:43 +01:00
|
|
|
public class ProcessMediaJob(
|
|
|
|
|
IMediaService mediaService,
|
|
|
|
|
ILogger<ProcessMediaJob> logger) : IJobHandler
|
2026-10-07 10:48:56 +02:00
|
|
|
{
|
|
|
|
|
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
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!await mediaService.ProcessPending(pending, token))
|
|
|
|
|
logger.LogInformation("Upload {Id} could not be processed", pending.ID);
|
2026-10-07 10:48:56 +02:00
|
|
|
return JobOutcome.Done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|