Skip to content

Commit

Permalink
Update main.d
Browse files Browse the repository at this point in the history
* Simplify existing shutdown cleanup before #2519 is merged
  • Loading branch information
abraunegg committed Oct 25, 2023
1 parent b3a1e4a commit 58d17d5
Showing 1 changed file with 32 additions and 45 deletions.
77 changes: 32 additions & 45 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,55 +69,17 @@ int main(string[] cliArgs) {

// Define 'exit' and 'failure' scopes
scope(exit) {
// detail what scope was called
// 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);
// Perform exit tasks
performStandardExitProcess();
}

scope(failure) {
// detail what scope was called
// 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);

// 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");
itemDB = null;
appConfig = null;
oneDriveApiInstance = null;
selectiveSync = null;
syncEngineInstance = null;
// Perform exit tasks
performStandardExitProcess();
}

// Read in application options as passed in
Expand Down Expand Up @@ -643,7 +606,7 @@ int main(string[] cliArgs) {
}

// Configure the monitor class
Monitor filesystemMonitor = new Monitor(appConfig, selectiveSync);
filesystemMonitor = new Monitor(appConfig, selectiveSync);

// Delegated function for when inotify detects a new local directory has been created
filesystemMonitor.onDirCreated = delegate(string path) {
Expand Down Expand Up @@ -937,6 +900,30 @@ 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);

if (filesystemMonitor !is null) {
filesystemMonitor.shutdown();
object.destroy(filesystemMonitor);
}
}

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();
Expand Down

0 comments on commit 58d17d5

Please sign in to comment.