allow starting offline in monitor mode (#266)

* SyncEngine: allow for multiple calls to init by saving the state
* Factor out syncengine initialization code so that it can be reused
* Don't exit in monitor when started offline
* Always call initSyncEngine during regular syncs, this will call the SyncEngine init routine which caches the state, thus a quasi no-op.
* Add forgotten http.shutdown
This commit is contained in:
Norbert Preining 2018-12-04 09:15:44 +09:00 committed by abraunegg
parent 29808c905c
commit b10e641fe9
2 changed files with 47 additions and 25 deletions

View file

@ -186,9 +186,11 @@ int main(string[] args)
} catch (CurlException e) {
// No network connection to OneDrive Service
log.error("No network connection to Microsoft OneDrive Service");
return EXIT_FAILURE;
}
if (!monitor) {
return EXIT_FAILURE;
}
}
// Initialize OneDrive, check for authorization
auto onedrive = new OneDriveApi(cfg, debugHttp);
onedrive.printAccessToken = printAccessToken;
@ -259,21 +261,18 @@ int main(string[] args)
auto sync = new SyncEngine(cfg, onedrive, itemdb, selectiveSync);
try {
sync.init();
} catch (OneDriveException e) {
if (e.httpStatusCode == 400 || e.httpStatusCode == 401) {
// Authorization is invalid
log.log("\nAuthorization token invalid, use --logout to authorize the client again\n");
if (!initSyncEngine(sync)) {
onedrive.http.shutdown();
return EXIT_FAILURE;
}
if (e.httpStatusCode >= 500) {
// There was a HTTP 5xx Server Side Error, message already printed
} catch (CurlException e) {
if (!monitor) {
log.log("\nNo internet connection.");
onedrive.http.shutdown();
return EXIT_FAILURE;
}
}
// We should only set noRemoteDelete in an upload-only scenario
if ((uploadOnly)&&(noRemoteDelete)) sync.setNoRemoteDelete();
@ -383,23 +382,20 @@ int main(string[] args)
auto currTime = MonoTime.currTime();
if (currTime - lastCheckTime > checkInterval) {
try {
online = testNetwork();
if (!initSyncEngine(sync)) {
onedrive.http.shutdown();
return EXIT_FAILURE;
}
performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly);
if (!downloadOnly) {
// discard all events that may have been generated by the sync
m.update(false);
}
} catch (CurlException e) {
// TODO better check of type of exception from Curl
// could be either timeout of operation of connection error
// No network connection to OneDrive Service
log.log("No network connection to Microsoft OneDrive Service, skipping sync");
online = false;
}
if (online) {
try {
performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly);
if (!downloadOnly) {
// discard all events that may have been generated by the sync
m.update(false);
}
} catch (CurlException e) {
// TODO better check of type of exception from Curl
log.log("No network connection to Microsoft OneDrive Service, skipping sync");
}
}
// performSync complete, set lastCheckTime to current time
lastCheckTime = MonoTime.currTime();
@ -415,6 +411,24 @@ int main(string[] args)
return EXIT_SUCCESS;
}
bool initSyncEngine(SyncEngine sync)
{
try {
sync.init();
} catch (OneDriveException e) {
if (e.httpStatusCode == 400 || e.httpStatusCode == 401) {
// Authorization is invalid
log.log("\nAuthorization token invalid, use --logout to authorize the client again\n");
return false;
}
if (e.httpStatusCode >= 500) {
// There was a HTTP 5xx Server Side Error, message already printed
return false;
}
}
return true;
}
// try to synchronize the folder three times
void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, bool localFirst, bool uploadOnly)
{

View file

@ -167,6 +167,8 @@ final class SyncEngine
private bool malwareDetected = false;
// download filesystem issue flag
private bool downloadFailed = false;
// initialization has been done
private bool initDone = false;
this(Config cfg, OneDriveApi onedrive, ItemDatabase itemdb, SelectiveSync selectiveSync)
{
@ -183,6 +185,11 @@ final class SyncEngine
// Set accountType, defaultDriveId, defaultRootId & remainingFreeSpace once and reuse where possible
JSONValue oneDriveDetails;
if (initDone) {
return;
}
// Need to catch 400 or 5xx server side errors at initialization
try {
oneDriveDetails = onedrive.getDefaultDrive();
@ -239,6 +246,7 @@ final class SyncEngine
auto item = session.upload();
saveItem(item);
}
initDone = true;
}
// Configure noRemoteDelete if function is called