From 05961eb7002f583c875f76b20d93f6eaafd49baf Mon Sep 17 00:00:00 2001 From: nanxun Date: Sat, 25 Apr 2026 14:20:19 +0800 Subject: [PATCH] fix: restart live recording after session deletion --- .../Services/RecordService.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/LiveRecorder.Application/Services/RecordService.cs b/src/LiveRecorder.Application/Services/RecordService.cs index fac3016..148a4eb 100644 --- a/src/LiveRecorder.Application/Services/RecordService.cs +++ b/src/LiveRecorder.Application/Services/RecordService.cs @@ -276,6 +276,7 @@ public sealed class RecordService var deletedFilePaths = new List(); var deletedDanmakuPaths = new List(); var affectedSessionIds = new HashSet(); + var affectedLiveRoomIds = new HashSet(); foreach (var taskId in taskIds) { @@ -310,6 +311,7 @@ public sealed class RecordService _recordTaskRepository.Remove(recordTask); deletedTaskIds.Add(recordTask.Id); affectedSessionIds.Add(recordTask.RecordSessionId); + affectedLiveRoomIds.Add(recordTask.LiveRoomId); } var deletedSessionIds = new List(); @@ -357,6 +359,11 @@ public sealed class RecordService cancellationToken: cancellationToken); } + if ((deletedTaskIds.Count > 0 || deletedSessionIds.Count > 0) && affectedLiveRoomIds.Count > 0) + { + await TryAutoRestartDeletedLiveRoomsAsync(affectedLiveRoomIds, cancellationToken); + } + return new DeleteCompletedRecordTasksResultDto { DeletedTaskIds = deletedTaskIds, @@ -457,6 +464,80 @@ public sealed class RecordService } } + private async Task TryAutoRestartDeletedLiveRoomsAsync( + IReadOnlyCollection liveRoomIds, + CancellationToken cancellationToken) + { + if (liveRoomIds.Count == 0) + { + return; + } + + var settings = await _systemSettingsService.GetAsync(cancellationToken); + if (!settings.AutoStartRecordingOnLive) + { + return; + } + + var storageCheck = _storageGuardService.CheckCanStartOrResume(settings); + if (!storageCheck.HasEnoughSpace) + { + foreach (var liveRoomId in liveRoomIds) + { + await _systemLogService.WriteAsync( + SystemLogLevel.Warning, + "Storage", + "Immediate auto-start after session deletion skipped because storage is below resume threshold.", + storageCheck.Message, + liveRoomId: liveRoomId, + cancellationToken: cancellationToken); + } + + return; + } + + foreach (var liveRoomId in liveRoomIds) + { + var liveRoom = await _liveRoomRepository.GetByIdAsync(liveRoomId, cancellationToken); + if (liveRoom is null || !liveRoom.IsEnabled || liveRoom.AvailabilityStatus != LiveRoomAvailabilityStatus.Live) + { + continue; + } + + if (await _recordSessionRepository.GetActiveByLiveRoomIdAsync(liveRoom.Id, cancellationToken) is not null) + { + continue; + } + + try + { + await _systemLogService.WriteAsync( + SystemLogLevel.Info, + "RecordSession", + "Live room is still online after session deletion. Attempting immediate auto-start.", + liveRoomId: liveRoom.Id, + cancellationToken: cancellationToken); + + await StartAsync( + new StartRecordTaskRequest + { + LiveRoomId = liveRoom.Id + }, + cancellationToken); + } + catch (Exception ex) + { + await _systemLogService.WriteAsync( + SystemLogLevel.Warning, + "RecordSession", + "Immediate auto-start after session deletion did not start a recording session.", + ex.ToString(), + liveRoomId: liveRoom.Id, + cancellationToken: cancellationToken); + } + } + } + internal static DeleteCompletedRecordTasksResultDto CreateEmptyDeleteResult() => new() { DeletedTaskIds = [],