diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-28 17:43:02 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-28 17:43:02 -0600 |
| commit | 7450257b3d0522b9a3e0aa326148e3491d43ce52 (patch) | |
| tree | 6bf0d980f7ff140a5243750329048b7894bad900 /src | |
| parent | 8792e614e7c3b181bdb37bb6b94bce08f04ab256 (diff) | |
| parent | b0efa281523b825c1272c83f8ced3b71d07f3ab2 (diff) | |
| download | slskd-7450257b3d0522b9a3e0aa326148e3491d43ce52.tar.gz | |
Merge pull request #1546 from slskd/tryfail
Simplify transfer-failing logic
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 48 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 128 |
2 files changed, 115 insertions, 61 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 9d0a8ed9..d906688a 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -108,6 +108,20 @@ namespace slskd.Transfers.Downloads bool TryCancel(Guid id); /// <summary> + /// Fails the download matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the download.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the download was successfully failed.</returns> + bool TryFail(Guid id, Exception exception); + + /// <summary> /// Updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -785,26 +799,34 @@ namespace slskd.Transfers.Downloads { var t = Find(t => t.Id == id); - if (t is not null) + if (t is null) { - t.EndedAt ??= DateTime.UtcNow; - t.Exception ??= exception.Message; + return false; + } + + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception.Message; - if (!t.State.HasFlag(TransferStates.Completed)) + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | exception switch { - t.State = TransferStates.Completed | exception switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; - } + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + } + try + { Update(t); return true; } - - return false; + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } } /// <summary> diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 4b35bffb..c541d4f0 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -126,6 +126,20 @@ namespace slskd.Transfers.Uploads bool TryCancel(Guid id); /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + bool TryFail(Guid id, Exception exception); + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -418,28 +432,22 @@ namespace slskd.Transfers.Uploads return transfer; } - catch (NotFoundException) + catch (NotFoundException ex) { - transfer.EndedAt = DateTime.Now; - transfer.Exception = "File could not be found"; - transfer.State = TransferStates.Completed | TransferStates.Aborted; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); + + TryFail(transfer.Id, "File could not be found", TransferStates.Completed | TransferStates.Aborted); + // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); throw; } catch (Exception ex) when (ex is OperationCanceledException || ex is TimeoutException) { - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.State |= ex switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); @@ -450,9 +458,7 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed | TransferStates.Errored; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); @@ -488,7 +494,6 @@ namespace slskd.Transfers.Uploads catch (Exception ex) { Log.Error(ex, "Failed to finalize upload of {Filename} to {Username}: {Message}", transfer.Filename, transfer.Username, ex.Message); - throw; } } } @@ -651,23 +656,9 @@ namespace slskd.Transfers.Uploads */ Log.Error(task.Exception, "Task for upload of {Filename} to {Username} did not complete successfully: {Error}", filename, username, task.Exception.Message); - try - { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - transfer.Exception ??= task.Exception.Message; - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception ex) + if (!TryFail(id, task.Exception)) { - Log.Error(ex, "Failed to clean up transfer {Id} after failed execution: {Message}", id, ex.Message); - throw; + Log.Error(task.Exception, "Failed to clean up transfer {Id} after failed execution: {Message}", id, task.Exception.Message); } }); @@ -677,22 +668,9 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Failed to enqueue upload of {Filename} to {Username}: {Message}", filename, username, ex.Message); - try + if (!TryFail(id, exception: ex)) { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - transfer.Exception ??= ex.Message; - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception innerEx) - { - Log.Error(innerEx, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, innerEx.Message); + Log.Error(ex, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, ex.Message); throw; } @@ -899,6 +877,31 @@ namespace slskd.Transfers.Uploads } /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + public bool TryFail(Guid id, Exception exception) + { + var state = exception switch + { + NotFoundException => TransferStates.Aborted, + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + + return TryFail(id, exception.Message, state); + } + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -910,6 +913,35 @@ namespace slskd.Transfers.Uploads context.SaveChanges(); } + private bool TryFail(Guid id, string exception, TransferStates state) + { + var t = Find(t => t.Id == id); + + if (t is null) + { + return false; + } + + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception; + + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | state; + } + + try + { + Update(t); + return true; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } + } + /// <summary> /// Resolves a remote file's location and size from shares and potentially disk. /// </summary> |
