/// <summary> /// Deletes old log files outside of the retention period. /// </summary> private async Task DeleteOldLogFilesAsync() { try { var settingName = Constants.RuntimeSettingNames.LogFilesRetentionInDays; var logFilesRetentionInDays = Convert.ToInt32(await Database.GetApplicationOptionAsync(settingName).ConfigureAwait(false)); var dirInfo = new DirectoryInfo(CoreSettings.GetLogFilesArchiveDirectory()); var files = dirInfo.EnumerateFiles(); var oldFileDate = DateTime.Now.AddDays(-logFilesRetentionInDays); foreach (var file in files) { if (file.Name.StartsWith(LogFileNameFragment) && file.Extension == LogFileExtension && file.LastWriteTime < oldFileDate) { var newFileName = Path.Combine(CoreSettings.GetLogFilesArchiveDirectory(), file.Name); Logger.WriteTraceMessage(string.Format("Removing log file [{0}] that has exceeded the retention period.", file.Name), InstanceID); file.Delete(); } } } catch (Exception ex) { Logger.WriteTraceError("Failed to delete old log files from the log archive folder.", ex, Logger.GenerateFullContextStackTrace(), InstanceID); } }
/// <summary> /// Imports sources for scanning. /// </summary> /// <remarks> /// This function is marked as safe and should not throw exceptions. /// </remarks> /// <returns></returns> private async Task <SourceLocations> SafeImportSourcesAsync() { try { // grab the current copy from DB (this includes last scanned timestamp) var dbSources = await GetSourceLocationsFromDatabaseAsync().ConfigureAwait(false); if (dbSources.Any(x => x.Path == CoreSettings.GetDatabaseBackupsDirectory()) == false) { // add the base meta DB backup folder. var databaseFolderSource = new LocalSourceLocation(); databaseFolderSource.Path = CoreSettings.GetDatabaseBackupsDirectory(); databaseFolderSource.FileMatchFilter = "*.bak"; databaseFolderSource.RevisionCount = 1; databaseFolderSource.Priority = Files.FileBackupPriority.Meta; databaseFolderSource.DestinationContainerName = "archivial-core-database-backups"; await Database.SetSourceLocationAsync(databaseFolderSource).ConfigureAwait(false); } if (dbSources.Any(x => x.Path == CoreSettings.GetLogFilesArchiveDirectory()) == false) { // add the base meta log backup folder. var logFolderSource = new LocalSourceLocation(); logFolderSource.Path = CoreSettings.GetLogFilesArchiveDirectory(); logFolderSource.FileMatchFilter = "*.log"; logFolderSource.RevisionCount = 1; logFolderSource.Priority = Files.FileBackupPriority.Meta; logFolderSource.DestinationContainerName = "archivial-core-log-backups"; await Database.SetSourceLocationAsync(logFolderSource).ConfigureAwait(false); } return(dbSources); } catch (Exception ex) { string err = "Failed to import scan sources."; Logger.WriteSystemEvent(err, ex, Logger.GenerateFullContextStackTrace(), Constants.EventIDs.FailedToLoadScanSources, true); return(null); } }
/// <summary> /// Moves completed log files to the archive directory. /// </summary> private void MoveCompletedLogFiles() { try { var dirInfo = new DirectoryInfo(CoreSettings.GetLogFilesDirectory()); var files = dirInfo.EnumerateFiles(); var oldFileDate = DateTime.Now.AddHours(-24); foreach (var file in files) { if (file.Name.StartsWith(LogFileNameFragment) && file.Extension == LogFileExtension && file.LastWriteTime < oldFileDate) { var newFileName = Path.Combine(CoreSettings.GetLogFilesArchiveDirectory(), file.Name); Logger.WriteTraceMessage(string.Format("Moving completed log file {0} to the archive folder.", file.Name), InstanceID); file.MoveTo(newFileName); } } } catch (Exception ex) { Logger.WriteTraceError("Failed to move completed log files to the log archive folder.", ex, Logger.GenerateFullContextStackTrace(), InstanceID); } }