Refine shutdown cleanup

This commit is contained in:
Jcomp 2023-10-20 10:25:30 +00:00
parent 90c6200425
commit 7d11a8d429
2 changed files with 29 additions and 37 deletions

View file

@ -41,6 +41,7 @@ OneDriveApi oneDriveApiInstance;
SyncEngine syncEngineInstance;
ItemDatabase itemDB;
ClientSideFiltering selectiveSync;
Monitor filesystemMonitor;
int main(string[] cliArgs) {
// Disable buffering on stdout - this is needed so that when we are using plain write() it will go to the terminal
@ -72,44 +73,14 @@ int main(string[] cliArgs) {
// detail what scope was called
log.vdebug("Exit scope was called");
// Was itemDB initialised?
if (itemDB !is null) {
// Make sure the .wal file is incorporated into the main db before we exit
itemDB.performVacuum();
object.destroy(itemDB);
}
// Free other objects and memory
if (appConfig !is null) {
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles(appConfig.databaseFilePathDryRun);
object.destroy(appConfig);
}
if (oneDriveApiInstance !is null) object.destroy(oneDriveApiInstance);
if (selectiveSync !is null) object.destroy(selectiveSync);
if (syncEngineInstance !is null) object.destroy(syncEngineInstance);
performStandardExitProcess();
}
scope(failure) {
// detail what scope was called
log.vdebug("Failure scope was called");
// Was itemDB initialised?
if (itemDB !is null) {
// Make sure the .wal file is incorporated into the main db before we exit
itemDB.performVacuum();
object.destroy(itemDB);
}
// Free other objects and memory
if (appConfig !is null) {
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles(appConfig.databaseFilePathDryRun);
object.destroy(appConfig);
}
if (oneDriveApiInstance !is null) object.destroy(oneDriveApiInstance);
if (selectiveSync !is null) object.destroy(selectiveSync);
if (syncEngineInstance !is null) object.destroy(syncEngineInstance);
performStandardExitProcess();
// Set these to be null due to failure scope - prevent 'ERROR: Unable to perform a database vacuum: out of memory' when the exit scope is then called
log.vdebug("Setting Class Objects to null due to failure scope");
@ -644,8 +615,8 @@ int main(string[] cliArgs) {
}
// Configure the monitor class
Monitor filesystemMonitor = new Monitor(appConfig, selectiveSync);
Tid workerTid;
filesystemMonitor = new Monitor(appConfig, selectiveSync);
// Delegated function for when inotify detects a new local directory has been created
filesystemMonitor.onDirCreated = delegate(string path) {
@ -972,9 +943,6 @@ int main(string[] cliArgs) {
}
}
}
if (filesystemMonitor.initialised) {
workerTid.send(0); // exit signal
}
}
} else {
// Exit application as the sync engine could not be initialised
@ -991,6 +959,27 @@ int main(string[] cliArgs) {
}
}
void performStandardExitProcess() {
// Was itemDB initialised?
if (itemDB !is null) {
// Make sure the .wal file is incorporated into the main db before we exit
itemDB.performVacuum();
object.destroy(itemDB);
}
// Free other objects and memory
if (appConfig !is null) {
// Cleanup any existing dry-run elements ... these should never be left hanging around
cleanupDryRunDatabaseFiles(appConfig.databaseFilePathDryRun);
object.destroy(appConfig);
}
if (oneDriveApiInstance !is null) object.destroy(oneDriveApiInstance);
if (selectiveSync !is null) object.destroy(selectiveSync);
if (syncEngineInstance !is null) object.destroy(syncEngineInstance);
// cleanup hooks
if (filesystemMonitor && filesystemMonitor.initialised) filesystemMonitor.shutdown();
}
void performUploadOnlySyncProcess(string localPath, Monitor filesystemMonitor = null) {
// Perform the local database consistency check, picking up locally modified data and uploading this to OneDrive
syncEngineInstance.performDatabaseConsistencyAndIntegrityCheck();

View file

@ -128,7 +128,10 @@ shared class MonitorBackgroundWorker {
}
void shutdown() {
if (fd > 0) close(fd);
if (fd > 0) {
close(fd);
fd = 0;
}
}
}