From fb5c44eed29826a82a009b6418c6eadc775533c1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Sep 2023 13:27:38 -0500 Subject: add file retention options to example --- config/slskd.example.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 2c22d020..4b7670f0 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -116,6 +116,9 @@ # succeeded: 1440 # 1 day # errored: 20160 # 2 weeks # cancelled: 5 +# file: +# complete: 20160 # 2 weeks +# incomplete: 10080 # 1 week# # logger: # loki: ~ # metrics: -- cgit v1.2.3 From bc9373054f1d1ce75f65961890021cf8547da66b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Sep 2023 13:28:00 -0500 Subject: add file retention options --- src/slskd/Core/Options.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 30297cac..0dd3c045 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1059,6 +1059,12 @@ namespace slskd /// public class RetentionOptions { + /// + /// Gets file retention options. + /// + [Validate] + public FileRetentionOptions File { get; init; } = new FileRetentionOptions(); + /// /// Gets upload retention options. /// @@ -1071,6 +1077,22 @@ namespace slskd [Validate] public TransferRetentionOptions Download { get; init; } = new TransferRetentionOptions(); + /// + /// File retention options. + /// + public class FileRetentionOptions + { + /// + /// Gets the time to retain completed files, in minutes. + /// + public int? Complete { get; init; } = null; + + /// + /// Gets the time to retain incomplete files, in minutes. + /// + public int? Incomplete { get; init; } = null; + } + /// /// Transfer retention options. /// -- cgit v1.2.3 From 6dff5f95f7b337d571be954088d47b0ac31bc535 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Sep 2023 13:43:09 -0500 Subject: add option for log retention --- config/slskd.example.yml | 3 ++- src/slskd/Core/Options.cs | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 4b7670f0..f995f496 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -118,7 +118,8 @@ # cancelled: 5 # file: # complete: 20160 # 2 weeks -# incomplete: 10080 # 1 week# +# incomplete: 10080 # 1 week +# logs: 259200 # 180 days # logger: # loki: ~ # metrics: diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 0dd3c045..428ef06b 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1077,6 +1077,11 @@ namespace slskd [Validate] public TransferRetentionOptions Download { get; init; } = new TransferRetentionOptions(); + /// + /// Gets log retention options. + /// + public int? Logs { get; init; } = 259200; // 180 days + /// /// File retention options. /// -- cgit v1.2.3 From 051cb671df78190dac9a80d3d2b3f51d86d3a938 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Sep 2023 13:44:51 -0500 Subject: stub file and log pruning --- src/slskd/Application.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 7002da24..d721a1d1 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -727,15 +727,27 @@ namespace slskd private void Clock_EveryFiveMinutes(object sender, EventArgs e) { - PruneTransfers(); + _ = Task.Run(() => PruneTransfers()); } private void Clock_EveryThirtyMinutes(object sender, EventArgs e) { + _ = Task.Run(() => PruneFiles()); } private void Clock_EveryHour(object sender, EventArgs e) { + _ = Task.Run(() => PruneLogs()); + } + + private void PruneFiles() + { + + } + + private void PruneLogs() + { + } private void PruneTransfers() -- cgit v1.2.3 From 0e95d22594456a36f2c53d47505553e034b1f047 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Sep 2023 13:47:15 -0500 Subject: add todos --- src/slskd/Application.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index d721a1d1..e2ae9bd6 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -742,12 +742,13 @@ namespace slskd private void PruneFiles() { - + // todo: incomplete + // todo: complete } private void PruneLogs() { - + // todo: delete files matching pattern slskd-20230216.log } private void PruneTransfers() -- cgit v1.2.3 From 93ee09d4c6952f691ccfe13c6aa4996d9015e661 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Sep 2023 16:49:08 -0500 Subject: fire all events at startup --- src/slskd/Core/Clock.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 332308d7..2e18598d 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -18,6 +18,7 @@ namespace slskd { using System; + using System.Threading.Tasks; using System.Timers; /// @@ -27,10 +28,15 @@ namespace slskd { static Clock() { - 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); + EveryMinuteTimer.Elapsed += (_, _) => EveryMinute?.Fire(); + EveryFiveMinutesTimer.Elapsed += (_, _) => EveryFiveMinutes?.Fire(); + EveryThirtyMinutesTimer.Elapsed += (_, _) => EveryThirtyMinutes?.Fire(); + EveryHourTimer.Elapsed += (_, _) => EveryHour?.Fire(); + + _ = Task.Run(() => EveryMinute?.Fire()); + _ = Task.Run(() => EveryFiveMinutes?.Fire()); + _ = Task.Run(() => EveryThirtyMinutes?.Fire()); + _ = Task.Run(() => EveryHour?.Fire()); } /// @@ -53,11 +59,18 @@ namespace slskd /// public static event EventHandler EveryHour; + /// + /// Invokes the EventHandler with a null sender and . + /// + /// The EventHandler to invoke. + public static void Fire(this EventHandler e) => e?.Invoke(null, EventArgs.Empty); + 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 }; + } } \ No newline at end of file -- cgit v1.2.3 From 7e51e0a314240f6966177c397caab8158e1da011 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Sep 2023 16:52:55 -0500 Subject: explicitly start clock --- src/slskd/Application.cs | 4 ++++ src/slskd/Core/Clock.cs | 34 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index e2ae9bd6..980b274b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -285,6 +285,10 @@ namespace slskd { Log.Information("Application started"); + Log.Debug("Starting clock"); + Clock.Start(); + Log.Debug("Clock started"); + // if the application shut down "uncleanly", transfers may need to be cleaned up. we deliberately don't allow these // records to be updated if the application has started to shut down so that we can do this cleanup and properly // disposition them as having failed due to an application shutdown, instead of some random exception thrown while diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 2e18598d..796c36ed 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -28,15 +28,10 @@ namespace slskd { static Clock() { - EveryMinuteTimer.Elapsed += (_, _) => EveryMinute?.Fire(); - EveryFiveMinutesTimer.Elapsed += (_, _) => EveryFiveMinutes?.Fire(); - EveryThirtyMinutesTimer.Elapsed += (_, _) => EveryThirtyMinutes?.Fire(); - EveryHourTimer.Elapsed += (_, _) => EveryHour?.Fire(); - - _ = Task.Run(() => EveryMinute?.Fire()); - _ = Task.Run(() => EveryFiveMinutes?.Fire()); - _ = Task.Run(() => EveryThirtyMinutes?.Fire()); - _ = Task.Run(() => EveryHour?.Fire()); + EveryMinuteTimer.Elapsed += (_, _) => Fire(EveryMinute); + EveryFiveMinutesTimer.Elapsed += (_, _) => Fire(EveryFiveMinutes); + EveryThirtyMinutesTimer.Elapsed += (_, _) => Fire(EveryThirtyMinutes); + EveryHourTimer.Elapsed += (_, _) => Fire(EveryHour); } /// @@ -59,18 +54,25 @@ namespace slskd /// public static event EventHandler EveryHour; - /// - /// Invokes the EventHandler with a null sender and . - /// - /// The EventHandler to invoke. - public static void Fire(this EventHandler e) => e?.Invoke(null, EventArgs.Empty); + public static void Start() + { + EveryMinuteTimer.Enabled = true; + EveryFiveMinutesTimer.Enabled = true; + EveryThirtyMinutesTimer.Enabled = true; + EveryHourTimer.Enabled = true; + + _ = Task.Run(() => Fire(EveryMinute)); + _ = Task.Run(() => Fire(EveryFiveMinutes)); + _ = Task.Run(() => Fire(EveryThirtyMinutes)); + _ = Task.Run(() => Fire(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 }; - + private static Timer CreateTimer(double interval) => new() { AutoReset = true, Interval = interval, Enabled = false }; + private static void Fire(EventHandler e) => e?.Invoke(null, EventArgs.Empty); } } \ No newline at end of file -- cgit v1.2.3 From 6886d768afdf2ed36b571e7810132a1df6ebb01b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 16:57:43 -0500 Subject: wait for clock startup --- src/slskd/Application.cs | 2 +- src/slskd/Core/Clock.cs | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 980b274b..dabf112e 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -286,7 +286,7 @@ namespace slskd Log.Information("Application started"); Log.Debug("Starting clock"); - Clock.Start(); + await Clock.StartAsync(); Log.Debug("Clock started"); // if the application shut down "uncleanly", transfers may need to be cleaned up. we deliberately don't allow these diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 796c36ed..4ee29ee3 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -54,17 +54,22 @@ namespace slskd /// public static event EventHandler EveryHour; - public static void Start() + /// + /// Starts the clock. + /// + /// A Task that completes when all startup events have finished processing. + public static Task StartAsync() { EveryMinuteTimer.Enabled = true; EveryFiveMinutesTimer.Enabled = true; EveryThirtyMinutesTimer.Enabled = true; EveryHourTimer.Enabled = true; - _ = Task.Run(() => Fire(EveryMinute)); - _ = Task.Run(() => Fire(EveryFiveMinutes)); - _ = Task.Run(() => Fire(EveryThirtyMinutes)); - _ = Task.Run(() => Fire(EveryHour)); + return Task.WhenAll( + Task.Run(() => Fire(EveryMinute)), + Task.Run(() => Fire(EveryFiveMinutes)), + Task.Run(() => Fire(EveryThirtyMinutes)), + Task.Run(() => Fire(EveryHour))); } private static Timer EveryMinuteTimer { get; } = CreateTimer(interval: 1000 * 60); -- cgit v1.2.3 From 26e48163ca53b61a82c4a5fd5a40228096aae12c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 17:00:01 -0500 Subject: stop the clock on shutdown --- src/slskd/Application.cs | 3 +++ src/slskd/Core/Clock.cs | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index dabf112e..0ebffe94 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -446,6 +446,9 @@ namespace slskd { ShuttingDown = true; Log.Warning("Application is shutting down"); + + Clock.Stop(); + Client.Disconnect("Shutting down", new ApplicationShutdownException("Shutting down")); Client.Dispose(); Log.Information("Client stopped"); diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 4ee29ee3..31519fc8 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -72,6 +72,17 @@ namespace slskd Task.Run(() => Fire(EveryHour))); } + /// + /// Stops the clock. + /// + public static void Stop() + { + EveryMinuteTimer.Stop(); + EveryFiveMinutesTimer.Stop(); + EveryThirtyMinutesTimer.Stop(); + EveryHourTimer.Stop(); + } + 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); -- cgit v1.2.3 From 86b85add618f336e737fe172ccba789a534b4b41 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 17:07:08 -0500 Subject: remove nullability for log retention --- src/slskd/Core/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 428ef06b..596019ae 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1080,7 +1080,7 @@ namespace slskd /// /// Gets log retention options. /// - public int? Logs { get; init; } = 259200; // 180 days + public int Logs { get; init; } = 259200; // 180 days /// /// File retention options. -- cgit v1.2.3 From dc6b79df40ed8f274e6e248c6a27ceda3c5168b2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 17:07:24 -0500 Subject: configure serilog to respect log retention value --- src/slskd/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 418a29c2..40a39a62 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -952,7 +952,8 @@ namespace slskd config.File( Path.Combine(AppDirectory, "logs", $"{AppName}-.log"), outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", - rollingInterval: RollingInterval.Day)) + rollingInterval: RollingInterval.Day, + retainedFileTimeLimit: TimeSpan.FromMinutes(OptionsAtStartup.Retention.Logs))) .WriteTo.Conditional( e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki), config => config.GrafanaLoki( -- cgit v1.2.3 From 988191364cb96b6f83337e5c855652067ba0a8cd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 17:09:45 -0500 Subject: log retention set in days --- src/slskd/Core/Options.cs | 5 +++-- src/slskd/Program.cs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 596019ae..080f1b0d 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1078,9 +1078,10 @@ namespace slskd public TransferRetentionOptions Download { get; init; } = new TransferRetentionOptions(); /// - /// Gets log retention options. + /// Gets the time to retain logs, in days. /// - public int Logs { get; init; } = 259200; // 180 days + [Range(1, maximum: int.MaxValue)] + public int Logs { get; init; } = 180; /// /// File retention options. diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 40a39a62..7a832ddb 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -953,7 +953,7 @@ namespace slskd Path.Combine(AppDirectory, "logs", $"{AppName}-.log"), outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, - retainedFileTimeLimit: TimeSpan.FromMinutes(OptionsAtStartup.Retention.Logs))) + retainedFileTimeLimit: TimeSpan.FromDays(OptionsAtStartup.Retention.Logs))) .WriteTo.Conditional( e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki), config => config.GrafanaLoki( -- cgit v1.2.3 From 6598b2ad3d7dcb2b814cee6a3f7b38b5365a7522 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 17:11:31 -0500 Subject: add range for file retention options --- src/slskd/Core/Options.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 080f1b0d..eb852015 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1091,11 +1091,13 @@ namespace slskd /// /// Gets the time to retain completed files, in minutes. /// + [Range(30, maximum: int.MaxValue)] public int? Complete { get; init; } = null; /// /// Gets the time to retain incomplete files, in minutes. /// + [Range(30, maximum: int.MaxValue)] public int? Incomplete { get; init; } = null; } -- cgit v1.2.3 From 14cdb89836575741104cfcf73f446233f18c3809 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Oct 2023 22:16:53 -0500 Subject: implement file pruning --- src/slskd/Application.cs | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 0ebffe94..b084e684 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -744,18 +744,52 @@ namespace slskd private void Clock_EveryHour(object sender, EventArgs e) { - _ = Task.Run(() => PruneLogs()); } private void PruneFiles() { - // todo: incomplete - // todo: complete - } + void PruneDirectory(int? age, string directory) + { + if (!age.HasValue) + { + return; + } - private void PruneLogs() - { - // todo: delete files matching pattern slskd-20230216.log + Log.Debug("Pruning files older than {Age} minutes from {Directory}", age, directory); + + var options = new EnumerationOptions + { + IgnoreInaccessible = true, + AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, + RecurseSubdirectories = true, + }; + + var files = System.IO.Directory.GetFiles(directory, "*", options) + .Select(filename => new FileInfo(filename)) + .Where(file => file.LastAccessTimeUtc <= DateTime.UtcNow.AddMinutes(-age.Value)); + + Log.Debug("Found {Count} files of need of pruning", files.Count()); + + int errors = 0; + + foreach (var file in files) + { + try + { + file.Delete(); + } + catch (Exception ex) + { + errors++; + Log.Warning(ex, "Failed to prune file {File}: {Message}", file, ex.Message); + } + } + + Log.Debug("Pruning complete. Deleted: {Deleted}, Errors: {Errors}", files.Count() - errors, errors); + } + + PruneDirectory(age: Options.Retention.File.Incomplete, directory: Options.Directories.Incomplete); + PruneDirectory(age: Options.Retention.File.Complete, directory: Options.Directories.Downloads); } private void PruneTransfers() -- cgit v1.2.3 From 4db01adefce961f4762880870ad80f0a3939f159 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Oct 2023 14:09:59 -0500 Subject: pluralize files under retention --- config/slskd.example.yml | 2 +- src/slskd/Application.cs | 63 ++++++++++++++++++++++++++--------------------- src/slskd/Core/Options.cs | 2 +- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index f995f496..885d0206 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -116,7 +116,7 @@ # succeeded: 1440 # 1 day # errored: 20160 # 2 weeks # cancelled: 5 -# file: +# files: # complete: 20160 # 2 weeks # incomplete: 10080 # 1 week # logs: 259200 # 180 days diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b084e684..6d6ccd37 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -750,46 +750,53 @@ namespace slskd { void PruneDirectory(int? age, string directory) { - if (!age.HasValue) + try { - return; - } + if (!age.HasValue) + { + return; + } - Log.Debug("Pruning files older than {Age} minutes from {Directory}", age, directory); + Log.Debug("Pruning files older than {Age} minutes from {Directory}", age, directory); - var options = new EnumerationOptions - { - IgnoreInaccessible = true, - AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, - RecurseSubdirectories = true, - }; + var options = new EnumerationOptions + { + IgnoreInaccessible = true, + AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, + RecurseSubdirectories = true, + }; - var files = System.IO.Directory.GetFiles(directory, "*", options) - .Select(filename => new FileInfo(filename)) - .Where(file => file.LastAccessTimeUtc <= DateTime.UtcNow.AddMinutes(-age.Value)); + var files = System.IO.Directory.GetFiles(directory, "*", options) + .Select(filename => new FileInfo(filename)) + .Where(file => file.LastAccessTimeUtc <= DateTime.UtcNow.AddMinutes(-age.Value)); - Log.Debug("Found {Count} files of need of pruning", files.Count()); + Log.Debug("Found {Count} files of need of pruning", files.Count()); - int errors = 0; + int errors = 0; - foreach (var file in files) - { - try + foreach (var file in files) { - file.Delete(); - } - catch (Exception ex) - { - errors++; - Log.Warning(ex, "Failed to prune file {File}: {Message}", file, ex.Message); + try + { + file.Delete(); + } + catch (Exception ex) + { + errors++; + Log.Warning(ex, "Failed to prune file {File}: {Message}", file, ex.Message); + } } - } - Log.Debug("Pruning complete. Deleted: {Deleted}, Errors: {Errors}", files.Count() - errors, errors); + Log.Debug("Pruning complete. Deleted: {Deleted}, Errors: {Errors}", files.Count() - errors, errors); + } + catch (Exception ex) + { + Log.Error("Failed to prune files in directory {Directory}: {Message}", directory, ex.Message); + } } - PruneDirectory(age: Options.Retention.File.Incomplete, directory: Options.Directories.Incomplete); - PruneDirectory(age: Options.Retention.File.Complete, directory: Options.Directories.Downloads); + PruneDirectory(age: Options.Retention.Files.Incomplete, directory: Options.Directories.Incomplete); + PruneDirectory(age: Options.Retention.Files.Complete, directory: Options.Directories.Downloads); } private void PruneTransfers() diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index eb852015..9d92bb95 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1063,7 +1063,7 @@ namespace slskd /// Gets file retention options. /// [Validate] - public FileRetentionOptions File { get; init; } = new FileRetentionOptions(); + public FileRetentionOptions Files { get; init; } = new FileRetentionOptions(); /// /// Gets upload retention options. -- cgit v1.2.3 From 4115016aaa7de7873bbc24bcbf6d9e4b54a576a4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Oct 2023 14:11:22 -0500 Subject: extend example retention for incomplete files to 30 days --- config/slskd.example.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 885d0206..daabc838 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -118,7 +118,7 @@ # cancelled: 5 # files: # complete: 20160 # 2 weeks -# incomplete: 10080 # 1 week +# incomplete: 43200 # 30 days # logs: 259200 # 180 days # logger: # loki: ~ -- cgit v1.2.3 From b0f14895166c65709d4150f18d9b3a4c324ff528 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Oct 2023 21:15:40 -0500 Subject: docs for file and log retention --- docs/config.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index b08cd797..502d5c03 100644 --- a/docs/config.md +++ b/docs/config.md @@ -721,7 +721,13 @@ filters: # Data Retention -Retention of transfer records on the UI (and in API endpoints) is, by default, indefinite; completed transfers will remain visible until they are manually removed. Users can optionally configure time-based retention rules for both uploads and downloads, and can specify different settings for different dispositions (succeeded, errored, and cancelled). Transfers are checked for expiration on a 5 minute interval, and the minimum retention time is 5 minutes. +By default, most things created by the application are retained indefinitely; they have to be removed manually by the user. Users can optionally configure certain things to be removed or deleted automatically after a period of time. + +Transfers can be configured to be removed from the UI after they are complete. Users can specify different retention periods for uploads and downloads separately, and by transfer state; succeeded, errored, and cancelled. + +Files (on disk) can be configured to be deleted after the age of their last access time exceeds the configured time. Completed and incomplete files can be configured separately. + +Application logs are removed after 180 days by default, but this can be configured as well. All retention periods are specified in minutes. @@ -736,6 +742,10 @@ retention: succeeded: 1440 # 1 day errored: 20160 # 2 weeks cancelled: 5 + files: + complete: 20160 # 2 weeks + incomplete: 43200 # 30 days + logs: 259200 # 180 days ``` # Integrations -- cgit v1.2.3 From 4670f2a708b48d3bb73021a3954b8ca76bf56aa5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Oct 2023 14:24:04 -0500 Subject: tweak docs --- docs/config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index 502d5c03..2038d98a 100644 --- a/docs/config.md +++ b/docs/config.md @@ -723,7 +723,7 @@ filters: By default, most things created by the application are retained indefinitely; they have to be removed manually by the user. Users can optionally configure certain things to be removed or deleted automatically after a period of time. -Transfers can be configured to be removed from the UI after they are complete. Users can specify different retention periods for uploads and downloads separately, and by transfer state; succeeded, errored, and cancelled. +Transfers can be configured to be removed from the UI after they are complete by specifying retention periods for uploads and downloads separately, and by transfer state; succeeded, errored, and cancelled. Files (on disk) can be configured to be deleted after the age of their last access time exceeds the configured time. Completed and incomplete files can be configured separately. -- cgit v1.2.3