diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-10-15 14:27:13 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-15 14:27:13 -0500 |
| commit | 5986bcadc179e3e9a34152687b80210dcc65e366 (patch) | |
| tree | 69c89224d513f513793ac415664b4b584c383a82 /src/slskd/Application.cs | |
| parent | 12e89983470a55e03f30f35002135b921183c72b (diff) | |
| parent | 4670f2a708b48d3bb73021a3954b8ca76bf56aa5 (diff) | |
| download | slskd-5986bcadc179e3e9a34152687b80210dcc65e366.tar.gz | |
Merge pull request #978 from slskd/file-retention
Add automated removal of downloaded and incomplete files, and application logs
Diffstat (limited to 'src/slskd/Application.cs')
| -rw-r--r-- | src/slskd/Application.cs | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 7002da24..6d6ccd37 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"); + 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 // 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 @@ -442,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"); @@ -727,17 +734,71 @@ 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) { } + private void PruneFiles() + { + void PruneDirectory(int? age, string directory) + { + try + { + if (!age.HasValue) + { + return; + } + + 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); + } + catch (Exception ex) + { + Log.Error("Failed to prune files in directory {Directory}: {Message}", directory, ex.Message); + } + } + + PruneDirectory(age: Options.Retention.Files.Incomplete, directory: Options.Directories.Incomplete); + PruneDirectory(age: Options.Retention.Files.Complete, directory: Options.Directories.Downloads); + } + private void PruneTransfers() { var options = OptionsMonitor.CurrentValue.Retention; |
