fix: improve recovery and auto-start flows

This commit is contained in:
2026-04-25 00:06:25 +08:00
parent 39db8c012f
commit 9701c88c8b
5 changed files with 207 additions and 14 deletions
@@ -4,6 +4,7 @@ using LiveRecorder.Application.Abstractions.Platforms;
using LiveRecorder.Application.Abstractions.Recording;
using LiveRecorder.Application.Abstractions.Settings;
using LiveRecorder.Application.Models.LiveRooms;
using LiveRecorder.Application.Models.RecordTasks;
using LiveRecorder.Domain.Entities;
using LiveRecorder.Domain.Enums;
@@ -17,6 +18,7 @@ public sealed class LiveRoomService
private readonly LiveRoomRecordingSettingsResolver _liveRoomRecordingSettingsResolver;
private readonly ISystemSettingsService _systemSettingsService;
private readonly StoppedOrphanRecordSessionCleanupService _stoppedOrphanRecordSessionCleanupService;
private readonly RecordService _recordService;
private readonly IUnitOfWork _unitOfWork;
private readonly ISystemLogService _systemLogService;
@@ -27,6 +29,7 @@ public sealed class LiveRoomService
LiveRoomRecordingSettingsResolver liveRoomRecordingSettingsResolver,
ISystemSettingsService systemSettingsService,
StoppedOrphanRecordSessionCleanupService stoppedOrphanRecordSessionCleanupService,
RecordService recordService,
IUnitOfWork unitOfWork,
ISystemLogService systemLogService)
{
@@ -36,6 +39,7 @@ public sealed class LiveRoomService
_liveRoomRecordingSettingsResolver = liveRoomRecordingSettingsResolver;
_systemSettingsService = systemSettingsService;
_stoppedOrphanRecordSessionCleanupService = stoppedOrphanRecordSessionCleanupService;
_recordService = recordService;
_unitOfWork = unitOfWork;
_systemLogService = systemLogService;
}
@@ -214,6 +218,7 @@ public sealed class LiveRoomService
$"Live room resolved: {liveRoom.RoomId} ({liveRoom.Platform}).",
liveRoomId: liveRoom.Id,
cancellationToken: cancellationToken);
await TryAutoStartRecordingAsync(liveRoom, liveStatus, cancellationToken);
var effectiveSettings = await _liveRoomRecordingSettingsResolver.ResolveAsync(liveRoom, cancellationToken);
return (liveRoom, effectiveSettings, created);
@@ -238,6 +243,7 @@ public sealed class LiveRoomService
detail: $"status={liveStatus.StatusCode}, rawStatus={liveStatus.RawStatus}",
liveRoomId: room.Id,
cancellationToken: cancellationToken);
await TryAutoStartRecordingAsync(room, liveStatus, cancellationToken);
var effectiveSettings = await _liveRoomRecordingSettingsResolver.ResolveAsync(room, cancellationToken);
return Map(room, effectiveSettings);
@@ -405,6 +411,56 @@ public sealed class LiveRoomService
return rooms.ToDictionary(item => item.Id, item => _liveRoomRecordingSettingsResolver.Resolve(item, systemSettings));
}
private async Task TryAutoStartRecordingAsync(
LiveRoom liveRoom,
LiveStatusSnapshot liveStatus,
CancellationToken cancellationToken)
{
if (!liveStatus.IsLive || !liveRoom.IsEnabled)
{
return;
}
var settings = await _systemSettingsService.GetAsync(cancellationToken);
if (!settings.AutoStartRecordingOnLive)
{
await _systemLogService.WriteAsync(
SystemLogLevel.Info,
"LiveRoom",
"Live room is already online, but immediate auto-start is disabled by settings.",
liveRoomId: liveRoom.Id,
cancellationToken: cancellationToken);
return;
}
await _systemLogService.WriteAsync(
SystemLogLevel.Info,
"LiveRoom",
"Live room is already online. Attempting immediate auto-start after resolve/refresh.",
liveRoomId: liveRoom.Id,
cancellationToken: cancellationToken);
try
{
await _recordService.StartAsync(
new StartRecordTaskRequest
{
LiveRoomId = liveRoom.Id
},
cancellationToken);
}
catch (Exception ex)
{
await _systemLogService.WriteAsync(
ex is InvalidOperationException ? SystemLogLevel.Warning : SystemLogLevel.Error,
"LiveRoom",
"Immediate auto-start after resolve/refresh did not start a recording session.",
ex.Message,
liveRoomId: liveRoom.Id,
cancellationToken: cancellationToken);
}
}
private LiveRoomDto Map(LiveRoom room, RecordingExecutionSettings effectiveSettings) => new()
{
Id = room.Id,