using ServiceShared;
namespace ServiceInside.Service
{
public class CleanUp
{
///
/// Flag for disposed
///
private static bool _Disposed = false;
///
/// Results database controller
///
private static ServiceShared.Database.Controllers.Results _dbResults = null;
private static ServiceShared.Database.Controllers.Traces _dbTraces = null;
///
/// old days to cleanup data
///
private static int _OldDays = 30;
///
/// Log Path to clean old files
///
private static string _LogPath = null;
///
/// Flag for service first run
///
private static bool FirstRun = true;
///
/// Set disposed flag to the CleanUp Service to stop background thread
///
///
public static void SetDisposed(bool disposed)
{
_Disposed = disposed;
}
///
/// Sets database controllers to the CleanUp Service
///
/// Database.Controllers.Results?
/// Database.Controllers.Traces?
public static void SetDbContext(ServiceShared.Database.Controllers.Results dbResults, ServiceShared.Database.Controllers.Traces dbTraces)
{
_dbResults = dbResults;
_dbTraces = dbTraces;
}
///
/// Sets old days for CleanUp Service
///
/// number of old days
public static void SetOldDays(int oldDays = 30, string logPath = null)
{
_OldDays = oldDays;
_LogPath = logPath;
}
///
/// Clean up old data in background
///
public static void CleanUpInBackground()
{
try
{
if (_dbResults != null && _dbTraces != null && !_Disposed)
{
Thread thread = new Thread(() =>
{
while (!_Disposed)
{
// Cleanup in the night up between 02:00 - 03:00
if (DateTime.Now.Hour == 2 || FirstRun)
{
FirstRun = false;
Log.Info("CleanUp Database");
_dbResults.CleanUp(_OldDays);
Log.Info("CleanUp files");
if(!string.IsNullOrEmpty(_LogPath))
{
Log.Info("CleanUp log files at " + _LogPath);
/** delete public files **/
string[] logFiles = Directory.GetFiles(_LogPath, "*.log");
if (logFiles != null && logFiles.Length > 0)
{
foreach (string file in logFiles)
{
DateTime nowTime = DateTime.Now;
DateTime createdTime = File.GetCreationTime(file);
int days = (int)(nowTime - createdTime).TotalDays;
if (days > _OldDays)
{
File.Delete(file);
}
}
}
}
/** delete public files **/
string[] publicFiles = Directory.GetFiles(BackgroundWorker.GetPublicDirectory());
if (publicFiles != null && publicFiles.Length > 0)
{
foreach (string file in publicFiles)
{
DateTime nowTime = DateTime.Now;
DateTime createdTime = File.GetCreationTime(file);
int days = (int)(nowTime - createdTime).TotalDays;
if (days > _OldDays)
{
File.Delete(file);
}
}
}
/** delete private files **/
string[] privateFiles = Directory.GetFiles(BackgroundWorker.GetPrivateDirectory());
if (privateFiles != null && privateFiles.Length > 0)
{
foreach (string file in privateFiles)
{
DateTime nowTime = DateTime.Now;
DateTime createdTime = File.GetCreationTime(file);
int days = (int)(nowTime - createdTime).TotalDays;
if (days > _OldDays)
{
File.Delete(file);
}
}
}
/** delete ack files **/
string[] ackFiles = Directory.GetFiles(BackgroundWorker.GetPrivateDirectoryAck());
if (ackFiles != null && ackFiles.Length > 0)
{
foreach (string file in ackFiles)
{
DateTime nowTime = DateTime.Now;
DateTime createdTime = File.GetCreationTime(file);
int days = (int)(nowTime - createdTime).TotalDays;
if (days > _OldDays)
{
File.Delete(file);
}
}
}
/** delete ack done files **/
string[] ackDoneFiles = Directory.GetFiles(BackgroundWorker.GetPrivateDirectoryAckDone());
if (ackDoneFiles != null && ackDoneFiles.Length > 0)
{
foreach (string file in ackDoneFiles)
{
DateTime nowTime = DateTime.Now;
DateTime createdTime = File.GetCreationTime(file);
int days = (int)(nowTime - createdTime).TotalDays;
if (days > _OldDays)
{
File.Delete(file);
}
}
}
}
Thread.Sleep((((1000 * 60) * 3600))); // one hour cpu sleep
}
});
thread.Start();
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceInside.Service.CleanUp", "CleanUpInBackground");
}
}
}
}