Stronger types for unix timestamps

This commit is contained in:
baldo 2022-07-18 12:55:19 +02:00
parent 217ed6ff20
commit c627e702ce
4 changed files with 22 additions and 9 deletions

View file

@ -76,10 +76,9 @@ async function removePendingMailFromQueue(id: MailId): Promise<void> {
}
async function incrementFailureCounterForPendingEmail(id: MailId): Promise<void> {
const now = moment();
await db.run(
'UPDATE email_queue SET failures = failures + 1, modified_at = ? WHERE id = ?',
[now.unix(), id],
[moment().unix(), id],
);
}

View file

@ -666,6 +666,10 @@ export async function sendMonitoringMails(): Promise<void> {
}
}
function toUnixTimestamp(moment: Moment): UnixTimestampSeconds {
return moment.unix() as UnixTimestampSeconds;
}
export async function deleteOfflineNodes(): Promise<void> {
Logger
.tag('nodes', 'delete-offline')
@ -676,10 +680,10 @@ export async function deleteOfflineNodes(): Promise<void> {
);
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
);

View file

@ -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<UnixTimestampSeconds> {
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");

View file

@ -293,8 +293,12 @@ export type MAC = {
};
export const isMAC = toIsNewtype<MAC>(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;