@remit/imap-worker 2.0.5 → 2.0.7

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.
@@ -1,4 +1,4 @@
1
- import { isNotFoundError } from "@remit/data-ports/errors";
1
+ import { isNotFoundError, LostLeaseError } from "@remit/data-ports/errors";
2
2
  import { SyncPhase } from "@remit/domain-enums";
3
3
  import { RefreshTokenError } from "@remit/mail-oauth-service";
4
4
  import { isMailboxMutationInFlight, MailConnectionError, MessageSyncService, QuarantineService, SyncWarningService, } from "@remit/mailbox-service";
@@ -85,9 +85,9 @@ export const syncMessages = async (event, ctx) => {
85
85
  // mailbox lock and the actual sync run inside the wrapper callback.
86
86
  await withOAuthLifecycle(buildLifecycleDeps(secrets, accountService, ctx.oauth), account, log, async (credentials) => {
87
87
  // Acquire lock before starting sync operation
88
- const { executed } = await mailboxLockService.withMailboxLock(event.mailboxId, "SYNC_MESSAGES", event.accountId, async () => {
88
+ const { executed } = await mailboxLockService.withMailboxLock(event.mailboxId, "SYNC_MESSAGES", event.accountId, async (lease) => {
89
89
  try {
90
- await syncMailboxMessages(event, account, pool.borrow(account, credentials), {
90
+ await syncMailboxMessages(event, account, pool.borrow(account, credentials), lease, {
91
91
  accountService,
92
92
  mailboxService,
93
93
  messageService,
@@ -110,6 +110,17 @@ export const syncMessages = async (event, ctx) => {
110
110
  (err instanceof MailConnectionError && err.kind === "auth")) {
111
111
  throw err;
112
112
  }
113
+ // A successor took the lapsed lease and now owns this mailbox's
114
+ // sync, so this round stops without recording an error phase.
115
+ if (err instanceof LostLeaseError) {
116
+ log.warn({
117
+ accountId: event.accountId,
118
+ mailboxId: event.mailboxId,
119
+ eventId: event.eventId,
120
+ error: err.message,
121
+ }, "Resolving SYNC_MESSAGES: another holder took the lapsed mailbox lock");
122
+ return;
123
+ }
113
124
  // The guard above is a check-then-act: a delete asked for while
114
125
  // this round was in flight lands inside it, and the round then
115
126
  // fails somewhere deeper — on the row (NotFoundError from the
@@ -201,7 +212,7 @@ export const drainPendingFlagPushes = async (flagPushMarkerService, account, mai
201
212
  /**
202
213
  * Sync one batch of messages for a mailbox. Runs under the mailbox lock.
203
214
  */
204
- const syncMailboxMessages = async (event, account, borrowed, deps, log) => {
215
+ const syncMailboxMessages = async (event, account, borrowed, lease, deps, log) => {
205
216
  const { accountService, mailboxService, messageService, envelopeService, addressService, threadMessageService, mailboxSpecialUseService, quarantineService, syncWarningRepository, flagPushMarkerService, messageFlagService, emit, metrics, config, } = deps;
206
217
  // Get the mailbox - it must exist (should have been created by mailbox sync)
207
218
  const mailbox = await mailboxService.get(account.accountId, event.mailboxId);
@@ -216,6 +227,7 @@ const syncMailboxMessages = async (event, account, borrowed, deps, log) => {
216
227
  const syncService = new MessageSyncService(connection, mailboxService, mailboxSpecialUseService, messageService, envelopeService, addressService, threadMessageService, new SyncWarningService(syncWarningRepository), log, flagPushMarkerService, messageFlagService, new QuarantineService(quarantineService, mailboxSpecialUseService, config.workerVersion, log));
217
228
  return syncService.syncMessages(mailboxId, account.accountId, account.accountConfigId, MESSAGE_BATCH_SIZE);
218
229
  })().finally(() => borrowed.release());
230
+ await lease.renew();
219
231
  log.info({
220
232
  syncedCount: result.syncedCount,
221
233
  hasMore: result.hasMore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/imap-worker",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {