Skip to content

Commit eef2fe5

Browse files
committed
add logging
1 parent 6dd474f commit eef2fe5

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

apps/api/src/scheduler.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ConfiguredMailAccount, SyncConfig } from "./config.js";
2+
import { logInfo } from "./logger.js";
23
import type { SyncQueue } from "./sync-queue.js";
34

45
export type SyncSchedulerOptions = {
@@ -23,6 +24,12 @@ export function createSyncScheduler({
2324
let recentReconciliationTimer: ReturnType<typeof setInterval> | undefined;
2425

2526
function scheduleRegular() {
27+
logInfo("sync.scheduler.poll", {
28+
scope: "regular",
29+
accountCount: accounts.length,
30+
intervalMinutes: sync.regularSyncIntervalMinutes,
31+
});
32+
2633
for (const account of accounts) {
2734
syncQueue.schedule({
2835
accountId: account.id,
@@ -33,6 +40,13 @@ export function createSyncScheduler({
3340
}
3441

3542
function scheduleRecentReconciliation() {
43+
logInfo("sync.scheduler.poll", {
44+
scope: "recentReconciliation",
45+
accountCount: accounts.length,
46+
days: sync.recentReconciliationWindowDays,
47+
intervalMinutes: sync.recentReconciliationIntervalMinutes,
48+
});
49+
3650
for (const account of accounts) {
3751
syncQueue.schedule({
3852
accountId: account.id,

apps/api/src/sync-queue.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { logError, logInfo } from "./logger.js";
2+
13
export type SyncJobState =
24
| "pending"
35
| "running"
@@ -77,6 +79,7 @@ export function createSyncQueue({
7779
job.state = state;
7880
job.finishedAt = stamp();
7981
inactive.unshift(job);
82+
logInfo("sync.queue.inactive", syncJobLogFields(job));
8083
}
8184

8285
async function drain() {
@@ -93,14 +96,23 @@ export function createSyncQueue({
9396
running = job;
9497
job.state = "running";
9598
job.startedAt = stamp();
99+
logInfo("sync.queue.start", syncJobLogFields(job));
96100

97101
try {
98102
const result = await execute(job);
99103
job.state = "succeeded";
100104
job.result = result ?? {};
105+
logInfo("sync.queue.finish", {
106+
...syncJobLogFields(job),
107+
...syncJobResultLogFields(job.result),
108+
});
101109
} catch (error) {
102110
job.state = "failed";
103111
job.error = error instanceof Error ? error.message : String(error);
112+
logError("sync.queue.error", {
113+
...syncJobLogFields(job),
114+
error: job.error,
115+
});
104116
} finally {
105117
job.finishedAt = stamp();
106118
running = undefined;
@@ -146,6 +158,11 @@ export function createSyncQueue({
146158
schedule(request) {
147159
const duplicate = findDuplicateAutomatic(request);
148160
if (duplicate) {
161+
logInfo("sync.queue.coalesce", {
162+
...syncJobLogFields(duplicate),
163+
requestedOrigin: request.origin,
164+
requestedScope: syncScopeLabel(request.scope),
165+
});
149166
return duplicate;
150167
}
151168

@@ -177,6 +194,7 @@ export function createSyncQueue({
177194
}
178195

179196
pending.push(job);
197+
logInfo("sync.queue.schedule", syncJobLogFields(job));
180198
startDrain();
181199

182200
return job;
@@ -211,6 +229,40 @@ function sameScope(left: SyncScope, right: SyncScope) {
211229
return "days" in left && "days" in right && left.days === right.days;
212230
}
213231

232+
function syncJobLogFields(job: SyncJob) {
233+
return {
234+
jobId: job.id,
235+
accountId: job.accountId,
236+
origin: job.origin,
237+
scope: syncScopeLabel(job.scope),
238+
state: job.state,
239+
};
240+
}
241+
242+
function syncJobResultLogFields(result: SyncJobResult | undefined) {
243+
if (!result) {
244+
return {};
245+
}
246+
247+
return {
248+
mailboxCount: result.mailboxCount,
249+
scannedMailboxCount: result.scannedMailboxCount,
250+
skippedMailboxCount: result.skippedMailboxCount,
251+
fetchedMessageCount: result.fetchedMessageCount,
252+
storedMessageCount: result.storedMessageCount,
253+
removedMailboxEntryCount: result.removedMailboxEntryCount,
254+
durationMs: result.durationMs,
255+
};
256+
}
257+
258+
function syncScopeLabel(scope: SyncScope): string {
259+
if (scope.type === "regular") {
260+
return "regular";
261+
}
262+
263+
return `${scope.type}:${scope.days}`;
264+
}
265+
214266
function isSmallerCustomRange(left: SyncScope, right: Extract<SyncScope, { type: "customRange" }>) {
215267
return left.type === "customRange" && left.days < right.days;
216268
}

0 commit comments

Comments
 (0)