Issue 21 — event infrastructure: - NormalizedEvent<T> + payload types in packages/shared-types/src/events/ - Bus.onPublish() hook for side-effect bridges - NATS JetStream adapter (services/api/src/events/nats.ts): connects when NATS_URL is set, creates signals.> and feedback.> streams, bridges all in-process bus publishes to JetStream — no-ops gracefully when NATS is absent - NATS service added to docker-compose (profile: events|full, port 4222/8222) Issue 22 — Todoist background sync: - services/api/src/signals/scheduler.ts: queries all active-token users every 15 min (TODOIST_SYNC_INTERVAL_MS), fan-out via todoistSource.fetchSignals() which emits signals.task.synced; on-demand fetch remains as freshness fallback - NATS_URL + TODOIST_SYNC_INTERVAL_MS added to config Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
55
services/api/src/signals/scheduler.ts
Normal file
55
services/api/src/signals/scheduler.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Todoist background sync scheduler (Issue #22).
|
||||
*
|
||||
* Periodically fetches Todoist tasks for every user with an active token so
|
||||
* that signals are fresh before the next /recommend call. The on-demand fetch
|
||||
* in TodoistSignalSource remains as the freshness-critical fallback — if a user
|
||||
* hits /recommend and the cache is stale, it re-fetches inline.
|
||||
*
|
||||
* Interval: TODOIST_SYNC_INTERVAL_MS (default 15 min).
|
||||
*/
|
||||
|
||||
import { db } from '../db/index.js';
|
||||
import { integrationTokens } from '../db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { todoistSource } from './todoist.js';
|
||||
|
||||
const DEFAULT_INTERVAL_MS = 15 * 60 * 1000;
|
||||
|
||||
export function startTodoistSyncScheduler(intervalMs = DEFAULT_INTERVAL_MS): NodeJS.Timeout {
|
||||
async function syncAll(): Promise<void> {
|
||||
let users: { userId: string }[] = [];
|
||||
try {
|
||||
users = await db
|
||||
.select({ userId: integrationTokens.userId })
|
||||
.from(integrationTokens)
|
||||
.where(eq(integrationTokens.tokenStatus, 'active'));
|
||||
} catch (err: any) {
|
||||
console.error(`[scheduler] failed to query users: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!users.length) return;
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
users.map((u) => todoistSource.fetchSignals(u.userId)),
|
||||
);
|
||||
|
||||
let ok = 0;
|
||||
let failed = 0;
|
||||
for (const r of results) {
|
||||
if (r.status === 'fulfilled') ok++;
|
||||
else { failed++; console.error(`[scheduler] sync error:`, r.reason); }
|
||||
}
|
||||
|
||||
console.log(`[scheduler] todoist sync: ${ok} ok, ${failed} failed (${users.length} users)`);
|
||||
}
|
||||
|
||||
// Run once shortly after startup, then on interval
|
||||
const delay = setTimeout(() => {
|
||||
syncAll();
|
||||
setInterval(syncAll, intervalMs);
|
||||
}, 10_000);
|
||||
|
||||
return delay;
|
||||
}
|
||||
Reference in New Issue
Block a user