46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import type { SanitizedConfig } from "payload";
|
|
import { getPayload } from "payload";
|
|
import { markStaleServersOffline } from "@/lib/arma-bridge/presence";
|
|
import { createBinLogger, errorMessage } from "./lib/binFileLogger";
|
|
|
|
export const script = async (config: SanitizedConfig) => {
|
|
const payload = await getPayload({ config });
|
|
const log = createBinLogger("server-tick", payload.logger);
|
|
|
|
try {
|
|
log.info("[Server Tick] Starting server presence tick...");
|
|
|
|
const nowIso = new Date().toISOString();
|
|
const result = await markStaleServersOffline(payload);
|
|
|
|
const notifyUrl = (process.env.APP_URL ?? "http://localhost:3000").replace(/\/$/, "");
|
|
const notifySecret = process.env.GAME_TICK_NOTIFY_SECRET;
|
|
if (notifySecret) {
|
|
await fetch(`${notifyUrl}/api/game-tick/notify`, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-game-tick-secret": notifySecret,
|
|
},
|
|
body: JSON.stringify({ processedAt: nowIso, source: "server-tick" }),
|
|
}).catch((error: unknown) => {
|
|
log.error(`[Server Tick] Failed to notify app clients: ${errorMessage(error)}`);
|
|
});
|
|
} else {
|
|
log.warn("[Server Tick] GAME_TICK_NOTIFY_SECRET not set; skipping client notification");
|
|
}
|
|
|
|
if (result.markedOffline > 0) {
|
|
const names = result.servers.map((s) => s.serverId).join(", ");
|
|
log.info(`[Server Tick] Complete. Marked ${result.markedOffline} server(s) offline: ${names}`);
|
|
} else {
|
|
log.info("[Server Tick] Complete. No stale online servers.");
|
|
}
|
|
process.exit(0);
|
|
} catch (error) {
|
|
log.error(
|
|
`[Server Tick] Fatal: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
};
|