fix: address #167 review — release Trakt lock on failed enqueue; mark busy after generatePlaylist save
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -76,13 +76,7 @@ public partial class TraktController(
|
||||
CreateProblemDetails(422, "Validation failed", "Invalid Trakt list url"));
|
||||
}
|
||||
|
||||
if (!entityLocker.LockTrakt())
|
||||
{
|
||||
return ConflictProblem();
|
||||
}
|
||||
|
||||
await workerChannel.WriteAsync(AddTraktList.FromUrl(request.Url), cancellationToken);
|
||||
return new AcceptedResult();
|
||||
return await EnqueueWithTraktLock(AddTraktList.FromUrl(request.Url), cancellationToken);
|
||||
}
|
||||
|
||||
[HttpPost("/api/trakt/lists/{id:int}/match")]
|
||||
@@ -100,13 +94,7 @@ public partial class TraktController(
|
||||
return ApiResults.NotFoundProblem();
|
||||
}
|
||||
|
||||
if (!entityLocker.LockTrakt())
|
||||
{
|
||||
return ConflictProblem();
|
||||
}
|
||||
|
||||
await workerChannel.WriteAsync(new MatchTraktListItems(id), cancellationToken);
|
||||
return new AcceptedResult();
|
||||
return await EnqueueWithTraktLock(new MatchTraktListItems(id), cancellationToken);
|
||||
}
|
||||
|
||||
[HttpDelete("/api/trakt/lists/{id:int}")]
|
||||
@@ -124,13 +112,7 @@ public partial class TraktController(
|
||||
return ApiResults.NotFoundProblem();
|
||||
}
|
||||
|
||||
if (!entityLocker.LockTrakt())
|
||||
{
|
||||
return ConflictProblem();
|
||||
}
|
||||
|
||||
await workerChannel.WriteAsync(new DeleteTraktList(id), cancellationToken);
|
||||
return new AcceptedResult();
|
||||
return await EnqueueWithTraktLock(new DeleteTraktList(id), cancellationToken);
|
||||
}
|
||||
|
||||
[HttpPut("/api/trakt/lists/{id:int}")]
|
||||
@@ -185,6 +167,30 @@ public partial class TraktController(
|
||||
viewModel.AutoRefresh,
|
||||
viewModel.GeneratePlaylist);
|
||||
|
||||
private async Task<IActionResult> EnqueueWithTraktLock(
|
||||
IBackgroundServiceRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!entityLocker.LockTrakt())
|
||||
{
|
||||
return ConflictProblem();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await workerChannel.WriteAsync(request, cancellationToken);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// the background handler only unlocks when it receives the message;
|
||||
// if enqueueing fails (e.g. request aborted), release the lock here or it is held forever
|
||||
entityLocker.UnlockTrakt();
|
||||
throw;
|
||||
}
|
||||
|
||||
return new AcceptedResult();
|
||||
}
|
||||
|
||||
private static ConflictObjectResult ConflictProblem() =>
|
||||
new ConflictObjectResult(
|
||||
CreateProblemDetails(
|
||||
|
||||
Reference in New Issue
Block a user