Skip to content

Commit

Permalink
Added cleanup and database optimisation (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-j-green authored Oct 18, 2023
1 parent 7ae6eb8 commit ac97652
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
69 changes: 69 additions & 0 deletions gaseous-server/Classes/Maintenance.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
12 changes: 11 additions & 1 deletion gaseous-server/ProcessQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public void Execute()
gaseous_tools.DatabaseMigration.UpgradeScriptBackgroundTasks();
break;

case QueueItemType.Maintainer:
Logging.Log(Logging.LogType.Debug, "Timered Event", "Starting Maintenance");
Classes.Maintenance.RunMaintenance();
break;

}
}
catch (Exception ex)
Expand Down Expand Up @@ -243,7 +248,12 @@ public enum QueueItemType
/// <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
Expand Down
19 changes: 18 additions & 1 deletion gaseous-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
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);
Expand Down Expand Up @@ -229,7 +239,14 @@
ProcessQueue.QueueItemType.LibraryScan
})
);

ProcessQueue.QueueItems.Add(new ProcessQueue.QueueItem(
ProcessQueue.QueueItemType.Maintainer,
10080,
new List<ProcessQueue.QueueItemType>
{
ProcessQueue.QueueItemType.All
})
);

Logging.WriteToDiskOnly = false;

Expand Down
4 changes: 4 additions & 0 deletions gaseous-server/wwwroot/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ h3 {
padding: 0px;
margin: 0px;
display: flex;
z-index: 50;
}

#banner_icon:hover {
Expand Down Expand Up @@ -198,7 +199,9 @@ h3 {
}

.filter_panel_box {
position: relative;
padding: 10px;
z-index: -1;
}

input[type='text'], input[type='number'] {
Expand Down Expand Up @@ -268,6 +271,7 @@ input[id='filter_panel_userrating_max'] {
display: flex;
position: relative;
padding: 3px;
z-index: -1;
}

.filter_panel_item_checkbox {
Expand Down

0 comments on commit ac97652

Please sign in to comment.