mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-16 12:45:14 +02:00
Add webhook support for blocking wait
This commit is contained in:
parent
116a7b945b
commit
90c6200425
84
src/main.d
84
src/main.d
|
@ -749,6 +749,7 @@ int main(string[] cliArgs) {
|
||||||
MonoTime lastGitHubCheckTime = MonoTime.currTime();
|
MonoTime lastGitHubCheckTime = MonoTime.currTime();
|
||||||
string loopStartOutputMessage = "################################################## NEW LOOP ##################################################";
|
string loopStartOutputMessage = "################################################## NEW LOOP ##################################################";
|
||||||
string loopStopOutputMessage = "################################################ LOOP COMPLETE ###############################################";
|
string loopStopOutputMessage = "################################################ LOOP COMPLETE ###############################################";
|
||||||
|
bool notificationReceived = false;
|
||||||
|
|
||||||
while (performMonitor) {
|
while (performMonitor) {
|
||||||
// Do we need to validate the runtimeSyncDirectory to check for the presence of a '.nosync' file - the disk may have been ejected ..
|
// Do we need to validate the runtimeSyncDirectory to check for the presence of a '.nosync' file - the disk may have been ejected ..
|
||||||
|
@ -766,33 +767,12 @@ int main(string[] cliArgs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webhook Notification Handling
|
// Webhook Notification Handling
|
||||||
bool notificationReceived = false;
|
|
||||||
|
|
||||||
// Check for notifications pushed from Microsoft to the webhook
|
// Check for notifications pushed from Microsoft to the webhook
|
||||||
if (webhookEnabled) {
|
if (webhookEnabled) {
|
||||||
// Create a subscription on the first run, or renew the subscription
|
// Create a subscription on the first run, or renew the subscription
|
||||||
// on subsequent runs when it is about to expire.
|
// on subsequent runs when it is about to expire.
|
||||||
oneDriveApiInstance.createOrRenewSubscription();
|
oneDriveApiInstance.createOrRenewSubscription();
|
||||||
|
|
||||||
// Process incoming notifications if any.
|
|
||||||
|
|
||||||
// Empirical evidence shows that Microsoft often sends multiple
|
|
||||||
// notifications for one single change, so we need a loop to exhaust
|
|
||||||
// all signals that were queued up by the webhook. The notifications
|
|
||||||
// do not contain any actual changes, and we will always rely do the
|
|
||||||
// delta endpoint to sync to latest. Therefore, only one sync run is
|
|
||||||
// good enough to catch up for multiple notifications.
|
|
||||||
for (int signalCount = 0;; signalCount++) {
|
|
||||||
const auto signalExists = receiveTimeout(dur!"seconds"(-1), (ulong _) {});
|
|
||||||
if (signalExists) {
|
|
||||||
notificationReceived = true;
|
|
||||||
} else {
|
|
||||||
if (notificationReceived) {
|
|
||||||
log.log("Received ", signalCount," refresh signals from the webhook");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the current time this loop is starting
|
// Get the current time this loop is starting
|
||||||
|
@ -926,17 +906,61 @@ int main(string[] cliArgs) {
|
||||||
auto nextCheckTime = lastCheckTime + checkOnlineInterval;
|
auto nextCheckTime = lastCheckTime + checkOnlineInterval;
|
||||||
currentTime = MonoTime.currTime();
|
currentTime = MonoTime.currTime();
|
||||||
auto sleepTime = nextCheckTime - currentTime;
|
auto sleepTime = nextCheckTime - currentTime;
|
||||||
if(filesystemMonitor.initialised) {
|
log.vdebug("Sleep for ", sleepTime);
|
||||||
// If local monitor is on
|
|
||||||
// start the worker and wait for event
|
if(filesystemMonitor.initialised || webhookEnabled) {
|
||||||
if(!filesystemMonitor.isWorking()) {
|
if(filesystemMonitor.initialised) {
|
||||||
workerTid.send(1);
|
// If local monitor is on
|
||||||
|
// start the worker and wait for event
|
||||||
|
if(!filesystemMonitor.isWorking()) {
|
||||||
|
workerTid.send(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(webhookEnabled) {
|
||||||
|
// if onedrive webhook is enabled
|
||||||
|
// update sleep time based on renew interval
|
||||||
|
Duration nextWebhookCheckDuration = oneDriveApiInstance.getNextExpirationCheckDuration();
|
||||||
|
if (nextWebhookCheckDuration < sleepTime) {
|
||||||
|
sleepTime = nextWebhookCheckDuration;
|
||||||
|
log.vdebug("Update sleeping time to ", sleepTime);
|
||||||
|
}
|
||||||
|
notificationReceived = false;
|
||||||
|
}
|
||||||
|
|
||||||
int res = 1;
|
int res = 1;
|
||||||
|
// Process incoming notifications if any.
|
||||||
receiveTimeout(sleepTime, (int msg) {
|
auto signalExists = receiveTimeout(sleepTime,
|
||||||
res = msg;
|
(int msg) {
|
||||||
});
|
res = msg;
|
||||||
|
},
|
||||||
|
(ulong _) {
|
||||||
|
notificationReceived = true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
log.vdebug("signalExists = ", signalExists);
|
||||||
|
log.vdebug("worker status = ", res);
|
||||||
|
log.vdebug("notificationReceived = ", notificationReceived);
|
||||||
|
|
||||||
|
// Empirical evidence shows that Microsoft often sends multiple
|
||||||
|
// notifications for one single change, so we need a loop to exhaust
|
||||||
|
// all signals that were queued up by the webhook. The notifications
|
||||||
|
// do not contain any actual changes, and we will always rely do the
|
||||||
|
// delta endpoint to sync to latest. Therefore, only one sync run is
|
||||||
|
// good enough to catch up for multiple notifications.
|
||||||
|
int signalCount = notificationReceived ? 1 : 0;
|
||||||
|
for (;; signalCount++) {
|
||||||
|
signalExists = receiveTimeout(dur!"seconds"(-1), (ulong _) {});
|
||||||
|
if (signalExists) {
|
||||||
|
notificationReceived = true;
|
||||||
|
} else {
|
||||||
|
if (notificationReceived) {
|
||||||
|
log.log("Received ", signalCount," refresh signals from the webhook");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(res == -1) {
|
if(res == -1) {
|
||||||
log.error("Error: Monitor worker failed.");
|
log.error("Error: Monitor worker failed.");
|
||||||
monitorFailures = true;
|
monitorFailures = true;
|
||||||
|
|
|
@ -846,6 +846,15 @@ class OneDriveApi {
|
||||||
void resetRetryAfterValue() {
|
void resetRetryAfterValue() {
|
||||||
retryAfterValue = 0;
|
retryAfterValue = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the duration to next subscriptionExpiration check
|
||||||
|
Duration getNextExpirationCheckDuration() {
|
||||||
|
SysTime now = Clock.currTime(UTC());
|
||||||
|
if (hasValidSubscription())
|
||||||
|
return subscriptionExpiration - now - subscriptionRenewalInterval;
|
||||||
|
else
|
||||||
|
return subscriptionRetryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new subscription or renew the existing subscription
|
// Create a new subscription or renew the existing subscription
|
||||||
void createOrRenewSubscription() {
|
void createOrRenewSubscription() {
|
||||||
|
|
Loading…
Reference in a new issue