From c627e702ceaadf1a3edf9f8ddc02b0226f0414fd Mon Sep 17 00:00:00 2001 From: baldo Date: Mon, 18 Jul 2022 12:55:19 +0200 Subject: [PATCH] Stronger types for unix timestamps --- server/services/mailService.ts | 3 +-- server/services/monitoringService.ts | 10 +++++++--- server/services/nodeService.ts | 10 ++++++++-- server/types/shared.ts | 8 ++++++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/server/services/mailService.ts b/server/services/mailService.ts index 166a083..0b3fc24 100644 --- a/server/services/mailService.ts +++ b/server/services/mailService.ts @@ -76,10 +76,9 @@ async function removePendingMailFromQueue(id: MailId): Promise { } async function incrementFailureCounterForPendingEmail(id: MailId): Promise { - const now = moment(); await db.run( 'UPDATE email_queue SET failures = failures + 1, modified_at = ? WHERE id = ?', - [now.unix(), id], + [moment().unix(), id], ); } diff --git a/server/services/monitoringService.ts b/server/services/monitoringService.ts index 0cd4cda..875bbc5 100644 --- a/server/services/monitoringService.ts +++ b/server/services/monitoringService.ts @@ -666,6 +666,10 @@ export async function sendMonitoringMails(): Promise { } } +function toUnixTimestamp(moment: Moment): UnixTimestampSeconds { + return moment.unix() as UnixTimestampSeconds; +} + export async function deleteOfflineNodes(): Promise { Logger .tag('nodes', 'delete-offline') @@ -676,10 +680,10 @@ export async function deleteOfflineNodes(): Promise { ); const deleteBefore = - moment().subtract( + toUnixTimestamp(moment().subtract( DELETE_OFFLINE_NODES_AFTER_DURATION.amount, DELETE_OFFLINE_NODES_AFTER_DURATION.unit - ).unix(); + )); await deleteNeverOnlineNodesBefore(deleteBefore); await deleteNodesOfflineSinceBefore(deleteBefore); @@ -689,7 +693,7 @@ async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds): Logger .tag('nodes', 'delete-never-online') .info( - 'Deleting nodes that were never online created befor ' + + 'Deleting nodes that were never online created before ' + deleteBefore ); diff --git a/server/services/nodeService.ts b/server/services/nodeService.ts index b741009..32a1749 100644 --- a/server/services/nodeService.ts +++ b/server/services/nodeService.ts @@ -20,7 +20,9 @@ import { NodeStatistics, to, Token, + toUnixTimestampSeconds, unhandledEnumField, + UnixTimestampMilliseconds, UnixTimestampSeconds } from "../types"; import util from "util"; @@ -322,10 +324,14 @@ function setNodeValue(prefix: LINE_PREFIX, node: NodeBuilder, nodeSecrets: NodeS } } +async function getModifiedAt(file: string): Promise { + const modifiedAtMs = (await fs.lstat(file)).mtimeMs as UnixTimestampMilliseconds; + return toUnixTimestampSeconds(modifiedAtMs); +} + async function parseNodeFile(file: string): Promise<{ node: Node, nodeSecrets: NodeSecrets }> { const contents = await fs.readFile(file); - const stats = await fs.lstat(file); - const modifiedAt = Math.floor(stats.mtimeMs / 1000); + const modifiedAt = await getModifiedAt(file); const lines = contents.toString().split("\n"); diff --git a/server/types/shared.ts b/server/types/shared.ts index 9ddc35f..dae75c2 100644 --- a/server/types/shared.ts +++ b/server/types/shared.ts @@ -293,8 +293,12 @@ export type MAC = { }; export const isMAC = toIsNewtype(isString); -export type UnixTimestampSeconds = number; -export type UnixTimestampMilliseconds = number; +export type UnixTimestampSeconds = number & { readonly __tag: unique symbol }; +export type UnixTimestampMilliseconds = number & { readonly __tag: unique symbol }; + +export function toUnixTimestampSeconds(ms: UnixTimestampMilliseconds): UnixTimestampSeconds { + return Math.floor(ms) as UnixTimestampSeconds; +} export type MonitoringToken = { value: string;