From 55d20a72b70eb1dae2fd304db5aa3bce7fd54260 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Wed, 7 Sep 2022 16:19:18 -0400 Subject: [PATCH] backend: add types and use named exports for daemons --- packages/backend/src/boot/master.ts | 6 +++--- packages/backend/src/daemons/janitor.ts | 4 ++-- packages/backend/src/daemons/queue-stats.ts | 4 ++-- packages/backend/src/daemons/server-stats.ts | 19 +++++++++++-------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 67cfcca63..ef5520749 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -74,9 +74,9 @@ export async function masterMain(): Promise { bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true); if (!envOption.noDaemons) { - import('../daemons/server-stats.js').then(x => x.default()); - import('../daemons/queue-stats.js').then(x => x.default()); - import('../daemons/janitor.js').then(x => x.default()); + import('../daemons/server-stats.js').then(x => x.serverStats()); + import('../daemons/queue-stats.js').then(x => x.queueStats()); + import('../daemons/janitor.js').then(x => x.janitor()); } } diff --git a/packages/backend/src/daemons/janitor.ts b/packages/backend/src/daemons/janitor.ts index 4f56ecd98..06f32814f 100644 --- a/packages/backend/src/daemons/janitor.ts +++ b/packages/backend/src/daemons/janitor.ts @@ -8,8 +8,8 @@ const interval = 30 * 60 * 1000; /** * Clean up database occasionally */ -export default function() { - async function tick() { +export function janitor(): void { + async function tick(): Promise { await AttestationChallenges.delete({ createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)), }); diff --git a/packages/backend/src/daemons/queue-stats.ts b/packages/backend/src/daemons/queue-stats.ts index 1535abc6a..8d7736058 100644 --- a/packages/backend/src/daemons/queue-stats.ts +++ b/packages/backend/src/daemons/queue-stats.ts @@ -8,7 +8,7 @@ const interval = 10000; /** * Report queue stats regularly */ -export default function() { +export function queueStats(): void { const log = [] as any[]; ev.on('requestQueueStatsLog', x => { @@ -26,7 +26,7 @@ export default function() { activeInboxJobs++; }); - async function tick() { + async function tick(): Promise { const deliverJobCounts = await deliverQueue.getJobCounts(); const inboxJobCounts = await inboxQueue.getJobCounts(); diff --git a/packages/backend/src/daemons/server-stats.ts b/packages/backend/src/daemons/server-stats.ts index faf4e6e4a..29546d6ab 100644 --- a/packages/backend/src/daemons/server-stats.ts +++ b/packages/backend/src/daemons/server-stats.ts @@ -6,20 +6,20 @@ const ev = new Xev(); const interval = 2000; -const roundCpu = (num: number) => Math.round(num * 1000) / 1000; -const round = (num: number) => Math.round(num * 10) / 10; +const roundCpu = (num: number): number => Math.round(num * 1000) / 1000; +const round = (num: number): number => Math.round(num * 10) / 10; /** * Report server stats regularly */ -export default function() { +export function serverStats(): void { const log = [] as any[]; ev.on('requestServerStatsLog', x => { ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50)); }); - async function tick() { + async function tick(): Promise { const cpu = await cpuUsage(); const memStats = await mem(); const netStats = await net(); @@ -60,20 +60,23 @@ function cpuUsage(): Promise { } // MEMORY STAT -async function mem() { +async function mem(): Promise { const data = await si.mem(); return data; } // NETWORK STAT -async function net() { +async function net(): Promise { const iface = await si.networkInterfaceDefault(); const data = await si.networkStats(iface); return data[0]; } // FS STAT -async function fs() { +async function fs(): Promise { const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 })); - return data || { rIO_sec: 0, wIO_sec: 0 }; + return data; }