From f6d0b129806c36745f9b91317004c6ba9367e43c Mon Sep 17 00:00:00 2001 From: mxtsdev Date: Wed, 12 Feb 2020 14:58:56 +0100 Subject: [PATCH] Log cleanup (>30 days) on startup and every 24-hours. --- ArkBot/App.config | 1 + ArkBot/ArkBot.csproj | 2 + ArkBot/Services/ILogCleanupService.cs | 10 ++++ ArkBot/Services/LogCleanupService.cs | 77 +++++++++++++++++++++++++++ ArkBot/ViewModel/Workspace.cs | 2 + 5 files changed, 92 insertions(+) create mode 100644 ArkBot/Services/ILogCleanupService.cs create mode 100644 ArkBot/Services/LogCleanupService.cs diff --git a/ArkBot/App.config b/ArkBot/App.config index 1079f06..f9f9521 100644 --- a/ArkBot/App.config +++ b/ArkBot/App.config @@ -132,6 +132,7 @@ + diff --git a/ArkBot/ArkBot.csproj b/ArkBot/ArkBot.csproj index 892bca8..d7ec86c 100644 --- a/ArkBot/ArkBot.csproj +++ b/ArkBot/ArkBot.csproj @@ -666,7 +666,9 @@ + + diff --git a/ArkBot/Services/ILogCleanupService.cs b/ArkBot/Services/ILogCleanupService.cs new file mode 100644 index 0000000..1956608 --- /dev/null +++ b/ArkBot/Services/ILogCleanupService.cs @@ -0,0 +1,10 @@ +using System; +using System.Threading.Tasks; +using Discord; + +namespace ArkBot.Helpers +{ + public interface ILogCleanupService : IDisposable + { + } +} \ No newline at end of file diff --git a/ArkBot/Services/LogCleanupService.cs b/ArkBot/Services/LogCleanupService.cs new file mode 100644 index 0000000..5fdf856 --- /dev/null +++ b/ArkBot/Services/LogCleanupService.cs @@ -0,0 +1,77 @@ +using ArkBot.Helpers; +using QueryMaster.GameServer; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; + +namespace ArkBot.Services +{ + public class LogCleanupService : ILogCleanupService + { + private Timer _timer; + private readonly TimeSpan _delay = TimeSpan.FromHours(24); + + private const string _logDirectoryPath = @"logs\"; + private Regex _rDate = new Regex(@"_(?=\d{4,4})(?.+?)(?:-\d{3,})?\.log", RegexOptions.IgnoreCase | RegexOptions.Singleline); + + + public LogCleanupService() + { + _timer = new Timer(_timer_Callback, null, 0, Timeout.Infinite); + } + + private void _timer_Callback(object state) + { + try + { + _timer.Change(Timeout.Infinite, Timeout.Infinite); + + var t = DateTime.Now.AddDays(-30); + + foreach (var logFileName in Directory.GetFiles(_logDirectoryPath, "*.log", SearchOption.TopDirectoryOnly)) + { + var m = _rDate.Match(logFileName); + var dateStr = m.Success ? m.Groups["date"]?.Value : null; + if (dateStr == null) continue; + + if (!DateTime.TryParseExact(dateStr, @"yyyy-MM-dd.HH.mm.ss.ffff", CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out var dt) || dt > t) continue; + + File.Delete(logFileName); + } + } + finally + { + _timer.Change(_delay, _delay); + } + } + + #region IDisposable Support + private bool disposedValue = false; + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _timer?.Dispose(); + _timer = null; + } + + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(true); + } + #endregion + } +} diff --git a/ArkBot/ViewModel/Workspace.cs b/ArkBot/ViewModel/Workspace.cs index cdc3d9d..6799fa3 100644 --- a/ArkBot/ViewModel/Workspace.cs +++ b/ArkBot/ViewModel/Workspace.cs @@ -440,6 +440,7 @@ internal async Task Init() builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); //register vote handlers builder.RegisterType().As>(); @@ -504,6 +505,7 @@ internal async Task Init() { var playerLastActiveService = Container.Resolve(); var backupService = Container.Resolve(); + var logCleanupService = Container.Resolve(); foreach (var server in _config.Servers) { var clusterContext = _contextManager.GetCluster(server.ClusterKey);