From cf5a29422198e4defc3820edf8b160f9bcd00857 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Sep 2023 22:54:16 -0500 Subject: add retention options --- src/slskd/Core/Options.cs | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 2ee21733..b2c85146 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -253,6 +253,12 @@ namespace slskd [Validate] public WebOptions Web { get; init; } = new WebOptions(); + /// + /// Gets retention options. + /// + [Validate] + public RetentionOptions Retention { get; init; } = new RetentionOptions(); + /// /// Gets logger options. /// @@ -1048,6 +1054,48 @@ namespace slskd public string Loki { get; init; } = null; } + /// + /// Retention options. + /// + public class RetentionOptions + { + /// + /// Gets upload retention options. + /// + [Validate] + public TransferRetentionOptions Upload { get; init; } = new TransferRetentionOptions(); + + /// + /// Gets download retention options. + /// + [Validate] + public TransferRetentionOptions Download { get; init; } = new TransferRetentionOptions(); + + /// + /// Transfer retention options. + /// + public class TransferRetentionOptions + { + /// + /// Gets the time to retain successful transfers, in minutes. + /// + [Range(60, maximum: int.MaxValue)] + public int Succeeded { get; init; } = int.MaxValue; + + /// + /// Gets the time to retain errored transfers, in minutes. + /// + [Range(60, maximum: int.MaxValue)] + public int Errored { get; init; } = int.MaxValue; + + /// + /// Gets the time to retain cancelled transfers, in minutes. + /// + [Range(60, maximum: int.MaxValue)] + public int Cancelled { get; init; } = int.MaxValue; + } + } + /// /// Metrics options. /// -- cgit v1.2.3 From 5966f7fe0245110591cbf5634efcb301712763a8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Sep 2023 22:56:48 -0500 Subject: specify retention in hours --- src/slskd/Core/Options.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index b2c85146..7e21865f 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1077,21 +1077,21 @@ namespace slskd public class TransferRetentionOptions { /// - /// Gets the time to retain successful transfers, in minutes. + /// Gets the time to retain successful transfers, in hours. /// - [Range(60, maximum: int.MaxValue)] + [Range(1, maximum: int.MaxValue)] public int Succeeded { get; init; } = int.MaxValue; /// - /// Gets the time to retain errored transfers, in minutes. + /// Gets the time to retain errored transfers, in hours. /// - [Range(60, maximum: int.MaxValue)] + [Range(1, maximum: int.MaxValue)] public int Errored { get; init; } = int.MaxValue; /// - /// Gets the time to retain cancelled transfers, in minutes. + /// Gets the time to retain cancelled transfers, in hours. /// - [Range(60, maximum: int.MaxValue)] + [Range(1, maximum: int.MaxValue)] public int Cancelled { get; init; } = int.MaxValue; } } -- cgit v1.2.3 From 4e963e5aa70259f31d20b270e94d7bb6f897cc12 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Sep 2023 22:59:15 -0500 Subject: add EveryFiveMinutes and EveryHour clock events --- src/slskd/Core/Clock.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 4d2e6c10..4e7b3866 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -28,6 +28,8 @@ namespace slskd static Clock() { EveryMinuteTimer.Elapsed += (_, _) => EveryMinute?.Invoke(null, EventArgs.Empty); + EveryFiveMinutesTimer.Elapsed += (_, _) => EveryFiveMinutes?.Invoke(null, EventArgs.Empty); + EveryHourTimer.Elapsed += (_, _) => EveryHour?.Invoke(null, EventArgs.Empty); } /// @@ -35,6 +37,20 @@ namespace slskd /// public static event EventHandler EveryMinute; - private static Timer EveryMinuteTimer { get; } = new Timer() { AutoReset = true, Interval = 60000, Enabled = true }; + /// + /// Fires every 5 minutes. + /// + public static event EventHandler EveryFiveMinutes; + + /// + /// Fires every 60 minutes. + /// + public static event EventHandler EveryHour; + + private static Timer EveryMinuteTimer { get; } = CreateTimer(interval: 1000 * 60); + private static Timer EveryFiveMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 5); + private static Timer EveryHourTimer { get; } = CreateTimer(interval: 1000 * 60 * 60); + + private static Timer CreateTimer(double interval) => new() { AutoReset = true, Interval = interval, Enabled = true }; } } \ No newline at end of file -- cgit v1.2.3 From 132de2917d3d43b902387f44ef7baf8f49637fe0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Sep 2023 23:00:16 -0500 Subject: stub handlers for new clock events --- src/slskd/Application.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index d15d2025..2720375f 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -179,6 +179,8 @@ namespace slskd ConnectionWatchdog = connectionWatchdog; Clock.EveryMinute += Clock_EveryMinute; + Clock.EveryFiveMinutes += Clock_EveryFiveMinutes; + Clock.EveryHour += Clock_EveryHour; } /// @@ -722,6 +724,16 @@ namespace slskd Metrics.DistributedNetwork.CurrentBroadcastLatency.Set(Client.DistributedNetwork.AverageBroadcastLatency ?? 0); } + private void Clock_EveryFiveMinutes(object sender, EventArgs e) + { + + } + + private void Clock_EveryHour(object sender, EventArgs e) + { + + } + private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { // no-op. this is really verbose, use for troubleshooting. -- cgit v1.2.3 From 3920f14a70107f67831d09934861badbc1fc70cf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:01:36 -0500 Subject: consolidate I/UploadService --- src/slskd/Transfers/Uploads/IUploadService.cs | 90 --------------------------- src/slskd/Transfers/Uploads/UploadService.cs | 74 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 90 deletions(-) delete mode 100644 src/slskd/Transfers/Uploads/IUploadService.cs (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/IUploadService.cs b/src/slskd/Transfers/Uploads/IUploadService.cs deleted file mode 100644 index 8fa0b194..00000000 --- a/src/slskd/Transfers/Uploads/IUploadService.cs +++ /dev/null @@ -1,90 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Transfers.Uploads -{ - using System; - using System.Collections.Generic; - using System.Linq.Expressions; - using System.Threading.Tasks; - - /// - /// Manages uploads. - /// - public interface IUploadService - { - /// - /// Gets the upload governor. - /// - IUploadGovernor Governor { get; } - - /// - /// Gets the upload queue. - /// - IUploadQueue Queue { get; } - - /// - /// Adds the specified . Supersedes any existing record for the same file and username. - /// - /// This should generally not be called; use instead. - /// - void AddOrSupersede(Transfer transfer); - - /// - /// Enqueues the requested file. - /// - /// The username of the requesting user. - /// The local filename of the requested file. - /// The operation context. - Task EnqueueAsync(string username, string filename); - - /// - /// Finds a single upload matching the specified . - /// - /// The expression to use to match uploads. - /// The found transfer, or default if not found. - Transfer Find(Expression> expression); - - /// - /// Returns a list of all uploads matching the optional . - /// - /// An optional expression used to match uploads. - /// Optionally include uploads that have been removed previously. - /// The list of uploads matching the specified expression, or all uploads if no expression is specified. - List List(Expression> expression = null, bool includeRemoved = false); - - /// - /// Removes the upload matching the specified . - /// - /// This is a soft delete; the record is retained for historical retrieval. - /// The unique identifier of the upload. - void Remove(Guid id); - - /// - /// Cancels the upload matching the specified , if it is in progress. - /// - /// The unique identifier for the upload. - /// A value indicating whether the upload was successfully cancelled. - bool TryCancel(Guid id); - - /// - /// Synchronously updates the specified . - /// - /// The transfer to update. - void Update(Transfer transfer); - } -} \ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index cfbeeb8b..b9f74dac 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -34,6 +34,80 @@ namespace slskd.Transfers.Uploads using slskd.Shares; using slskd.Users; + /// + /// Manages uploads. + /// + public interface IUploadService + { + /// + /// Gets the upload governor. + /// + IUploadGovernor Governor { get; } + + /// + /// Gets the upload queue. + /// + IUploadQueue Queue { get; } + + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use instead. + /// + void AddOrSupersede(Transfer transfer); + + /// + /// Enqueues the requested file. + /// + /// The username of the requesting user. + /// The local filename of the requested file. + /// The operation context. + Task EnqueueAsync(string username, string filename); + + /// + /// Finds a single upload matching the specified . + /// + /// The expression to use to match uploads. + /// The found transfer, or default if not found. + Transfer Find(Expression> expression); + + /// + /// Returns a list of all uploads matching the optional . + /// + /// An optional expression used to match uploads. + /// Optionally include uploads that have been removed previously. + /// The list of uploads matching the specified expression, or all uploads if no expression is specified. + List List(Expression> expression = null, bool includeRemoved = false); + + /// + /// Removes uploads older than the specified . + /// + /// The age after which uploads are eligible for pruning, in hours. + /// An optional, additional state by which uploads are filtered for pruning. + /// The number of pruned uploads. + int Prune(int age, TransferStates state = TransferStates.Completed); + + /// + /// Removes the upload matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The unique identifier of the upload. + void Remove(Guid id); + + /// + /// Cancels the upload matching the specified , if it is in progress. + /// + /// The unique identifier for the upload. + /// A value indicating whether the upload was successfully cancelled. + bool TryCancel(Guid id); + + /// + /// Synchronously updates the specified . + /// + /// The transfer to update. + void Update(Transfer transfer); + } + /// /// Manages uploads. /// -- cgit v1.2.3 From 38b5449d622e8d7a12708a182a7498ad955654a3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:12:11 -0500 Subject: implement upload pruning --- src/slskd/Transfers/Uploads/UploadService.cs | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index b9f74dac..e8393a1a 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -452,6 +452,47 @@ namespace slskd.Transfers.Uploads } } + /// + /// Removes uploads older than the specified . + /// + /// The age after which uploads are eligible for pruning, in hours. + /// An optional, additional state by which uploads are filtered for pruning. + /// The number of pruned uploads. + public int Prune(int age, TransferStates state = TransferStates.Completed) + { + if (!state.HasFlag(TransferStates.Completed)) + { + throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(state)); + } + + try + { + using var context = ContextFactory.CreateDbContext(); + + var expired = context.Transfers + .Where(t => t.Direction == TransferDirection.Upload) + .Where(t => !t.Removed) + .Where(t => t.EndedAt.HasValue && (DateTime.UtcNow - t.EndedAt.Value).Hours >= age) + .Where(t => t.State.HasFlag(state)) + .ToList(); + + foreach (var tx in expired) + { + tx.Removed = true; + } + + var pruned = context.SaveChanges(); + + Log.Debug("Pruned {Count} expired uploads with state {State}", pruned, state); + return pruned; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to prune uploads: {Message}", ex.Message); + throw; + } + } + /// /// Removes the upload matching the specified . /// -- cgit v1.2.3 From 77201c8144bd88cb5a1f9cc4f5a0a939dd0c46e9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:18:10 -0500 Subject: consolidate I/DownloadService --- src/slskd/Transfers/Downloads/DownloadService.cs | 78 +++++++++++++++++++ src/slskd/Transfers/Downloads/IDownloadService.cs | 94 ----------------------- 2 files changed, 78 insertions(+), 94 deletions(-) delete mode 100644 src/slskd/Transfers/Downloads/IDownloadService.cs (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 827a0528..685bcb43 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -33,6 +33,84 @@ namespace slskd.Transfers.Downloads using slskd.Integrations.FTP; using slskd.Relay; + /// + /// Manages downloads. + /// + public interface IDownloadService + { + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use EnqueueAsync() instead. + /// + void AddOrSupersede(Transfer transfer); + + /// + /// Enqueues the requested list of . + /// + /// + /// If one file in the specified collection fails, the rest will continue. An will be + /// thrown after all files are dispositioned if any throws. + /// + /// The username of remote user. + /// The list of files to enqueue. + /// The operation context. + /// Thrown when the username is null or an empty string. + /// Thrown when no files are requested. + /// Thrown when at least one of the requested files throws. + Task EnqueueAsync(string username, IEnumerable<(string Filename, long Size)> files); + + /// + /// Finds a single download matching the specified . + /// + /// The expression to use to match downloads. + /// The found transfer, or default if not found. + Transfer Find(Expression> expression); + + /// + /// Retrieves the place in the remote queue for the download matching the specified . + /// + /// The unique identifier for the download. + /// The retrieved place in queue. + Task GetPlaceInQueueAsync(Guid id); + + /// + /// Returns a list of all downloads matching the optional . + /// + /// An optional expression used to match downloads. + /// Optionally include downloads that have been removed previously. + /// The list of downloads matching the specified expression, or all downloads if no expression is specified. + List List(Expression> expression = null, bool includeRemoved = false); + + /// + /// Removes downloads older than the specified . + /// + /// The age after which downloads are eligible for pruning, in hours. + /// An optional, additional state by which downloads are filtered for pruning. + /// The number of pruned downloads. + int Prune(int age, TransferStates state = TransferStates.Completed); + + /// + /// Removes the download matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The unique identifier of the download. + void Remove(Guid id); + + /// + /// Cancels the download matching the specified , if it is in progress. + /// + /// The unique identifier for the download. + /// A value indicating whether the download was successfully cancelled. + bool TryCancel(Guid id); + + /// + /// Updates the specified . + /// + /// The transfer to update. + void Update(Transfer transfer); + } + /// /// Manages downloads. /// diff --git a/src/slskd/Transfers/Downloads/IDownloadService.cs b/src/slskd/Transfers/Downloads/IDownloadService.cs deleted file mode 100644 index b49d575c..00000000 --- a/src/slskd/Transfers/Downloads/IDownloadService.cs +++ /dev/null @@ -1,94 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Transfers.Downloads -{ - using System; - using System.Collections.Generic; - using System.Linq.Expressions; - using System.Threading.Tasks; - - /// - /// Manages downloads. - /// - public interface IDownloadService - { - /// - /// Adds the specified . Supersedes any existing record for the same file and username. - /// - /// This should generally not be called; use EnqueueAsync() instead. - /// - void AddOrSupersede(Transfer transfer); - - /// - /// Enqueues the requested list of . - /// - /// - /// If one file in the specified collection fails, the rest will continue. An will be - /// thrown after all files are dispositioned if any throws. - /// - /// The username of remote user. - /// The list of files to enqueue. - /// The operation context. - /// Thrown when the username is null or an empty string. - /// Thrown when no files are requested. - /// Thrown when at least one of the requested files throws. - Task EnqueueAsync(string username, IEnumerable<(string Filename, long Size)> files); - - /// - /// Finds a single download matching the specified . - /// - /// The expression to use to match downloads. - /// The found transfer, or default if not found. - Transfer Find(Expression> expression); - - /// - /// Retrieves the place in the remote queue for the download matching the specified . - /// - /// The unique identifier for the download. - /// The retrieved place in queue. - Task GetPlaceInQueueAsync(Guid id); - - /// - /// Returns a list of all downloads matching the optional . - /// - /// An optional expression used to match downloads. - /// Optionally include downloads that have been removed previously. - /// The list of downloads matching the specified expression, or all downloads if no expression is specified. - List List(Expression> expression = null, bool includeRemoved = false); - - /// - /// Removes the download matching the specified . - /// - /// This is a soft delete; the record is retained for historical retrieval. - /// The unique identifier of the download. - void Remove(Guid id); - - /// - /// Cancels the download matching the specified , if it is in progress. - /// - /// The unique identifier for the download. - /// A value indicating whether the download was successfully cancelled. - bool TryCancel(Guid id); - - /// - /// Updates the specified . - /// - /// The transfer to update. - void Update(Transfer transfer); - } -} \ No newline at end of file -- cgit v1.2.3 From ad3008c64a3f8c34632f570733ba133d12d2ae63 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:18:37 -0500 Subject: implement download pruning --- src/slskd/Transfers/Downloads/DownloadService.cs | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 685bcb43..fc18b506 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -476,6 +476,47 @@ namespace slskd.Transfers.Downloads } } + /// + /// Removes downloads older than the specified . + /// + /// The age after which downloads are eligible for pruning, in hours. + /// An optional, additional state by which downloads are filtered for pruning. + /// The number of pruned downloads. + public int Prune(int age, TransferStates state = TransferStates.Completed) + { + if (!state.HasFlag(TransferStates.Completed)) + { + throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(state)); + } + + try + { + using var context = ContextFactory.CreateDbContext(); + + var expired = context.Transfers + .Where(t => t.Direction == TransferDirection.Download) + .Where(t => !t.Removed) + .Where(t => t.EndedAt.HasValue && (DateTime.UtcNow - t.EndedAt.Value).Hours >= age) + .Where(t => t.State.HasFlag(state)) + .ToList(); + + foreach (var tx in expired) + { + tx.Removed = true; + } + + var pruned = context.SaveChanges(); + + Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, state); + return pruned; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to prune downloads: {Message}", ex.Message); + throw; + } + } + /// /// Removes the download matching the specified . /// -- cgit v1.2.3 From 5e79b99824ef85832ee9399b149c46c3b35d8411 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:31:20 -0500 Subject: add another clock event --- src/slskd/Core/Clock.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 4e7b3866..332308d7 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -29,11 +29,12 @@ namespace slskd { EveryMinuteTimer.Elapsed += (_, _) => EveryMinute?.Invoke(null, EventArgs.Empty); EveryFiveMinutesTimer.Elapsed += (_, _) => EveryFiveMinutes?.Invoke(null, EventArgs.Empty); + EveryThirtyMinutesTimer.Elapsed += (_, _) => EveryThirtyMinutes?.Invoke(null, EventArgs.Empty); EveryHourTimer.Elapsed += (_, _) => EveryHour?.Invoke(null, EventArgs.Empty); } /// - /// Fires every 60 seconds. + /// Fires every minute. /// public static event EventHandler EveryMinute; @@ -43,12 +44,18 @@ namespace slskd public static event EventHandler EveryFiveMinutes; /// - /// Fires every 60 minutes. + /// Fires every 30 minutes. + /// + public static event EventHandler EveryThirtyMinutes; + + /// + /// Fires every hour. /// public static event EventHandler EveryHour; private static Timer EveryMinuteTimer { get; } = CreateTimer(interval: 1000 * 60); private static Timer EveryFiveMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 5); + private static Timer EveryThirtyMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 30); private static Timer EveryHourTimer { get; } = CreateTimer(interval: 1000 * 60 * 60); private static Timer CreateTimer(double interval) => new() { AutoReset = true, Interval = interval, Enabled = true }; -- cgit v1.2.3 From 98536d94108044b709658895782ba25d5693825b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:40:58 -0500 Subject: make retention options nullable, and in minutes --- config/slskd.example.yml | 12 ++++++------ src/slskd/Core/Options.cs | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 5b7ea7ae..2c22d020 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -109,13 +109,13 @@ # cidr: 0.0.0.0/0,::/0 # retention: # upload: -# succeeded: 2147483647 -# errored: 2147483647 -# cancelled: 2147483647 +# succeeded: 1440 # 1 day +# errored: 30 +# cancelled: 5 # download: -# succeeded: 2147483647 -# errored: 2147483647 -# cancelled: 2147483647 +# succeeded: 1440 # 1 day +# errored: 20160 # 2 weeks +# cancelled: 5 # logger: # loki: ~ # metrics: diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 7e21865f..30297cac 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1077,22 +1077,22 @@ namespace slskd public class TransferRetentionOptions { /// - /// Gets the time to retain successful transfers, in hours. + /// Gets the time to retain successful transfers, in minutes. /// - [Range(1, maximum: int.MaxValue)] - public int Succeeded { get; init; } = int.MaxValue; + [Range(5, maximum: int.MaxValue)] + public int? Succeeded { get; init; } = null; /// - /// Gets the time to retain errored transfers, in hours. + /// Gets the time to retain errored transfers, in minutes. /// - [Range(1, maximum: int.MaxValue)] - public int Errored { get; init; } = int.MaxValue; + [Range(5, maximum: int.MaxValue)] + public int? Errored { get; init; } = null; /// - /// Gets the time to retain cancelled transfers, in hours. + /// Gets the time to retain cancelled transfers, in minutes. /// - [Range(1, maximum: int.MaxValue)] - public int Cancelled { get; init; } = int.MaxValue; + [Range(5, maximum: int.MaxValue)] + public int? Cancelled { get; init; } = null; } } -- cgit v1.2.3 From fe96fc98ef6f618f362da57114117fd59cb11508 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 16:46:17 -0500 Subject: update implementation --- src/slskd/Application.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 2720375f..7002da24 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -180,6 +180,7 @@ namespace slskd Clock.EveryMinute += Clock_EveryMinute; Clock.EveryFiveMinutes += Clock_EveryFiveMinutes; + Clock.EveryThirtyMinutes += Clock_EveryThirtyMinutes; Clock.EveryHour += Clock_EveryHour; } @@ -726,12 +727,51 @@ namespace slskd private void Clock_EveryFiveMinutes(object sender, EventArgs e) { + PruneTransfers(); + } + private void Clock_EveryThirtyMinutes(object sender, EventArgs e) + { } private void Clock_EveryHour(object sender, EventArgs e) { + } + + private void PruneTransfers() + { + var options = OptionsMonitor.CurrentValue.Retention; + void PruneUpload(int? age, TransferStates state) + { + if (age.HasValue) + { + Transfers.Uploads.Prune(age.Value, TransferStates.Completed | state); + } + } + + void PruneDownload(int? age, TransferStates state) + { + if (age.HasValue) + { + Transfers.Downloads.Prune(age.Value, TransferStates.Completed | state); + } + } + + try + { + PruneUpload(options.Upload.Succeeded, TransferStates.Succeeded); + PruneUpload(options.Upload.Cancelled, TransferStates.Cancelled); + PruneUpload(options.Upload.Errored, TransferStates.Errored); + + PruneDownload(options.Download.Succeeded, TransferStates.Succeeded); + PruneDownload(options.Download.Cancelled, TransferStates.Cancelled); + PruneDownload(options.Download.Errored, TransferStates.Errored); + } + catch + { + Log.Error("Encountered one or more errors while pruning transfers"); + } } private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) -- cgit v1.2.3 From 467d862f158ff4730afac572f550aa8430464bfc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 22:10:31 -0500 Subject: fix enum comparison --- src/slskd/Transfers/Downloads/DownloadService.cs | 6 ++++-- src/slskd/Transfers/Uploads/UploadService.cs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index fc18b506..932c97ee 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -493,11 +493,13 @@ namespace slskd.Transfers.Downloads { using var context = ContextFactory.CreateDbContext(); + var cutoffDateTime = DateTime.UtcNow.AddMinutes(-age); + var expired = context.Transfers .Where(t => t.Direction == TransferDirection.Download) .Where(t => !t.Removed) - .Where(t => t.EndedAt.HasValue && (DateTime.UtcNow - t.EndedAt.Value).Hours >= age) - .Where(t => t.State.HasFlag(state)) + .Where(t => t.EndedAt.HasValue && t.EndedAt.Value < cutoffDateTime) + .Where(t => t.State == state) // https://github.com/dotnet/efcore/issues/20094 .ToList(); foreach (var tx in expired) diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index e8393a1a..efb08233 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -469,11 +469,13 @@ namespace slskd.Transfers.Uploads { using var context = ContextFactory.CreateDbContext(); + var cutoffDateTime = DateTime.UtcNow.AddMinutes(-age); + var expired = context.Transfers .Where(t => t.Direction == TransferDirection.Upload) .Where(t => !t.Removed) - .Where(t => t.EndedAt.HasValue && (DateTime.UtcNow - t.EndedAt.Value).Hours >= age) - .Where(t => t.State.HasFlag(state)) + .Where(t => t.EndedAt.HasValue && t.EndedAt.Value < cutoffDateTime) + .Where(t => t.State == state) // https://github.com/dotnet/efcore/issues/20094 .ToList(); foreach (var tx in expired) -- cgit v1.2.3 From da7f4cef11e415caa8fe4d27c3a33c9ce418ea05 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Sep 2023 22:16:22 -0500 Subject: only log if at least one transfer is pruned --- src/slskd/Transfers/Downloads/DownloadService.cs | 6 +++++- src/slskd/Transfers/Uploads/UploadService.cs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 932c97ee..25133629 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -509,7 +509,11 @@ namespace slskd.Transfers.Downloads var pruned = context.SaveChanges(); - Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, state); + if (pruned > 0) + { + Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, state); + } + return pruned; } catch (Exception ex) diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index efb08233..04e6a431 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -485,7 +485,11 @@ namespace slskd.Transfers.Uploads var pruned = context.SaveChanges(); - Log.Debug("Pruned {Count} expired uploads with state {State}", pruned, state); + if (pruned > 0) + { + Log.Debug("Pruned {Count} expired uploads with state {State}", pruned, state); + } + return pruned; } catch (Exception ex) -- cgit v1.2.3