Skip to content

Commit

Permalink
Update PR
Browse files Browse the repository at this point in the history
* Update PR
  • Loading branch information
abraunegg committed Oct 5, 2023
1 parent 5127464 commit 1503f96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/itemdb.d
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ final class ItemDatabase {

// Perform a vacuum on the database, commit WAL / SHM to file
void performVacuum() {
log.vdebug("Attempting to perform a database vacuum to merge any temporary data");
try {
auto stmt = db.prepare("VACUUM;");
stmt.exec();
Expand Down
8 changes: 8 additions & 0 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ int main(string[] cliArgs) {
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;
}

// Read in application options as passed in
Expand Down
53 changes: 23 additions & 30 deletions src/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -661,20 +661,16 @@ class SyncEngine {
for (;;) {
responseBundleCount++;
// Get the /delta changes via the OneDrive API
log.vdebug("");
log.vdebug("------------------------------------------------------------------");
log.vdebug("driveIdToQuery: ", driveIdToQuery);
log.vdebug("itemIdToQuery: ", itemIdToQuery);
log.vdebug("deltaLink: ", deltaLink);
log.vdebug("------------------------------------------------------------------");

// getDeltaChangesByItemId has the re-try logic for transient errors
deltaChanges = getDeltaChangesByItemId(driveIdToQuery, itemIdToQuery, deltaLink);

// If deltaChanges is an invalid JSON object, must exit
if (deltaChanges.type() != JSONType.object){
// Handle the invalid JSON response
invalidJSONResponseFromOneDriveAPI();
// If the initial deltaChanges response is an invalid JSON object, keep trying ..
if (deltaChanges.type() != JSONType.object) {
while (deltaChanges.type() != JSONType.object) {
// Handle the invalid JSON response adn retry
log.error("ERROR: Query of the OneDrive API via deltaChanges = getDeltaChangesByItemId() returned an invalid JSON response");
deltaChanges = getDeltaChangesByItemId(driveIdToQuery, itemIdToQuery, deltaLink);
}
}

ulong nrChanges = count(deltaChanges["value"].array);
Expand Down Expand Up @@ -2147,18 +2143,19 @@ class SyncEngine {
OneDriveApi getDeltaQueryOneDriveApiInstance;
getDeltaQueryOneDriveApiInstance = new OneDriveApi(appConfig);
getDeltaQueryOneDriveApiInstance.initialise();

log.vdebug("------------------------------------------------------------------");
log.vdebug("selectedDriveId: ", selectedDriveId);
log.vdebug("selectedItemId: ", selectedItemId);
log.vdebug("providedDeltaLink: ", providedDeltaLink);
log.vdebug("------------------------------------------------------------------");

try {
deltaChangesBundle = getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, providedDeltaLink);
} catch (OneDriveException exception) {
// caught an exception
log.log("------------------------------------------------------------------");
log.log("getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, providedDeltaLink) generated a OneDriveException");
log.log("");
log.log("selectedDriveId: ", selectedDriveId);
log.log("selectedItemId: ", selectedItemId);
log.log("providedDeltaLink: ", providedDeltaLink);
log.log("------------------------------------------------------------------");

log.vdebug("getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, providedDeltaLink) generated a OneDriveException");

auto errorArray = splitLines(exception.msg);
string thisFunctionName = getFunctionName!({});
// HTTP request returned status code 408,429,503,504
Expand All @@ -2181,9 +2178,9 @@ class SyncEngine {
log.vdebug("Thread sleeping for 30 seconds as the server did not receive a timely response from the upstream server it needed to access in attempting to complete the request");
Thread.sleep(dur!"seconds"(30));
}
// re-try original request - retried for 429, 503, 504 - but loop back calling this function
log.vdebug("Retrying Query: ");
deltaChangesBundle = getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, providedDeltaLink);
// dont retry request, loop back to calling function
log.vdebug("Looping back after failure");
deltaChangesBundle = null;
} else {
// Default operation if not 408,429,503,504 errors
if (exception.httpStatusCode == 410) {
Expand All @@ -2192,21 +2189,18 @@ class SyncEngine {
log.log("WARNING: Retrying OneDrive API call without using the locally stored deltaLink value");
// Configure an empty deltaLink
log.vdebug("Delta link expired for 'getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, providedDeltaLink)', setting 'deltaLink = null'");
string emptyDeltaLink;
string emptyDeltaLink = "";
// retry with empty deltaLink
deltaChangesBundle = getDeltaQueryOneDriveApiInstance.viewChangesByItemId(selectedDriveId, selectedItemId, emptyDeltaLink);
} else {
// display what the error is
log.log("CODING TO DO: Hitting this failure error output");
displayOneDriveErrorMessage(exception.msg, thisFunctionName);
deltaChangesBundle = null;
}
}
}

// Check the response JSON
if (deltaChangesBundle.type() != JSONType.object){
// Handle the invalid JSON response
invalidJSONResponseFromOneDriveAPI();
}

// Shutdown the API
getDeltaQueryOneDriveApiInstance.shutdown();
// Free object and memory
Expand Down Expand Up @@ -2250,7 +2244,6 @@ class SyncEngine {

// If the JSON response is not correct JSON object, exit
void invalidJSONResponseFromOneDriveAPI() {

log.error("ERROR: Query of the OneDrive API returned an invalid JSON response");
// Must exit
exit(-1);
Expand Down

0 comments on commit 1503f96

Please sign in to comment.