refactor: change all timestamps to be in seconds

This commit is contained in:
Nicolas Meienberger
2025-11-26 22:41:39 +01:00
parent 4328607cc1
commit 60f37076a8
12 changed files with 776 additions and 34 deletions

View File

@@ -21,6 +21,7 @@ import {
} from "./auth.dto";
import { authService } from "./auth.service";
import { toMessage } from "../../utils/errors";
import { logger } from "~/server/utils/logger";
const COOKIE_NAME = "session_id";
const COOKIE_OPTIONS = {
@@ -66,7 +67,7 @@ export const authController = new Hono()
setCookie(c, COOKIE_NAME, sessionId, {
...COOKIE_OPTIONS,
expires: new Date(expiresAt),
expires: new Date(expiresAt * 1000),
});
return c.json<LoginDto>({

View File

@@ -3,7 +3,7 @@ import { db } from "../../db/db";
import { sessionsTable, usersTable } from "../../db/schema";
import { logger } from "../../utils/logger";
const SESSION_DURATION = 1000 * 60 * 60 * 24 * 30; // 30 days
const SESSION_DURATION = 60 * 60 * 24 * 30; // 30 days in seconds
export class AuthService {
/**
@@ -30,7 +30,7 @@ export class AuthService {
logger.info(`User registered: ${username}`);
const sessionId = crypto.randomUUID();
const expiresAt = new Date(Date.now() + SESSION_DURATION).getTime();
const expiresAt = Math.floor(Date.now() / 1000) + SESSION_DURATION;
await db.insert(sessionsTable).values({
id: sessionId,
@@ -66,7 +66,7 @@ export class AuthService {
}
const sessionId = crypto.randomUUID();
const expiresAt = new Date(Date.now() + SESSION_DURATION).getTime();
const expiresAt = Math.floor(Date.now() / 1000) + SESSION_DURATION;
await db.insert(sessionsTable).values({
id: sessionId,
@@ -112,7 +112,7 @@ export class AuthService {
return null;
}
if (session.session.expiresAt < Date.now()) {
if (session.session.expiresAt < Math.floor(Date.now() / 1000)) {
await db.delete(sessionsTable).where(eq(sessionsTable.id, sessionId));
return null;
}
@@ -134,7 +134,7 @@ export class AuthService {
* Clean up expired sessions
*/
async cleanupExpiredSessions() {
const result = await db.delete(sessionsTable).where(lt(sessionsTable.expiresAt, Date.now())).returning();
const result = await db.delete(sessionsTable).where(lt(sessionsTable.expiresAt, Math.floor(Date.now() / 1000))).returning();
if (result.length > 0) {
logger.info(`Cleaned up ${result.length} expired sessions`);
}

View File

@@ -21,12 +21,12 @@ const calculateNextRun = (cronExpression: string): number => {
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
});
return interval.next().getTime();
return Math.floor(interval.next().getTime() / 1000);
} catch (error) {
logger.error(`Failed to parse cron expression "${cronExpression}": ${error}`);
const fallback = new Date();
fallback.setMinutes(fallback.getMinutes() + 1);
return fallback.getTime();
return Math.floor(fallback.getTime() / 1000);
}
};
@@ -126,7 +126,7 @@ const updateSchedule = async (scheduleId: number, data: UpdateBackupScheduleBody
const [updated] = await db
.update(backupSchedulesTable)
.set({ ...data, nextBackupAt, updatedAt: Date.now() })
.set({ ...data, nextBackupAt, updatedAt: Math.floor(Date.now() / 1000) })
.where(eq(backupSchedulesTable.id, scheduleId))
.returning();
@@ -209,7 +209,7 @@ const executeBackup = async (scheduleId: number, manual = false) => {
await db
.update(backupSchedulesTable)
.set({ lastBackupStatus: "in_progress", updatedAt: Date.now(), lastBackupError: null, nextBackupAt })
.set({ lastBackupStatus: "in_progress", updatedAt: Math.floor(Date.now() / 1000), lastBackupError: null, nextBackupAt })
.where(eq(backupSchedulesTable.id, scheduleId));
const abortController = new AbortController();
@@ -257,11 +257,11 @@ const executeBackup = async (scheduleId: number, manual = false) => {
await db
.update(backupSchedulesTable)
.set({
lastBackupAt: Date.now(),
lastBackupAt: Math.floor(Date.now() / 1000),
lastBackupStatus: exitCode === 0 ? "success" : "warning",
lastBackupError: null,
nextBackupAt: nextBackupAt,
updatedAt: Date.now(),
updatedAt: Math.floor(Date.now() / 1000),
})
.where(eq(backupSchedulesTable.id, scheduleId));
@@ -292,10 +292,10 @@ const executeBackup = async (scheduleId: number, manual = false) => {
await db
.update(backupSchedulesTable)
.set({
lastBackupAt: Date.now(),
lastBackupAt: Math.floor(Date.now() / 1000),
lastBackupStatus: "error",
lastBackupError: toMessage(error),
updatedAt: Date.now(),
updatedAt: Math.floor(Date.now() / 1000),
})
.where(eq(backupSchedulesTable.id, scheduleId));
@@ -323,7 +323,7 @@ const executeBackup = async (scheduleId: number, manual = false) => {
};
const getSchedulesToExecute = async () => {
const now = Date.now();
const now = Math.floor(Date.now() / 1000);
const schedules = await db.query.backupSchedulesTable.findMany({
where: eq(backupSchedulesTable.enabled, true),
});
@@ -362,7 +362,7 @@ const stopBackup = async (scheduleId: number) => {
.set({
lastBackupStatus: "error",
lastBackupError: "Backup was stopped by user",
updatedAt: Date.now(),
updatedAt: Math.floor(Date.now() / 1000),
})
.where(eq(backupSchedulesTable.id, scheduleId));

View File

@@ -105,7 +105,7 @@ const createRepository = async (name: string, config: RepositoryConfig, compress
if (!error) {
await db
.update(repositoriesTable)
.set({ status: "healthy", lastChecked: Date.now(), lastError: null })
.set({ status: "healthy", lastChecked: Math.floor(Date.now() / 1000), lastError: null })
.where(eq(repositoriesTable.id, id));
return { repository: created, status: 201 };
@@ -258,7 +258,7 @@ const checkHealth = async (repositoryId: string) => {
.update(repositoriesTable)
.set({
status,
lastChecked: Date.now(),
lastChecked: Math.floor(Date.now() / 1000),
lastError: error,
})
.where(eq(repositoriesTable.id, repository.id));
@@ -335,7 +335,7 @@ const doctorRepository = async (name: string) => {
.update(repositoriesTable)
.set({
status: allSuccessful ? "healthy" : "error",
lastChecked: Date.now(),
lastChecked: Math.floor(Date.now() / 1000),
lastError: allSuccessful ? null : steps.find((s) => !s.success)?.error,
})
.where(eq(repositoriesTable.id, repository.id));

View File

@@ -57,7 +57,7 @@ const createVolume = async (name: string, backendConfig: BackendConfig) => {
await db
.update(volumesTable)
.set({ status, lastError: error ?? null, lastHealthCheck: Date.now() })
.set({ status, lastError: error ?? null, lastHealthCheck: Math.floor(Date.now() / 1000) })
.where(eq(volumesTable.name, slug));
return { volume: created, status: 201 };
@@ -91,7 +91,7 @@ const mountVolume = async (name: string) => {
await db
.update(volumesTable)
.set({ status, lastError: error ?? null, lastHealthCheck: Date.now() })
.set({ status, lastError: error ?? null, lastHealthCheck: Math.floor(Date.now() / 1000) })
.where(eq(volumesTable.name, name));
if (status === "mounted") {
@@ -196,7 +196,7 @@ const updateVolume = async (name: string, volumeData: UpdateVolumeBody) => {
const { error, status } = await backend.mount();
await db
.update(volumesTable)
.set({ status, lastError: error ?? null, lastHealthCheck: Date.now() })
.set({ status, lastError: error ?? null, lastHealthCheck: Math.floor(Date.now() / 1000) })
.where(eq(volumesTable.id, existing.id));
serverEvents.emit("volume:updated", { volumeName: updated.name });
@@ -255,7 +255,7 @@ const checkHealth = async (name: string) => {
await db
.update(volumesTable)
.set({ lastHealthCheck: Date.now(), status, lastError: error ?? null })
.set({ lastHealthCheck: Math.floor(Date.now() / 1000), status, lastError: error ?? null })
.where(eq(volumesTable.name, volume.name));
return { status, error };