Merge pull request 'backend: add types and use named exports for daemons' (#127) from fix/backend-daemons-types into main

Reviewed-on: FoundKeyGang/FoundKey#127
This commit is contained in:
Norm 2022-09-10 14:23:30 +00:00
commit 70c12158f7
4 changed files with 18 additions and 15 deletions

View file

@ -74,9 +74,9 @@ export async function masterMain(): Promise<void> {
bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true); bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true);
if (!envOption.noDaemons) { if (!envOption.noDaemons) {
import('../daemons/server-stats.js').then(x => x.default()); import('../daemons/server-stats.js').then(x => x.serverStats());
import('../daemons/queue-stats.js').then(x => x.default()); import('../daemons/queue-stats.js').then(x => x.queueStats());
import('../daemons/janitor.js').then(x => x.default()); import('../daemons/janitor.js').then(x => x.janitor());
} }
} }

View file

@ -8,8 +8,8 @@ const interval = 30 * 60 * 1000;
/** /**
* Clean up database occasionally * Clean up database occasionally
*/ */
export default function() { export function janitor(): void {
async function tick() { async function tick(): Promise<void> {
await AttestationChallenges.delete({ await AttestationChallenges.delete({
createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)), createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)),
}); });

View file

@ -8,7 +8,7 @@ const interval = 10000;
/** /**
* Report queue stats regularly * Report queue stats regularly
*/ */
export default function() { export function queueStats(): void {
const log = [] as any[]; const log = [] as any[];
ev.on('requestQueueStatsLog', x => { ev.on('requestQueueStatsLog', x => {
@ -26,7 +26,7 @@ export default function() {
activeInboxJobs++; activeInboxJobs++;
}); });
async function tick() { async function tick(): Promise<void> {
const deliverJobCounts = await deliverQueue.getJobCounts(); const deliverJobCounts = await deliverQueue.getJobCounts();
const inboxJobCounts = await inboxQueue.getJobCounts(); const inboxJobCounts = await inboxQueue.getJobCounts();

View file

@ -6,20 +6,20 @@ const ev = new Xev();
const interval = 2000; const interval = 2000;
const roundCpu = (num: number) => Math.round(num * 1000) / 1000; const roundCpu = (num: number): number => Math.round(num * 1000) / 1000;
const round = (num: number) => Math.round(num * 10) / 10; const round = (num: number): number => Math.round(num * 10) / 10;
/** /**
* Report server stats regularly * Report server stats regularly
*/ */
export default function() { export function serverStats(): void {
const log = [] as any[]; const log = [] as any[];
ev.on('requestServerStatsLog', x => { ev.on('requestServerStatsLog', x => {
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50)); ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50));
}); });
async function tick() { async function tick(): Promise<void> {
const cpu = await cpuUsage(); const cpu = await cpuUsage();
const memStats = await mem(); const memStats = await mem();
const netStats = await net(); const netStats = await net();
@ -60,20 +60,23 @@ function cpuUsage(): Promise<number> {
} }
// MEMORY STAT // MEMORY STAT
async function mem() { async function mem(): Promise<si.Systeminformation.MemData> {
const data = await si.mem(); const data = await si.mem();
return data; return data;
} }
// NETWORK STAT // NETWORK STAT
async function net() { async function net(): Promise<si.Systeminformation.NetworkStatsData> {
const iface = await si.networkInterfaceDefault(); const iface = await si.networkInterfaceDefault();
const data = await si.networkStats(iface); const data = await si.networkStats(iface);
return data[0]; return data[0];
} }
// FS STAT // FS STAT
async function fs() { async function fs(): Promise<si.Systeminformation.DisksIoData | {
rIO_sec: number;
wIO_sec: number;
}> {
const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 })); const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 }));
return data || { rIO_sec: 0, wIO_sec: 0 }; return data;
} }