emailengine-app 2.84.2 → 2.86.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ncurc.js +29 -9
- package/.prettierignore +5 -1
- package/.prettierrc.js +2 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +107 -0
- package/Dockerfile +20 -1
- package/ENVIRONMENT.md +139 -0
- package/README.md +2 -2
- package/bin/emailengine.js +434 -408
- package/config/default.toml +2 -0
- package/copy-static-files.sh +41 -14
- package/data/google-crawlers.json +1 -1
- package/data/public_suffix_list.dat +16563 -0
- package/eslint.config.js +81 -99
- package/lib/account-assignment.js +19 -0
- package/lib/account.js +230 -170
- package/lib/api-error-codes.js +49 -0
- package/lib/api-reference/format.js +4 -3
- package/lib/api-reference/model.js +6 -1
- package/lib/api-routes/account-routes.js +255 -38
- package/lib/api-routes/blocklist-routes.js +91 -4
- package/lib/api-routes/delivery-test-routes.js +27 -4
- package/lib/api-routes/export-routes.js +28 -2
- package/lib/api-routes/gateway-routes.js +110 -12
- package/lib/api-routes/index.js +4 -0
- package/lib/api-routes/license-routes.js +2 -2
- package/lib/api-routes/mailbox-routes.js +44 -5
- package/lib/api-routes/message-routes.js +112 -10
- package/lib/api-routes/oauth2-app-routes.js +63 -23
- package/lib/api-routes/outbox-routes.js +97 -52
- package/lib/api-routes/permission-map.js +67 -7
- package/lib/api-routes/scoped-routes.js +74 -0
- package/lib/api-routes/settings-routes.js +34 -6
- package/lib/api-routes/stats-routes.js +5 -1
- package/lib/api-routes/submit-routes.js +337 -309
- package/lib/api-routes/template-routes.js +45 -8
- package/lib/api-routes/token-routes.js +152 -100
- package/lib/api-routes/webhook-delivery-routes.js +262 -0
- package/lib/api-routes/webhook-route-routes.js +132 -6
- package/lib/bounce-detect.js +397 -205
- package/lib/calendar-event.js +81 -1
- package/lib/consts.js +38 -45
- package/lib/db.js +14 -6
- package/lib/delivery-error.js +6 -0
- package/lib/deprecations.js +270 -0
- package/lib/email-client/base-client.js +137 -184
- package/lib/email-client/fallback-poll.js +62 -0
- package/lib/email-client/gmail-client.js +60 -22
- package/lib/email-client/imap/adaptive-idle-store.js +73 -0
- package/lib/email-client/imap/adaptive-idle.js +278 -0
- package/lib/email-client/imap/connection-errors.js +14 -1
- package/lib/email-client/imap/idle-settings.js +31 -5
- package/lib/email-client/imap/listing-diff.js +37 -0
- package/lib/email-client/imap/mailbox.js +372 -242
- package/lib/email-client/imap/subconnection.js +8 -30
- package/lib/email-client/imap/sync-operations.js +220 -127
- package/lib/email-client/imap-client.js +228 -164
- package/lib/email-client/message-builder.js +14 -79
- package/lib/email-client/notification-handler.js +1 -2
- package/lib/email-client/outlook-client.js +372 -338
- package/lib/email-client/smtp-pool-manager.js +16 -13
- package/lib/email-client/sync-state-fields.js +19 -0
- package/lib/encrypt.js +157 -41
- package/lib/enum-descriptions.js +3 -1
- package/lib/env-setting-overrides.js +288 -0
- package/lib/export-worker.js +751 -0
- package/lib/export.js +64 -15
- package/lib/gateway.js +19 -4
- package/lib/get-raw-email.js +25 -107
- package/lib/hapi-request-logger.js +107 -0
- package/lib/health.js +75 -0
- package/lib/imapproxy/imap-core/README.md +6 -5
- package/lib/lock-run.js +119 -0
- package/lib/logger-consts.js +48 -0
- package/lib/logger.js +1 -1
- package/lib/lua/z-head-scores.lua +28 -0
- package/lib/mcp/tools.js +37 -8
- package/lib/network-errors.js +1 -1
- package/lib/node-version.js +24 -0
- package/lib/notify-attachments.js +36 -0
- package/lib/oauth/outlook-mailbox.js +35 -0
- package/lib/oauth/state.js +24 -0
- package/lib/oauth2-apps.js +631 -656
- package/lib/outbox.js +184 -54
- package/lib/parse-message.js +3 -3
- package/lib/pbkdf2-phc.js +1 -1
- package/lib/privileged-settings.js +2 -2
- package/lib/queue-browser/definitions.js +11 -0
- package/lib/queue-browser/index.js +68 -19
- package/lib/queue-job-actions.js +18 -1
- package/lib/queue-retention.js +48 -12
- package/lib/queue-stats.js +11 -2
- package/lib/read-env-value.js +3 -0
- package/lib/redis-memory-warnings.js +99 -0
- package/lib/redis-reconnect.js +29 -0
- package/lib/request-timing.js +23 -0
- package/lib/respawn-backoff.js +35 -0
- package/lib/rewrite-text-nodes.js +19 -59
- package/lib/schemas.js +164 -40
- package/lib/settings.js +6 -2
- package/lib/smtp-auth.js +19 -1
- package/lib/sso.js +1 -1
- package/lib/swagger-options.js +1 -1
- package/lib/thread-diagnostics.js +63 -0
- package/lib/timezones.js +109 -0
- package/lib/tls/provision.js +6 -3
- package/lib/token-audit-log.js +19 -20
- package/lib/token-permission-view.js +7 -6
- package/lib/tools.js +48 -51
- package/lib/totp.js +2 -5
- package/lib/translations.js +1 -2
- package/lib/ui-routes/account-routes.js +475 -454
- package/lib/ui-routes/account-setup.js +99 -0
- package/lib/ui-routes/admin-config-routes.js +152 -100
- package/lib/ui-routes/admin-entities-routes.js +93 -21
- package/lib/ui-routes/auth-routes.js +90 -38
- package/lib/ui-routes/imap-idle-field.js +1 -1
- package/lib/ui-routes/internals-routes.js +5 -0
- package/lib/ui-routes/oauth-config-routes.js +124 -82
- package/lib/ui-routes/outlook-subscription-view.js +1 -1
- package/lib/ui-routes/overridable-fields.js +84 -0
- package/lib/ui-routes/queue-routes.js +8 -5
- package/lib/ui-routes/route-helpers.js +19 -3
- package/lib/ui-routes/settings-page.js +6 -0
- package/lib/ui-routes/tls-config-routes.js +58 -15
- package/lib/utils/back-off.js +82 -0
- package/lib/utils/decode-headers.js +3 -12
- package/lib/utils/filter-empty.js +20 -0
- package/lib/utils/int-setting.js +48 -0
- package/lib/utils/murmurhash3.js +66 -0
- package/lib/utils/public-suffix.js +122 -0
- package/lib/utils/random-id.js +26 -0
- package/lib/utils/tracking-settings.js +35 -0
- package/lib/utils/version.js +74 -0
- package/lib/web-safe-html.js +18 -105
- package/lib/webhook-delivery.js +461 -0
- package/lib/webhook-failures.js +132 -0
- package/lib/webhook-request.js +29 -1
- package/lib/webhook-routing.js +56 -0
- package/lib/webhook-test.js +88 -0
- package/lib/webhooks.js +93 -45
- package/lib/worker-rpc-error.js +2 -1
- package/package.json +37 -38
- package/sbom.json +7902 -1
- package/server.js +784 -518
- package/static/js/ace/ace.js +1 -1
- package/static/licenses.html +225 -1060
- package/translations/messages.pot +62 -63
- package/views/config/ai.hbs +1 -1
- package/views/config/security.hbs +24 -2
- package/views/config/service.hbs +47 -0
- package/views/config/webhooks.hbs +36 -0
- package/views/internals/index.hbs +8 -0
- package/views/partials/queue_retention_note.hbs +12 -8
- package/views/partials/ui/env-managed-alert.hbs +16 -7
- package/views/partials/webhook_form.hbs +1 -1
- package/views/suppression-lists/index.hbs +1 -1
- package/workers/api.js +840 -611
- package/workers/export.js +17 -632
- package/workers/imap.js +13 -0
- package/workers/smtp.js +9 -11
- package/workers/submit.js +1 -1
- package/workers/webhooks.js +18 -448
- package/lib/email-client/outlook-fallback-poll.js +0 -30
|
@@ -4,6 +4,7 @@ const { BaseClient, metricsMeta } = require('./base-client');
|
|
|
4
4
|
const { isSafePathSegment } = require('../utils/provider-id');
|
|
5
5
|
const { isTransientCredentialError } = require('./credential-errors');
|
|
6
6
|
const { Account } = require('../account');
|
|
7
|
+
const { runLocked, isContention } = require('../lock-run');
|
|
7
8
|
const { checkAccountScopes } = require('../oauth/scope-checker');
|
|
8
9
|
const settings = require('../settings');
|
|
9
10
|
const { oauth2Apps } = require('../oauth2-apps');
|
|
@@ -11,7 +12,7 @@ const getSecret = require('../get-secret');
|
|
|
11
12
|
const msgpack = require('../msgpack');
|
|
12
13
|
const he = require('he');
|
|
13
14
|
const { emitChangeEvent, prepareUrl, collectCidReferences, embedCidImages, truncateTextContents } = require('../tools');
|
|
14
|
-
const { parseFallbackPollInterval, FALLBACK_POLL_INTERVAL } = require('./
|
|
15
|
+
const { parseFallbackPollInterval, OUTLOOK_FALLBACK_POLL_INTERVAL: FALLBACK_POLL_INTERVAL } = require('./fallback-poll');
|
|
15
16
|
const { messageWebSafeHtml, applyWebSafeHtmlOptions } = require('../web-safe-html');
|
|
16
17
|
const crypto = require('crypto');
|
|
17
18
|
const { Gateway } = require('../gateway');
|
|
@@ -36,6 +37,7 @@ const {
|
|
|
36
37
|
OUTLOOK_CLOCK_SKEW_BUFFER,
|
|
37
38
|
OUTLOOK_MISSED_LOOKBACK,
|
|
38
39
|
OUTLOOK_ANNOUNCED_RETENTION,
|
|
40
|
+
OUTLOOK_SYNC_MAX_LOOKBACK,
|
|
39
41
|
MESSAGE_UPDATED_NOTIFY,
|
|
40
42
|
MESSAGE_DELETED_NOTIFY,
|
|
41
43
|
MESSAGE_MISSING_NOTIFY,
|
|
@@ -54,6 +56,22 @@ const RENEW_WATCH_TTL = 60 * 60 * 1000; // 1h
|
|
|
54
56
|
// unannounced ones and announce hours of mail again
|
|
55
57
|
const ANNOUNCED_SINCE_FIELD = 'outlookAnnouncedInSince';
|
|
56
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Where the recovery pass after a flush starts: the flush's `notifyFrom` when it lies in the past,
|
|
61
|
+
* no further back than "Run sync" may look. Null when there is nothing before now to recover
|
|
62
|
+
* @param {string|Date} [notifyFrom] - Stored notifyFrom value
|
|
63
|
+
* @param {number} now - Epoch ms
|
|
64
|
+
* @returns {number|null} Epoch ms
|
|
65
|
+
*/
|
|
66
|
+
function flushRecoveryStart(notifyFrom, now) {
|
|
67
|
+
const time = notifyFrom ? new Date(notifyFrom).getTime() : NaN;
|
|
68
|
+
// a minute of slack, so the default flush ("now", stored a few seconds ago) needs no pass
|
|
69
|
+
if (!Number.isFinite(time) || time >= now - 60 * 1000) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return Math.max(time, now - OUTLOOK_SYNC_MAX_LOOKBACK);
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
// The `serverResponseCode` a failed change subscription is reported with, and the marker that
|
|
58
76
|
// tells the report apart from anything else the account may have stored. Only this client reads
|
|
59
77
|
// it: reportSubscriptionFailure() writes it, clearSubscriptionFailure() lifts it, and
|
|
@@ -569,10 +587,25 @@ class OutlookClient extends BaseClient {
|
|
|
569
587
|
}
|
|
570
588
|
|
|
571
589
|
/**
|
|
572
|
-
*
|
|
590
|
+
* Deletes the account's Graph subscription. Best effort: Graph lets an orphaned subscription
|
|
591
|
+
* expire within three days on its own, it only keeps posting to the notification endpoint and
|
|
592
|
+
* counting against the tenant's subscription quota until then
|
|
593
|
+
* @returns {Promise<boolean>} True when a subscription was deleted
|
|
573
594
|
*/
|
|
574
|
-
async
|
|
575
|
-
|
|
595
|
+
async releaseRemoteResources() {
|
|
596
|
+
try {
|
|
597
|
+
const outlookSubscription = await this.getStoredSubscription();
|
|
598
|
+
if (!outlookSubscription.id) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
await this.request(`/subscriptions/${outlookSubscription.id}`, 'DELETE');
|
|
602
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
603
|
+
this.logger.info({ msg: 'Deleted MS Graph subscription', account: this.account, subscription: outlookSubscription.id });
|
|
604
|
+
return true;
|
|
605
|
+
} catch (err) {
|
|
606
|
+
this.logger.warn({ msg: 'Failed to delete MS Graph subscription', account: this.account, err });
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
576
609
|
}
|
|
577
610
|
|
|
578
611
|
/**
|
|
@@ -582,15 +615,9 @@ class OutlookClient extends BaseClient {
|
|
|
582
615
|
this.clearTimers();
|
|
583
616
|
this.closed = true;
|
|
584
617
|
|
|
585
|
-
//
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
if (outlookSubscription.id) {
|
|
589
|
-
await this.request(`/subscriptions/${outlookSubscription.id}`, 'DELETE');
|
|
590
|
-
}
|
|
591
|
-
} catch (err) {
|
|
592
|
-
this.logger.warn({ msg: 'Failed to delete MS Graph subscription', account: this.account, err });
|
|
593
|
-
}
|
|
618
|
+
// Normally done already by the teardown that runs before the account record is removed,
|
|
619
|
+
// and then a no-op here
|
|
620
|
+
await this.releaseRemoteResources();
|
|
594
621
|
|
|
595
622
|
if (['init', 'connecting', 'syncing', 'connected'].includes(this.state)) {
|
|
596
623
|
this.state = 'disconnected';
|
|
@@ -601,6 +628,23 @@ class OutlookClient extends BaseClient {
|
|
|
601
628
|
return null;
|
|
602
629
|
}
|
|
603
630
|
|
|
631
|
+
/**
|
|
632
|
+
* Starts syncing again after a flush. The subscription only reports changes made from now on,
|
|
633
|
+
* so a flush that asked for messages from an earlier `notifyFrom` also gets the pass "Run sync"
|
|
634
|
+
* makes, from that point and bounded the same way. A flush clears the announced set, so the
|
|
635
|
+
* messages it finds are announced again, as on an IMAP account
|
|
636
|
+
*/
|
|
637
|
+
async resumeAfterFlush() {
|
|
638
|
+
await this.reconnect();
|
|
639
|
+
|
|
640
|
+
// the one field this needs, read on its own: reconnect() has just loaded the whole account
|
|
641
|
+
const notifyFrom = await this.redis.hget(this.getAccountKey(), 'notifyFrom');
|
|
642
|
+
const since = flushRecoveryStart(notifyFrom, Date.now());
|
|
643
|
+
if (since && !this.closed && this.state === 'connected') {
|
|
644
|
+
await this.syncMailboxes({ since });
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
604
648
|
/**
|
|
605
649
|
* Reconnect to the account (re-initialize)
|
|
606
650
|
*/
|
|
@@ -3535,183 +3579,163 @@ class OutlookClient extends BaseClient {
|
|
|
3535
3579
|
return { success: false, reason: 'renewal_in_progress' };
|
|
3536
3580
|
}
|
|
3537
3581
|
|
|
3538
|
-
const
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
if (!skipLock) {
|
|
3542
|
-
try {
|
|
3543
|
-
renewLock = await lock.acquireLock(lockId, OUTLOOK_SUBSCRIPTION_LOCK_TTL);
|
|
3544
|
-
} catch (err) {
|
|
3545
|
-
this.logger.error({ msg: 'Failed to acquire subscription renew lock', account: this.account, err });
|
|
3546
|
-
this.scheduleSubscriptionRetry({
|
|
3547
|
-
delayMs: OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * 1000,
|
|
3548
|
-
reason: 'lock_error',
|
|
3549
|
-
errorMsg: 'Subscription renew retry after lock error failed',
|
|
3550
|
-
fn: () => this.renewSubscription({ force: false })
|
|
3551
|
-
});
|
|
3552
|
-
return { success: false, reason: 'lock_error' };
|
|
3553
|
-
}
|
|
3554
|
-
|
|
3555
|
-
if (!renewLock.success) {
|
|
3556
|
-
this.logger.info({
|
|
3557
|
-
msg: 'Subscription renewal skipped',
|
|
3558
|
-
reason: 'lock_exists',
|
|
3559
|
-
account: this.account
|
|
3560
|
-
});
|
|
3561
|
-
return { success: false, reason: 'lock_exists' };
|
|
3562
|
-
}
|
|
3563
|
-
}
|
|
3582
|
+
const renew = async () => {
|
|
3583
|
+
// Set in-memory flag to prevent concurrent calls
|
|
3584
|
+
this.renewalInProgress = true;
|
|
3564
3585
|
|
|
3565
|
-
|
|
3566
|
-
|
|
3586
|
+
try {
|
|
3587
|
+
// Get current subscription
|
|
3588
|
+
let outlookSubscription = await this.getStoredSubscription();
|
|
3567
3589
|
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3590
|
+
// Check if subscription exists and is valid
|
|
3591
|
+
if (!outlookSubscription.id) {
|
|
3592
|
+
this.subscriptionState = 'unset';
|
|
3593
|
+
return { success: false, reason: 'no_subscription' };
|
|
3594
|
+
}
|
|
3571
3595
|
|
|
3572
|
-
|
|
3573
|
-
if (!outlookSubscription.id) {
|
|
3574
|
-
this.subscriptionState = 'unset';
|
|
3575
|
-
return { success: false, reason: 'no_subscription' };
|
|
3576
|
-
}
|
|
3596
|
+
let existingExpirationDateTime = this.parseExpirationDate(outlookSubscription.expirationDateTime);
|
|
3577
3597
|
|
|
3578
|
-
|
|
3598
|
+
const now = Date.now();
|
|
3579
3599
|
|
|
3580
|
-
|
|
3600
|
+
// Check if already expired (with buffer for clock skew)
|
|
3601
|
+
if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_CLOCK_SKEW_BUFFER) {
|
|
3602
|
+
this.logger.warn({
|
|
3603
|
+
msg: 'Subscription already expired or expiring imminently',
|
|
3604
|
+
subscriptionId: outlookSubscription.id,
|
|
3605
|
+
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3606
|
+
account: this.account,
|
|
3607
|
+
msUntilExpiry: existingExpirationDateTime.getTime() - now
|
|
3608
|
+
});
|
|
3609
|
+
// Clear the expired subscription
|
|
3610
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3611
|
+
this.subscriptionState = 'expired';
|
|
3612
|
+
return { success: false, reason: 'expired' };
|
|
3613
|
+
}
|
|
3581
3614
|
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
this.logger.warn({
|
|
3585
|
-
msg: 'Subscription already expired or expiring imminently',
|
|
3586
|
-
subscriptionId: outlookSubscription.id,
|
|
3587
|
-
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3588
|
-
account: this.account,
|
|
3589
|
-
msUntilExpiry: existingExpirationDateTime.getTime() - now
|
|
3590
|
-
});
|
|
3591
|
-
// Clear the expired subscription
|
|
3592
|
-
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3593
|
-
this.subscriptionState = 'expired';
|
|
3594
|
-
return { success: false, reason: 'expired' };
|
|
3595
|
-
}
|
|
3615
|
+
// Check if renewal is needed
|
|
3616
|
+
const shouldRenew = force || (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME);
|
|
3596
3617
|
|
|
3597
|
-
|
|
3598
|
-
|
|
3618
|
+
if (!shouldRenew) {
|
|
3619
|
+
this.logger.debug({
|
|
3620
|
+
msg: 'Subscription renewal not needed',
|
|
3621
|
+
subscriptionId: outlookSubscription.id,
|
|
3622
|
+
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3623
|
+
account: this.account
|
|
3624
|
+
});
|
|
3625
|
+
this.subscriptionState = 'valid';
|
|
3626
|
+
// A subscription that is still good is the same recovery as one just renewed
|
|
3627
|
+
await this.clearSubscriptionFailure();
|
|
3628
|
+
return { success: true, reason: 'not_needed' };
|
|
3629
|
+
}
|
|
3599
3630
|
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
account: this.account
|
|
3606
|
-
});
|
|
3607
|
-
this.subscriptionState = 'valid';
|
|
3608
|
-
// A subscription that is still good is the same recovery as one just renewed
|
|
3609
|
-
await this.clearSubscriptionFailure();
|
|
3610
|
-
return { success: true, reason: 'not_needed' };
|
|
3611
|
-
}
|
|
3631
|
+
// Perform renewal
|
|
3632
|
+
const expirationDateTime = new Date(now + OUTLOOK_EXPIRATION_TIME);
|
|
3633
|
+
const subscriptionPayload = {
|
|
3634
|
+
expirationDateTime: expirationDateTime.toISOString()
|
|
3635
|
+
};
|
|
3612
3636
|
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
expirationDateTime: expirationDateTime.toISOString()
|
|
3617
|
-
};
|
|
3637
|
+
// Capture previous retry counts before overwriting state
|
|
3638
|
+
const previousRetryCount = outlookSubscription.state?.retryCount || 0;
|
|
3639
|
+
const previousCreateRetryCount = outlookSubscription.state?.createRetryCount || 0;
|
|
3618
3640
|
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3641
|
+
// Update state to renewing
|
|
3642
|
+
outlookSubscription.state = {
|
|
3643
|
+
state: 'renewing',
|
|
3644
|
+
time: Date.now()
|
|
3645
|
+
};
|
|
3646
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3647
|
+
this.subscriptionState = 'pending';
|
|
3622
3648
|
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
state: 'renewing',
|
|
3626
|
-
time: Date.now()
|
|
3627
|
-
};
|
|
3628
|
-
await this.saveStoredSubscription(outlookSubscription);
|
|
3629
|
-
this.subscriptionState = 'pending';
|
|
3649
|
+
try {
|
|
3650
|
+
const subscriptionRes = await this.request(`/subscriptions/${outlookSubscription.id}`, 'PATCH', subscriptionPayload);
|
|
3630
3651
|
|
|
3631
|
-
|
|
3632
|
-
|
|
3652
|
+
if (subscriptionRes?.expirationDateTime) {
|
|
3653
|
+
outlookSubscription.expirationDateTime = subscriptionRes.expirationDateTime;
|
|
3654
|
+
outlookSubscription.state = {
|
|
3655
|
+
state: 'created',
|
|
3656
|
+
time: Date.now(),
|
|
3657
|
+
retryCount: 0,
|
|
3658
|
+
createRetryCount: 0,
|
|
3659
|
+
error: null
|
|
3660
|
+
};
|
|
3633
3661
|
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
state: 'created',
|
|
3638
|
-
time: Date.now(),
|
|
3639
|
-
retryCount: 0,
|
|
3640
|
-
createRetryCount: 0,
|
|
3641
|
-
error: null
|
|
3642
|
-
};
|
|
3662
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3663
|
+
this.subscriptionState = 'valid';
|
|
3664
|
+
await this.clearSubscriptionFailure();
|
|
3643
3665
|
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3666
|
+
this.logger.info({
|
|
3667
|
+
msg: previousRetryCount > 0 ? 'Subscription renewed successfully after retry' : 'Subscription renewed successfully',
|
|
3668
|
+
subscriptionId: outlookSubscription.id,
|
|
3669
|
+
newExpirationDateTime: subscriptionRes.expirationDateTime,
|
|
3670
|
+
account: this.account,
|
|
3671
|
+
previousRetryCount
|
|
3672
|
+
});
|
|
3647
3673
|
|
|
3648
|
-
|
|
3649
|
-
|
|
3674
|
+
return { success: true, expirationDateTime: subscriptionRes.expirationDateTime };
|
|
3675
|
+
} else {
|
|
3676
|
+
throw new Error('Empty server response');
|
|
3677
|
+
}
|
|
3678
|
+
} catch (err) {
|
|
3679
|
+
this.logger.warn({
|
|
3680
|
+
msg: 'Subscription renewal failed',
|
|
3650
3681
|
subscriptionId: outlookSubscription.id,
|
|
3651
|
-
newExpirationDateTime: subscriptionRes.expirationDateTime,
|
|
3652
3682
|
account: this.account,
|
|
3653
|
-
|
|
3683
|
+
err
|
|
3654
3684
|
});
|
|
3655
3685
|
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
err
|
|
3666
|
-
});
|
|
3667
|
-
|
|
3668
|
-
outlookSubscription.state = {
|
|
3669
|
-
state: 'error',
|
|
3670
|
-
error: `Subscription renewal failed: ${err.oauthRequest?.response?.error?.message || err.message}`,
|
|
3671
|
-
time: Date.now(),
|
|
3672
|
-
retryCount: previousRetryCount + 1,
|
|
3673
|
-
createRetryCount: previousCreateRetryCount
|
|
3674
|
-
};
|
|
3675
|
-
await this.saveStoredSubscription(outlookSubscription);
|
|
3676
|
-
this.subscriptionState = 'failed';
|
|
3686
|
+
outlookSubscription.state = {
|
|
3687
|
+
state: 'error',
|
|
3688
|
+
error: `Subscription renewal failed: ${err.oauthRequest?.response?.error?.message || err.message}`,
|
|
3689
|
+
time: Date.now(),
|
|
3690
|
+
retryCount: previousRetryCount + 1,
|
|
3691
|
+
createRetryCount: previousCreateRetryCount
|
|
3692
|
+
};
|
|
3693
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3694
|
+
this.subscriptionState = 'failed';
|
|
3677
3695
|
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3696
|
+
// Schedule retry with exponential backoff
|
|
3697
|
+
const retryCount = outlookSubscription.state.retryCount;
|
|
3698
|
+
if (retryCount < OUTLOOK_MAX_RETRY_ATTEMPTS) {
|
|
3699
|
+
const retryDelay =
|
|
3700
|
+
Math.min(OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * Math.pow(2, retryCount - 1), OUTLOOK_SUBSCRIPTION_RETRY_MAX_DELAY) * 1000;
|
|
3683
3701
|
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
|
|
3702
|
+
this.scheduleSubscriptionRetry({
|
|
3703
|
+
delayMs: retryDelay,
|
|
3704
|
+
reason: 'renewal_failure',
|
|
3705
|
+
errorMsg: 'Subscription renewal retry failed',
|
|
3706
|
+
fn: () => this.renewSubscription({ force }),
|
|
3707
|
+
retryAttempt: retryCount
|
|
3708
|
+
});
|
|
3691
3709
|
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3710
|
+
this.logger.info({
|
|
3711
|
+
msg: 'Scheduling subscription renewal retry',
|
|
3712
|
+
account: this.account,
|
|
3713
|
+
retryAttempt: retryCount,
|
|
3714
|
+
retryDelayMs: retryDelay
|
|
3715
|
+
});
|
|
3716
|
+
} else {
|
|
3717
|
+
await this.reportSubscriptionFailure('renewal', outlookSubscription.state, retryCount);
|
|
3718
|
+
}
|
|
3701
3719
|
|
|
3702
|
-
|
|
3703
|
-
}
|
|
3704
|
-
} finally {
|
|
3705
|
-
// Always release the lock and clear in-memory flag
|
|
3706
|
-
this.renewalInProgress = false;
|
|
3707
|
-
if (!skipLock && renewLock?.success) {
|
|
3708
|
-
try {
|
|
3709
|
-
await lock.releaseLock(renewLock);
|
|
3710
|
-
} catch (err) {
|
|
3711
|
-
this.logger.error({ msg: 'Failed to release subscription renew lock', account: this.account, err });
|
|
3720
|
+
return { success: false, reason: 'renewal_failed', error: err.message };
|
|
3712
3721
|
}
|
|
3722
|
+
} finally {
|
|
3723
|
+
this.renewalInProgress = false;
|
|
3713
3724
|
}
|
|
3725
|
+
};
|
|
3726
|
+
|
|
3727
|
+
if (skipLock) {
|
|
3728
|
+
return await renew();
|
|
3714
3729
|
}
|
|
3730
|
+
|
|
3731
|
+
// One try at the lock: a renewal already running elsewhere is left to finish
|
|
3732
|
+
return await this.withSubscriptionLock(renew, {
|
|
3733
|
+
action: 'renew',
|
|
3734
|
+
skippedMsg: 'Subscription renewal skipped',
|
|
3735
|
+
retry: () => this.renewSubscription({ force: false }),
|
|
3736
|
+
skipped: { success: false, reason: 'lock_exists' },
|
|
3737
|
+
failed: { success: false, reason: 'lock_error' }
|
|
3738
|
+
});
|
|
3715
3739
|
}
|
|
3716
3740
|
|
|
3717
3741
|
/**
|
|
@@ -3741,6 +3765,35 @@ class OutlookClient extends BaseClient {
|
|
|
3741
3765
|
}
|
|
3742
3766
|
}
|
|
3743
3767
|
|
|
3768
|
+
/**
|
|
3769
|
+
* Runs fn under the account's subscription lock, with one try at it: a renewal or creation
|
|
3770
|
+
* already running elsewhere is left to finish (answered with `skipped`), and a lock that could
|
|
3771
|
+
* not be taken at all schedules `retry` (answered with `failed`)
|
|
3772
|
+
* @param {Function} fn
|
|
3773
|
+
* @param {Object} opts
|
|
3774
|
+
* @param {string} opts.action - 'renew' or 'ensure', for the log
|
|
3775
|
+
* @param {string} opts.skippedMsg - Log message when another holder has the lock
|
|
3776
|
+
* @param {Function} opts.retry - What to retry after a lock error
|
|
3777
|
+
* @param {*} [opts.skipped] - Result when another holder has the lock
|
|
3778
|
+
* @param {*} [opts.failed] - Result when the lock could not be taken
|
|
3779
|
+
*/
|
|
3780
|
+
async withSubscriptionLock(fn, { action, skippedMsg, retry, skipped, failed }) {
|
|
3781
|
+
return await runLocked(this.accountObject.getLock(), this.getSubscriptionLockId(), { ttl: OUTLOOK_SUBSCRIPTION_LOCK_TTL, wait: false }, fn, err => {
|
|
3782
|
+
if (isContention(err)) {
|
|
3783
|
+
this.logger.info({ msg: skippedMsg, reason: 'lock_exists', account: this.account });
|
|
3784
|
+
return skipped;
|
|
3785
|
+
}
|
|
3786
|
+
this.logger.error({ msg: `Failed to acquire subscription ${action} lock`, account: this.account, err });
|
|
3787
|
+
this.scheduleSubscriptionRetry({
|
|
3788
|
+
delayMs: OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * 1000,
|
|
3789
|
+
reason: 'lock_error',
|
|
3790
|
+
errorMsg: `Subscription ${action} retry after lock error failed`,
|
|
3791
|
+
fn: retry
|
|
3792
|
+
});
|
|
3793
|
+
return failed;
|
|
3794
|
+
});
|
|
3795
|
+
}
|
|
3796
|
+
|
|
3744
3797
|
/**
|
|
3745
3798
|
* Ensure webhook subscription exists and is valid
|
|
3746
3799
|
* Creates new subscription or renews existing one
|
|
@@ -3750,8 +3803,9 @@ class OutlookClient extends BaseClient {
|
|
|
3750
3803
|
return;
|
|
3751
3804
|
}
|
|
3752
3805
|
|
|
3753
|
-
|
|
3754
|
-
let
|
|
3806
|
+
const urls = await settings.getMulti('serviceUrl', 'notificationBaseUrl');
|
|
3807
|
+
let serviceUrl = urls.serviceUrl || null;
|
|
3808
|
+
let notificationBaseUrl = urls.notificationBaseUrl || serviceUrl || '';
|
|
3755
3809
|
|
|
3756
3810
|
if (!serviceUrl) {
|
|
3757
3811
|
this.logger.error({ msg: 'Cannot create MS Graph subscription, service URL is not set', account: this.account });
|
|
@@ -3759,201 +3813,181 @@ class OutlookClient extends BaseClient {
|
|
|
3759
3813
|
return false;
|
|
3760
3814
|
}
|
|
3761
3815
|
|
|
3762
|
-
//
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3816
|
+
// A creation already running elsewhere is left to finish. Do not forward clearExisting on a
|
|
3817
|
+
// retry after a lock error: by the time it fires, another path may have created a valid
|
|
3818
|
+
// subscription that should not be cleared
|
|
3819
|
+
return await this.withSubscriptionLock(() => this.ensureSubscriptionLocked({ clearExisting, serviceUrl, notificationBaseUrl }), {
|
|
3820
|
+
action: 'ensure',
|
|
3821
|
+
skippedMsg: 'Subscription creation skipped',
|
|
3822
|
+
retry: () => this.ensureSubscription()
|
|
3823
|
+
});
|
|
3824
|
+
}
|
|
3825
|
+
|
|
3826
|
+
/**
|
|
3827
|
+
* The part of ensureSubscription() that runs under the subscription lock
|
|
3828
|
+
* @param {Object} opts
|
|
3829
|
+
* @param {boolean} opts.clearExisting
|
|
3830
|
+
* @param {string|null} opts.serviceUrl
|
|
3831
|
+
* @param {string} opts.notificationBaseUrl
|
|
3832
|
+
*/
|
|
3833
|
+
async ensureSubscriptionLocked({ clearExisting, serviceUrl, notificationBaseUrl }) {
|
|
3834
|
+
if (clearExisting) {
|
|
3835
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3782
3836
|
this.logger.info({
|
|
3783
|
-
msg: '
|
|
3784
|
-
reason: 'lock_exists',
|
|
3837
|
+
msg: 'Cleared existing subscription data for re-creation',
|
|
3785
3838
|
account: this.account
|
|
3786
3839
|
});
|
|
3787
|
-
return;
|
|
3788
3840
|
}
|
|
3789
3841
|
|
|
3790
|
-
|
|
3791
|
-
if (clearExisting) {
|
|
3792
|
-
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3793
|
-
this.logger.info({
|
|
3794
|
-
msg: 'Cleared existing subscription data for re-creation',
|
|
3795
|
-
account: this.account
|
|
3796
|
-
});
|
|
3797
|
-
}
|
|
3842
|
+
let outlookSubscription = await this.getStoredSubscription();
|
|
3798
3843
|
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
account: this.account
|
|
3810
|
-
});
|
|
3844
|
+
// If state is creating/renewing, the previous lock holder crashed before completing.
|
|
3845
|
+
// Since we hold the lock, no other operation is in progress — reset to a clean state.
|
|
3846
|
+
if (['creating', 'renewing'].includes(outlookSubscription.state?.state)) {
|
|
3847
|
+
this.logger.warn({
|
|
3848
|
+
msg: 'Found stale subscription state from crashed operation, resetting state',
|
|
3849
|
+
subscriptionId: outlookSubscription?.id,
|
|
3850
|
+
state: outlookSubscription.state?.state,
|
|
3851
|
+
stateTime: outlookSubscription.state?.time,
|
|
3852
|
+
account: this.account
|
|
3853
|
+
});
|
|
3811
3854
|
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3855
|
+
// Reset transient state so downstream logic sees a clean state
|
|
3856
|
+
outlookSubscription.state.state = outlookSubscription.id ? 'created' : 'error';
|
|
3857
|
+
outlookSubscription.state.time = Date.now();
|
|
3858
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3859
|
+
}
|
|
3817
3860
|
|
|
3818
|
-
|
|
3819
|
-
|
|
3861
|
+
let now = Date.now();
|
|
3862
|
+
let expirationDateTime = new Date(now + OUTLOOK_EXPIRATION_TIME);
|
|
3820
3863
|
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3864
|
+
// Renew existing subscription if needed
|
|
3865
|
+
if (outlookSubscription.id) {
|
|
3866
|
+
let existingExpirationDateTime = this.parseExpirationDate(outlookSubscription.expirationDateTime);
|
|
3824
3867
|
|
|
3825
|
-
|
|
3826
|
-
|
|
3868
|
+
if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_CLOCK_SKEW_BUFFER) {
|
|
3869
|
+
// already expired (with buffer for clock skew)
|
|
3870
|
+
outlookSubscription = {};
|
|
3871
|
+
} else if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME) {
|
|
3872
|
+
// Use the unified renewal method (skip lock since we already hold it)
|
|
3873
|
+
const renewalResult = await this.renewSubscription({ force: false, skipLock: true });
|
|
3874
|
+
if (!renewalResult.success && renewalResult.reason === 'expired') {
|
|
3875
|
+
// Subscription expired, clear it to create a new one
|
|
3827
3876
|
outlookSubscription = {};
|
|
3828
|
-
} else if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME) {
|
|
3829
|
-
// Use the unified renewal method (skip lock since we already hold it)
|
|
3830
|
-
const renewalResult = await this.renewSubscription({ force: false, skipLock: true });
|
|
3831
|
-
if (!renewalResult.success && renewalResult.reason === 'expired') {
|
|
3832
|
-
// Subscription expired, clear it to create a new one
|
|
3833
|
-
outlookSubscription = {};
|
|
3834
|
-
} else {
|
|
3835
|
-
return renewalResult.success;
|
|
3836
|
-
}
|
|
3837
3877
|
} else {
|
|
3838
|
-
|
|
3839
|
-
this.logger.debug({
|
|
3840
|
-
msg: 'Subscription renewal skipped',
|
|
3841
|
-
reason: 'valid',
|
|
3842
|
-
subscriptionId: outlookSubscription?.id,
|
|
3843
|
-
state: outlookSubscription.state?.state,
|
|
3844
|
-
created: outlookSubscription.state?.time,
|
|
3845
|
-
expirationDateTime: outlookSubscription.expirationDateTime
|
|
3846
|
-
});
|
|
3847
|
-
this.subscriptionState = 'valid';
|
|
3848
|
-
// A subscription that is still good is the same recovery as one just created
|
|
3849
|
-
await this.clearSubscriptionFailure();
|
|
3850
|
-
return;
|
|
3878
|
+
return renewalResult.success;
|
|
3851
3879
|
}
|
|
3880
|
+
} else {
|
|
3881
|
+
// Subscription is valid, do nothing
|
|
3882
|
+
this.logger.debug({
|
|
3883
|
+
msg: 'Subscription renewal skipped',
|
|
3884
|
+
reason: 'valid',
|
|
3885
|
+
subscriptionId: outlookSubscription?.id,
|
|
3886
|
+
state: outlookSubscription.state?.state,
|
|
3887
|
+
created: outlookSubscription.state?.time,
|
|
3888
|
+
expirationDateTime: outlookSubscription.expirationDateTime
|
|
3889
|
+
});
|
|
3890
|
+
this.subscriptionState = 'valid';
|
|
3891
|
+
// A subscription that is still good is the same recovery as one just created
|
|
3892
|
+
await this.clearSubscriptionFailure();
|
|
3893
|
+
return;
|
|
3852
3894
|
}
|
|
3895
|
+
}
|
|
3853
3896
|
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3897
|
+
// Create new subscription if needed
|
|
3898
|
+
if (!outlookSubscription.id) {
|
|
3899
|
+
const notificationUrl = prepareUrl('/oauth/msg/notification', notificationBaseUrl, {
|
|
3900
|
+
account: this.account
|
|
3901
|
+
});
|
|
3859
3902
|
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3903
|
+
const lifecycleNotificationUrl = prepareUrl('/oauth/msg/lifecycle', notificationBaseUrl, {
|
|
3904
|
+
account: this.account
|
|
3905
|
+
});
|
|
3863
3906
|
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3907
|
+
const subscriptionPayload = {
|
|
3908
|
+
changeType: 'created,updated,deleted',
|
|
3909
|
+
notificationUrl,
|
|
3910
|
+
lifecycleNotificationUrl,
|
|
3911
|
+
resource: `/${this.oauth2UserPath}/messages`,
|
|
3912
|
+
expirationDateTime: expirationDateTime.toISOString(),
|
|
3913
|
+
clientState: crypto.randomUUID()
|
|
3914
|
+
};
|
|
3872
3915
|
|
|
3873
|
-
|
|
3874
|
-
|
|
3916
|
+
// Capture previous create retry count before overwriting state
|
|
3917
|
+
const previousCreateRetryCount = outlookSubscription.state?.createRetryCount || 0;
|
|
3875
3918
|
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3919
|
+
outlookSubscription.state = {
|
|
3920
|
+
state: 'creating',
|
|
3921
|
+
time: Date.now()
|
|
3922
|
+
};
|
|
3923
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3924
|
+
this.subscriptionState = 'pending';
|
|
3882
3925
|
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
};
|
|
3898
|
-
this.subscriptionState = 'valid';
|
|
3899
|
-
await this.clearSubscriptionFailure();
|
|
3900
|
-
} else {
|
|
3901
|
-
throw new Error('Empty server response');
|
|
3902
|
-
}
|
|
3903
|
-
} catch (err) {
|
|
3904
|
-
const errorMessage = err.oauthRequest?.response?.error?.message || err.message;
|
|
3905
|
-
this.logger.warn({
|
|
3906
|
-
msg: 'Failed to create MS Graph subscription',
|
|
3907
|
-
account: this.account,
|
|
3908
|
-
error: errorMessage,
|
|
3909
|
-
code: err.oauthRequest?.response?.error?.code,
|
|
3910
|
-
statusCode: err.oauthRequest?.status
|
|
3911
|
-
});
|
|
3912
|
-
outlookSubscription.state = {
|
|
3913
|
-
state: 'error',
|
|
3914
|
-
error: `Subscription failed: ${errorMessage}`,
|
|
3915
|
-
time: Date.now(),
|
|
3916
|
-
createRetryCount: previousCreateRetryCount + 1
|
|
3926
|
+
let subscriptionRes;
|
|
3927
|
+
try {
|
|
3928
|
+
subscriptionRes = await this.request(`/subscriptions`, 'post', subscriptionPayload);
|
|
3929
|
+
if (subscriptionRes?.expirationDateTime) {
|
|
3930
|
+
outlookSubscription = {
|
|
3931
|
+
id: subscriptionRes.id,
|
|
3932
|
+
expirationDateTime: subscriptionRes.expirationDateTime,
|
|
3933
|
+
clientState: subscriptionRes.clientState,
|
|
3934
|
+
state: {
|
|
3935
|
+
state: 'created',
|
|
3936
|
+
time: Date.now(),
|
|
3937
|
+
createRetryCount: 0,
|
|
3938
|
+
retryCount: 0
|
|
3939
|
+
}
|
|
3917
3940
|
};
|
|
3918
|
-
this.subscriptionState = '
|
|
3919
|
-
|
|
3920
|
-
|
|
3941
|
+
this.subscriptionState = 'valid';
|
|
3942
|
+
await this.clearSubscriptionFailure();
|
|
3943
|
+
} else {
|
|
3944
|
+
throw new Error('Empty server response');
|
|
3921
3945
|
}
|
|
3946
|
+
} catch (err) {
|
|
3947
|
+
const errorMessage = err.oauthRequest?.response?.error?.message || err.message;
|
|
3948
|
+
this.logger.warn({
|
|
3949
|
+
msg: 'Failed to create MS Graph subscription',
|
|
3950
|
+
account: this.account,
|
|
3951
|
+
error: errorMessage,
|
|
3952
|
+
code: err.oauthRequest?.response?.error?.code,
|
|
3953
|
+
statusCode: err.oauthRequest?.status
|
|
3954
|
+
});
|
|
3955
|
+
outlookSubscription.state = {
|
|
3956
|
+
state: 'error',
|
|
3957
|
+
error: `Subscription failed: ${errorMessage}`,
|
|
3958
|
+
time: Date.now(),
|
|
3959
|
+
createRetryCount: previousCreateRetryCount + 1
|
|
3960
|
+
};
|
|
3961
|
+
this.subscriptionState = 'failed';
|
|
3962
|
+
} finally {
|
|
3963
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3964
|
+
}
|
|
3922
3965
|
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3966
|
+
// Schedule retry with exponential backoff if creation failed
|
|
3967
|
+
if (outlookSubscription.state?.state === 'error') {
|
|
3968
|
+
const createRetryCount = outlookSubscription.state.createRetryCount || 0;
|
|
3969
|
+
if (createRetryCount < OUTLOOK_MAX_RETRY_ATTEMPTS) {
|
|
3970
|
+
const retryDelay =
|
|
3971
|
+
Math.min(OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * Math.pow(2, createRetryCount - 1), OUTLOOK_SUBSCRIPTION_RETRY_MAX_DELAY) * 1000;
|
|
3929
3972
|
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3973
|
+
this.scheduleSubscriptionRetry({
|
|
3974
|
+
delayMs: retryDelay,
|
|
3975
|
+
reason: 'creation_failure',
|
|
3976
|
+
errorMsg: 'Subscription creation retry failed',
|
|
3977
|
+
fn: () => this.ensureSubscription(),
|
|
3978
|
+
retryAttempt: createRetryCount
|
|
3979
|
+
});
|
|
3937
3980
|
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
}
|
|
3949
|
-
}
|
|
3950
|
-
}
|
|
3951
|
-
} finally {
|
|
3952
|
-
if (ensureLock?.success) {
|
|
3953
|
-
try {
|
|
3954
|
-
await lock.releaseLock(ensureLock);
|
|
3955
|
-
} catch (err) {
|
|
3956
|
-
this.logger.error({ msg: 'Failed to release subscription ensure lock', account: this.account, err });
|
|
3981
|
+
this.logger.info({
|
|
3982
|
+
msg: 'Scheduling subscription creation retry',
|
|
3983
|
+
account: this.account,
|
|
3984
|
+
retryAttempt: createRetryCount,
|
|
3985
|
+
retryDelayMs: retryDelay
|
|
3986
|
+
});
|
|
3987
|
+
} else {
|
|
3988
|
+
// End of the fast ladder, not the end of trying: renewOrCreateSubscription()
|
|
3989
|
+
// clears the counters and starts a new one on the next hourly pass
|
|
3990
|
+
await this.reportSubscriptionFailure('creation', outlookSubscription.state, createRetryCount);
|
|
3957
3991
|
}
|
|
3958
3992
|
}
|
|
3959
3993
|
}
|
|
@@ -4322,7 +4356,7 @@ class OutlookClient extends BaseClient {
|
|
|
4322
4356
|
|
|
4323
4357
|
async _updateLastNotificationTime() {
|
|
4324
4358
|
try {
|
|
4325
|
-
await this.redis.
|
|
4359
|
+
await this.redis.hSetExists(this.getAccountKey(), 'outlookLastNotification', Date.now().toString());
|
|
4326
4360
|
} catch (err) {
|
|
4327
4361
|
this.logger.error({ msg: 'Failed to update last notification time', err });
|
|
4328
4362
|
}
|
|
@@ -5011,4 +5045,4 @@ class OutlookClient extends BaseClient {
|
|
|
5011
5045
|
}
|
|
5012
5046
|
}
|
|
5013
5047
|
|
|
5014
|
-
module.exports = { OutlookClient, encodeFolderSegment, decodeFolderSegment, parseFallbackPollInterval };
|
|
5048
|
+
module.exports = { OutlookClient, encodeFolderSegment, decodeFolderSegment, parseFallbackPollInterval, flushRecoveryStart };
|