Added cleanup and database optimisation (#168)
This commit is contained in:
69
gaseous-server/Classes/Maintenance.cs
Normal file
69
gaseous-server/Classes/Maintenance.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using gaseous_server.Models;
|
||||
using gaseous_tools;
|
||||
using Microsoft.VisualStudio.Web.CodeGeneration;
|
||||
|
||||
namespace gaseous_server.Classes
|
||||
{
|
||||
public class Maintenance
|
||||
{
|
||||
const int MaxFileAge = 30;
|
||||
|
||||
public static void RunMaintenance()
|
||||
{
|
||||
// delete files and directories older than 7 days in PathsToClean
|
||||
List<string> PathsToClean = new List<string>();
|
||||
PathsToClean.Add(Config.LibraryConfiguration.LibraryUploadDirectory);
|
||||
PathsToClean.Add(Config.LibraryConfiguration.LibraryTempDirectory);
|
||||
|
||||
foreach (string PathToClean in PathsToClean)
|
||||
{
|
||||
Logging.Log(Logging.LogType.Information, "Maintenance", "Removing files older than " + MaxFileAge + " days from " + PathToClean);
|
||||
|
||||
// get content
|
||||
// files first
|
||||
foreach (string filePath in Directory.GetFiles(PathToClean))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(filePath);
|
||||
if (fileInfo.LastWriteTimeUtc.AddDays(MaxFileAge) < DateTime.UtcNow)
|
||||
{
|
||||
Logging.Log(Logging.LogType.Warning, "Maintenance", "Deleting file " + filePath);
|
||||
File.Delete(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// now directories
|
||||
foreach (string dirPath in Directory.GetDirectories(PathToClean))
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
|
||||
if (directoryInfo.LastWriteTimeUtc.AddDays(MaxFileAge) < DateTime.UtcNow)
|
||||
{
|
||||
Logging.Log(Logging.LogType.Warning, "Maintenance", "Deleting directory " + directoryInfo);
|
||||
Directory.Delete(dirPath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Logging.Log(Logging.LogType.Information, "Maintenance", "Optimising database tables");
|
||||
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
|
||||
string sql = "SHOW TABLES;";
|
||||
DataTable tables = db.ExecuteCMD(sql);
|
||||
|
||||
foreach (DataRow row in tables.Rows)
|
||||
{
|
||||
sql = "OPTIMIZE TABLE " + row[0].ToString();
|
||||
DataTable response = db.ExecuteCMD(sql);
|
||||
foreach (DataRow responseRow in response.Rows)
|
||||
{
|
||||
string retVal = "";
|
||||
for (int i = 0; i < responseRow.ItemArray.Length; i++)
|
||||
{
|
||||
retVal += responseRow.ItemArray[i] + "; ";
|
||||
}
|
||||
Logging.Log(Logging.LogType.Information, "Maintenance", "Optimizse table " + row[0].ToString() + ": " + retVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -164,6 +164,11 @@ namespace gaseous_server
|
||||
gaseous_tools.DatabaseMigration.UpgradeScriptBackgroundTasks();
|
||||
break;
|
||||
|
||||
case QueueItemType.Maintainer:
|
||||
Logging.Log(Logging.LogType.Debug, "Timered Event", "Starting Maintenance");
|
||||
Classes.Maintenance.RunMaintenance();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -243,7 +248,12 @@ namespace gaseous_server
|
||||
/// <summary>
|
||||
/// Performs and post database upgrade scripts that can be processed as a background task
|
||||
/// </summary>
|
||||
BackgroundDatabaseUpgrade
|
||||
BackgroundDatabaseUpgrade,
|
||||
|
||||
/// <summary>
|
||||
/// Performs a clean up of old files, and optimises the database
|
||||
/// </summary>
|
||||
Maintainer
|
||||
}
|
||||
|
||||
public enum QueueItemState
|
||||
|
@@ -48,6 +48,16 @@ if (Config.ReadSetting("API Key", "Test API Key") == "Test API Key")
|
||||
Config.SetSetting("API Key", APIKey.ToString());
|
||||
}
|
||||
|
||||
// clean up storage
|
||||
if (Directory.Exists(Config.LibraryConfiguration.LibraryTempDirectory))
|
||||
{
|
||||
Directory.Delete(Config.LibraryConfiguration.LibraryTempDirectory, true);
|
||||
}
|
||||
if (Directory.Exists(Config.LibraryConfiguration.LibraryUploadDirectory))
|
||||
{
|
||||
Directory.Delete(Config.LibraryConfiguration.LibraryUploadDirectory, true);
|
||||
}
|
||||
|
||||
// kick off any delayed upgrade tasks
|
||||
// run 1002 background updates in the background on every start
|
||||
DatabaseMigration.BackgroundUpgradeTargetSchemaVersions.Add(1002);
|
||||
@@ -229,7 +239,14 @@ ProcessQueue.QueueItems.Add(new ProcessQueue.QueueItem(
|
||||
ProcessQueue.QueueItemType.LibraryScan
|
||||
})
|
||||
);
|
||||
|
||||
ProcessQueue.QueueItems.Add(new ProcessQueue.QueueItem(
|
||||
ProcessQueue.QueueItemType.Maintainer,
|
||||
10080,
|
||||
new List<ProcessQueue.QueueItemType>
|
||||
{
|
||||
ProcessQueue.QueueItemType.All
|
||||
})
|
||||
);
|
||||
|
||||
Logging.WriteToDiskOnly = false;
|
||||
|
||||
|
@@ -101,6 +101,7 @@ h3 {
|
||||
padding: 0px;
|
||||
margin: 0px;
|
||||
display: flex;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
#banner_icon:hover {
|
||||
@@ -198,7 +199,9 @@ h3 {
|
||||
}
|
||||
|
||||
.filter_panel_box {
|
||||
position: relative;
|
||||
padding: 10px;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
input[type='text'], input[type='number'] {
|
||||
@@ -268,6 +271,7 @@ input[id='filter_panel_userrating_max'] {
|
||||
display: flex;
|
||||
position: relative;
|
||||
padding: 3px;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
.filter_panel_item_checkbox {
|
||||
|
Reference in New Issue
Block a user