Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ArkBot/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<file value="logs/application.log" />
<rollingStyle value="Date" />
<appendToFile value="true" />
<maxSizeRollBackups value="30" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<datePattern value="yyyyMMdd'.log'" />
<layout type="log4net.Layout.PatternLayout">
Expand Down
2 changes: 2 additions & 0 deletions ArkBot/ArkBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,9 @@
</Compile>
<Compile Include="Notifications\NotificationManager.cs" />
<Compile Include="ScheduledTasks\ScheduledTasksManager.cs" />
<Compile Include="Services\ILogCleanupService.cs" />
<Compile Include="Services\IPlayerLastActiveService.cs" />
<Compile Include="Services\LogCleanupService.cs" />
<Compile Include="Services\PlayerLastActiveService.cs" />
<Compile Include="Services\ArkServerService.cs" />
<Compile Include="IArkSaveFileWatcher.cs" />
Expand Down
10 changes: 10 additions & 0 deletions ArkBot/Services/ILogCleanupService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;
using Discord;

namespace ArkBot.Helpers
{
public interface ILogCleanupService : IDisposable
{
}
}
77 changes: 77 additions & 0 deletions ArkBot/Services/LogCleanupService.cs
Original file line number Diff line number Diff line change
@@ -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})(?<date>.+?)(?:-\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
}
}
2 changes: 2 additions & 0 deletions ArkBot/ViewModel/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ internal async Task Init()
builder.RegisterType<ArkServerService>().As<IArkServerService>().SingleInstance();
builder.RegisterType<SavegameBackupService>().As<ISavegameBackupService>().SingleInstance();
builder.RegisterType<PlayerLastActiveService>().As<IPlayerLastActiveService>().SingleInstance();
builder.RegisterType<LogCleanupService>().As<ILogCleanupService>().SingleInstance();

//register vote handlers
builder.RegisterType<BanVoteHandler>().As<IVoteHandler<BanVote>>();
Expand Down Expand Up @@ -504,6 +505,7 @@ internal async Task Init()
{
var playerLastActiveService = Container.Resolve<IPlayerLastActiveService>();
var backupService = Container.Resolve<ISavegameBackupService>();
var logCleanupService = Container.Resolve<ILogCleanupService>();
foreach (var server in _config.Servers)
{
var clusterContext = _contextManager.GetCluster(server.ClusterKey);
Expand Down