feat(scheduler): run wake pushes every five minutes
Add db.getAll for registered tokens and tick the interval with sendPushToDevice per device, with error logging on tick failure.
This commit is contained in:
@@ -55,6 +55,11 @@ export const db = {
|
|||||||
await save(all);
|
await save(all);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async getAll(): Promise<StoredRow[]> {
|
||||||
|
const all = await load();
|
||||||
|
return Object.values(all);
|
||||||
|
},
|
||||||
|
|
||||||
async getByFcmToken(fcmToken: string): Promise<StoredRow | undefined> {
|
async getByFcmToken(fcmToken: string): Promise<StoredRow | undefined> {
|
||||||
const all = await load();
|
const all = await load();
|
||||||
return all[fcmToken];
|
return all[fcmToken];
|
||||||
|
|||||||
+14
-4
@@ -1,11 +1,21 @@
|
|||||||
|
import { db } from "./db/fcmTokens.js";
|
||||||
|
import { sendPushToDevice } from "./services/pushService.js";
|
||||||
|
|
||||||
let intervalId: ReturnType<typeof setInterval> | undefined;
|
let intervalId: ReturnType<typeof setInterval> | undefined;
|
||||||
|
|
||||||
export function startScheduler(): void {
|
export function startScheduler(): void {
|
||||||
if (intervalId !== undefined) return;
|
if (intervalId !== undefined) return;
|
||||||
// TODO: replace with job queue or cron for wake-up checks
|
|
||||||
intervalId = setInterval(() => {
|
intervalId = setInterval(async () => {
|
||||||
// placeholder tick
|
try {
|
||||||
}, 60_000);
|
const devices = await db.getAll();
|
||||||
|
for (const d of devices) {
|
||||||
|
await sendPushToDevice(d.fcmToken);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("scheduler tick failed", err);
|
||||||
|
}
|
||||||
|
}, 5 * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopScheduler(): void {
|
export function stopScheduler(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user