diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-28 21:49:32 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-28 21:49:32 -0500 |
| commit | bb08368d27bee40f16e847ed95e87e602238172c (patch) | |
| tree | 49b54928d16bdd447e16d595011f383f81186e8a /src/slskd/Integrations/FTP | |
| parent | 750ce2d4672ec387523217eb2b5caab85873e0c3 (diff) | |
| download | slskd-bb08368d27bee40f16e847ed95e87e602238172c.tar.gz | |
implement retry, clean up a bit
Diffstat (limited to 'src/slskd/Integrations/FTP')
| -rw-r--r-- | src/slskd/Integrations/FTP/FTPClient.cs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/src/slskd/Integrations/FTP/FTPClient.cs b/src/slskd/Integrations/FTP/FTPClient.cs index e6a748a9..105863ea 100644 --- a/src/slskd/Integrations/FTP/FTPClient.cs +++ b/src/slskd/Integrations/FTP/FTPClient.cs @@ -1,4 +1,4 @@ -// <copyright file="FTPService.cs" company="slskd Team"> +// <copyright file="FTPClient.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ namespace slskd.Integrations.FTP { using System; using System.IO; + using System.Threading; using System.Threading.Tasks; using FluentFTP; using Microsoft.Extensions.Logging; @@ -70,36 +71,41 @@ namespace slskd.Integrations.FTP return; } - // todo: add retries up to configured count with exponential backoff - try - { - var fileOnly = Path.GetFileName(filename); - var fileAndParentDirectory = Path.Combine(Path.GetDirectoryName(filename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(filename)), string.Empty), fileOnly).TrimStart('/').TrimStart('\\'); + var fileOnly = Path.GetFileName(filename); + var fileAndParentDirectory = Path.Combine(Path.GetDirectoryName(filename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(filename)), string.Empty), fileOnly).TrimStart('/').TrimStart('\\'); - var remotePath = FTPOptions.RemotePath.TrimEnd('/').TrimEnd('\\'); - // todo: sanitize filename - var remoteFilename = $"{remotePath}/{fileAndParentDirectory}"; + var remotePath = FTPOptions.RemotePath.TrimEnd('/').TrimEnd('\\'); + var remoteFilename = $"{remotePath}/{fileAndParentDirectory}"; - var existsMode = FTPOptions.OverwriteExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip; + var existsMode = FTPOptions.OverwriteExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip; - Log.LogInformation("Uploading {Filename} to FTP {Address}:{Port} as {RemoteFilename}", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port, remoteFilename); - var client = Factory.CreateFtpClient(); + try + { + await Retry.Do(async () => + { + Log.LogInformation("Uploading {Filename} to FTP {Address}:{Port} as {RemoteFilename}", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port, remoteFilename); + var client = Factory.CreateFtpClient(); - // todo: add cancellation token for timeout - await client.ConnectAsync(); + var cts = new CancellationTokenSource(FTPOptions.ConnectionTimeout); + await client.ConnectAsync(cts.Token); - var status = await client.UploadFileAsync(filename, remoteFilename, existsMode, createRemoteDir: true); + var status = await client.UploadFileAsync(filename, remoteFilename, existsMode, createRemoteDir: true); - if (status == FtpStatus.Failed) - { - throw new FtpException("FTP client reported a failed transfer"); - } + if (status == FtpStatus.Failed) + { + throw new FtpException("FTP client reported a failed transfer"); + } - Log.LogInformation("FTP upload of {Filename} to {Address}:{Port} complete", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port); + Log.LogInformation("FTP upload of {Filename} to {Address}:{Port} complete", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port); + }, (attempts, ex) => + { + Log.LogWarning("Failed attempt {Attempts} to upload {Filename} to FTP: {Message}", attempts, ex.Message); + return true; + }, maxAttempts: FTPOptions.RetryAttempts, maxDelayInMilliseconds: 30000); } catch (Exception ex) { - Log.LogWarning(ex, "Failed to upload {Filename} to FTP: {Message}", ex.Message); + Log.LogWarning(ex, "Failed to upload {Filename} to FTP after {Attempts}: {Message}", FTPOptions.RetryAttempts, ex.Message); throw; } } |
