@remit/imap-worker 2.0.6 → 2.0.8

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.6",
3
+ "version": "2.0.8",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
package/README.md DELETED
@@ -1,103 +0,0 @@
1
- # @remit/imap-worker
2
-
3
- SQS-driven Lambda worker for handling IMAP lifecycle events in the Remit system.
4
-
5
- ## Features
6
-
7
- - **Event Driven**: Processes events from SQS (`SYNC_MAILBOXES`, `SYNC_MESSAGES`, etc.)
8
- - **CLI Tool**: Includes a CLI for manually triggering events during development
9
- - **Bundled**: Built with esbuild for optimal Lambda performance
10
-
11
- ## Installation
12
-
13
- ```bash
14
- npm install
15
- npm run bundle
16
- ```
17
-
18
- ## Usage
19
-
20
- ### Lambda Handler
21
-
22
- The package exports a standard AWS Lambda SQS handler:
23
-
24
- ```typescript
25
- import { handler } from "@remit/imap-worker";
26
- ```
27
-
28
- ### CLI (Local Development)
29
-
30
- Trigger events manually using the included CLI:
31
-
32
- ```bash
33
- # Sync all mailboxes for an account
34
- npm run cli -- -t SYNC_MAILBOXES -a <accountId>
35
-
36
- # Sync messages in a mailbox
37
- npm run cli -- -t SYNC_MESSAGES -a <accountId> -m <mailboxId>
38
-
39
- # Force full sync (ignore lastSyncUid)
40
- npm run cli -- -t SYNC_MESSAGES -a <accountId> -m <mailboxId> --fullSync
41
-
42
- # Sync message bodies for specific messages
43
- npm run cli -- -t SYNC_MESSAGE_BODY -a <accountId> -m <mailboxId> --messageIds id1,id2,id3
44
- ```
45
-
46
- ## Environment Variables
47
-
48
- | Variable | Required | Description |
49
- | ----------------------------- | -------- | -------------------------------------------------------- |
50
- | `DATA_BACKEND` | Yes | Relational backend to run against (`sqlite`) |
51
- | `SQLITE_APP_DB_PATH` | Yes | Path to the app SQLite database |
52
- | `SQLITE_MAIL_DB_PATH` | Yes | Path to the tenant mail SQLite database |
53
- | `SQLITE_SEARCH_DB_PATH` | Yes | Path to the tenant search SQLite database |
54
- | `SQS_QUEUE_URL_MAILBOXES` | Yes | FIFO queue URL for `SYNC_MAILBOXES` events |
55
- | `SQS_QUEUE_URL_MESSAGES` | Yes | FIFO queue URL for `SYNC_MESSAGES` events |
56
- | `SQS_QUEUE_URL_BODY` | Yes | FIFO queue URL for `SYNC_MESSAGE_BODY` events |
57
- | `SQS_QUEUE_URL_FLAGS` | Yes | FIFO queue URL for `SYNC_FLAGS` events |
58
- | `SQS_QUEUE_URL_MAILBOX_MGMT` | Yes | Standard queue URL for mailbox management events |
59
- | `SQS_QUEUE_URL_MESSAGE_MGMT` | Yes | Standard queue URL for message management events |
60
- | `S3_BUCKET` | Yes | S3 bucket for storing raw message content |
61
- | `NODE_ENV` | No | Set to `development` for local execution |
62
- | `LOG_LEVEL` | No | Logging level (default: `info`) |
63
-
64
- ## Event Types
65
-
66
- | Event | Description | Required Fields |
67
- | ------------------- | ---------------------------------------------- | --------------------------------------- |
68
- | `SYNC_MAILBOXES` | Discovers and syncs mailboxes for an account | `accountId` |
69
- | `SYNC_MESSAGES` | Fetches new messages for a mailbox (one batch) | `accountId`, `mailboxId` |
70
- | `SYNC_MESSAGE_BODY` | Fetches and stores message bodies in batch | `accountId`, `mailboxId`, `messageIds` |
71
-
72
- ### Event Schema
73
-
74
- All events share a base schema:
75
-
76
- ```typescript
77
- interface BaseEvent {
78
- accountId: string;
79
- eventId: string; // Idempotency key
80
- timestamp: number; // Unix timestamp
81
- }
82
- ```
83
-
84
- ## Architecture
85
-
86
- ```
87
- SQS Queue
88
- │
89
- ▼
90
- ┌───────────────┐
91
- │ Lambda/CLI │
92
- │ (index.ts) │
93
- └───────┬───────┘
94
- │
95
- ▼
96
- ┌───────────────┐
97
- │ processor │ ─── Routes events to handlers
98
- └───────┬───────┘
99
- │
100
- ├──► syncMailboxes
101
- ├──► syncMessages
102
- └──► syncMessageBody
103
- ```