Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cleanup and database optimisation #168

Merged
merged 1 commit into from
Oct 18, 2023
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
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
Loading