From 12947d160f484cf0bebd6a0e9afacc4b00c132fc Mon Sep 17 00:00:00 2001 From: abraunegg Date: Wed, 30 Oct 2019 17:46:02 +1100 Subject: [PATCH] Fix unhandled monitor initialisation exception (Issue #704) (#705) * Catch MonitorException when initialisation failure occurs, print error and exit ... cant enter monitor loop if we cant initialise correctly. * Cleanup and add documentation update --- docs/USAGE.md | 25 ++++++++++ src/main.d | 131 +++++++++++++++++++++++++++----------------------- 2 files changed, 95 insertions(+), 61 deletions(-) diff --git a/docs/USAGE.md b/docs/USAGE.md index b515e3c2..9c59dca2 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -427,6 +427,31 @@ Currently not supported. ### SharePoint / Office 365 Shared Libraries Refer to [./Office365.md](Office365.md) for configuration assistance. +## Running 'onedrive' in 'monitor' mode +Monitor mode (`--monitor`) allows the onedrive process to continually monitor your local file system for changes to files. + +Two common errors can occur when using monitor mode: +* Intialisation failure +* Unable to add a new inotify watch + +Both of these errors are local environment issues, where the following system variables need to be increased as the current system values are potentially too low: +* `fs.file-max` +* `fs.inotify.max_user_watches` + +To determine what these values are on your system use the following commands: +``` +sysctl fs.file-max +sysctl fs.inotify.max_user_watches +``` + +To make a change to these variables: +``` +sudo sysctl fs.file-max= +sudo sysctl fs.inotify.max_user_watches= +``` + +To make these changes permanent, refer to your OS reference documentation. + ## Running 'onedrive' as a system service There are two ways that onedrive can be used as a service * via init.d diff --git a/src/main.d b/src/main.d index 80d514f9..97616b16 100644 --- a/src/main.d +++ b/src/main.d @@ -671,72 +671,81 @@ int main(string[] args) signal(SIGINT, &exitHandler); signal(SIGTERM, &exitHandler); - // initialise the monitor class - if (!cfg.getValueBool("download_only")) m.init(cfg, cfg.getValueLong("verbose") > 0, cfg.getValueBool("skip_symlinks"), cfg.getValueBool("check_nosync")); - // monitor loop - immutable auto checkInterval = dur!"seconds"(cfg.getValueLong("monitor_interval")); - immutable auto logInterval = cfg.getValueLong("monitor_log_frequency"); - immutable auto fullScanFrequency = cfg.getValueLong("monitor_fullscan_frequency"); - auto lastCheckTime = MonoTime.currTime(); - auto logMonitorCounter = 0; - auto fullScanCounter = 0; - bool fullScanRequired = true; - bool syncListConfiguredOverride = false; - while (true) { - if (!cfg.getValueBool("download_only")) m.update(online); - auto currTime = MonoTime.currTime(); - if (currTime - lastCheckTime > checkInterval) { - // log monitor output suppression - logMonitorCounter += 1; - if (logMonitorCounter > logInterval) - logMonitorCounter = 1; - - // full scan of sync_dir - fullScanCounter += 1; - if (fullScanCounter > fullScanFrequency){ - fullScanCounter = 1; - fullScanRequired = true; - if (syncListConfigured) { - syncListConfiguredOverride = true; - } - } - - // log.logAndNotify("DEBUG trying to create checkpoint"); - // auto res = itemdb.db_checkpoint(); - // log.logAndNotify("Checkpoint return: ", res); - // itemdb.dump_open_statements(); - - try { - if (!initSyncEngine(sync)) { - oneDrive.http.shutdown(); - return EXIT_FAILURE; + // attempt to initialise monitor class + try { + m.init(cfg, cfg.getValueLong("verbose") > 0, cfg.getValueBool("skip_symlinks"), cfg.getValueBool("check_nosync")); + } catch (MonitorException e) { + // monitor initialisation failed + log.error("ERROR: ", e.msg); + exit(-1); + } + + if (!cfg.getValueBool("download_only")) { + // monitor loop + immutable auto checkInterval = dur!"seconds"(cfg.getValueLong("monitor_interval")); + immutable auto logInterval = cfg.getValueLong("monitor_log_frequency"); + immutable auto fullScanFrequency = cfg.getValueLong("monitor_fullscan_frequency"); + auto lastCheckTime = MonoTime.currTime(); + auto logMonitorCounter = 0; + auto fullScanCounter = 0; + bool fullScanRequired = true; + bool syncListConfiguredOverride = false; + while (true) { + if (!cfg.getValueBool("download_only")) m.update(online); + auto currTime = MonoTime.currTime(); + if (currTime - lastCheckTime > checkInterval) { + // log monitor output suppression + logMonitorCounter += 1; + if (logMonitorCounter > logInterval) + logMonitorCounter = 1; + + // full scan of sync_dir + fullScanCounter += 1; + if (fullScanCounter > fullScanFrequency){ + fullScanCounter = 1; + fullScanRequired = true; + if (syncListConfigured) { + syncListConfiguredOverride = true; + } } + + // log.logAndNotify("DEBUG trying to create checkpoint"); + // auto res = itemdb.db_checkpoint(); + // log.logAndNotify("Checkpoint return: ", res); + // itemdb.dump_open_statements(); + try { - // perform a --monitor sync - performSync(sync, cfg.getValueString("single_directory"), cfg.getValueBool("download_only"), cfg.getValueBool("local_first"), cfg.getValueBool("upload_only"), (logMonitorCounter == logInterval ? MONITOR_LOG_QUIET : MONITOR_LOG_SILENT), fullScanRequired, syncListConfiguredOverride); - if (!cfg.getValueBool("download_only")) { - // discard all events that may have been generated by the sync - m.update(false); + if (!initSyncEngine(sync)) { + oneDrive.http.shutdown(); + return EXIT_FAILURE; + } + try { + // perform a --monitor sync + performSync(sync, cfg.getValueString("single_directory"), cfg.getValueBool("download_only"), cfg.getValueBool("local_first"), cfg.getValueBool("upload_only"), (logMonitorCounter == logInterval ? MONITOR_LOG_QUIET : MONITOR_LOG_SILENT), fullScanRequired, syncListConfiguredOverride); + if (!cfg.getValueBool("download_only")) { + // discard all events that may have been generated by the sync + m.update(false); + } + } catch (CurlException e) { + // we already tried three times in the performSync routine + // if we still have problems, then the sync handle might have + // gone stale and we need to re-initialize the sync engine + log.log("Persistent connection errors, reinitializing connection"); + sync.reset(); } } catch (CurlException e) { - // we already tried three times in the performSync routine - // if we still have problems, then the sync handle might have - // gone stale and we need to re-initialize the sync engine - log.log("Persistent connection errors, reinitializing connection"); - sync.reset(); + log.log("Cannot initialize connection to OneDrive"); } - } catch (CurlException e) { - log.log("Cannot initialize connection to OneDrive"); - } - // performSync complete, set lastCheckTime to current time - fullScanRequired = false; - if (syncListConfigured) { - syncListConfiguredOverride = false; - } - lastCheckTime = MonoTime.currTime(); - GC.collect(); - } - Thread.sleep(dur!"msecs"(500)); + // performSync complete, set lastCheckTime to current time + fullScanRequired = false; + if (syncListConfigured) { + syncListConfiguredOverride = false; + } + lastCheckTime = MonoTime.currTime(); + GC.collect(); + } + Thread.sleep(dur!"msecs"(500)); + } } } }