all repos — homestead @ 47f5e5886968102c081859fc6d5cd6c74ffd3dae

Code for my website

fly: stop running in secondary regions if no meaningful requests

Alan Pearce
commit

47f5e5886968102c081859fc6d5cd6c74ffd3dae

parent

249bf8c7f676360f0d304d6a7dde701ecb96fcd7

4 files changed, 26 insertions(+), 4 deletions(-)

jump to
M bun.lockbbun.lockb

Not showing binary file.

M fly.tomlfly.toml
@@ -15,9 +15,9 @@
[http_service] internal_port = 3000 force_https = true - auto_stop_machines = false + auto_stop_machines = true auto_start_machines = true - min_machines_running = 3 + min_machines_running = 1 processes = ["app"] [http_service.http_options.response.headers] Strict-Transport-Security = "max-age=31536000; includeSubdomains; preload"
M src/app.tssrc/app.ts
@@ -5,6 +5,7 @@ import type { BunFile, Serve } from "bun";
import * as Sentry from "@sentry/node"; import prom from "bun-prometheus-client"; import log from "loglevel"; +import { keepAwake } from "./sleep.ts"; import config from "./config";
@@ -24,6 +25,10 @@ const defaultHeaders = {
...config.extra.headers, vary: "Accept-Encoding", }; + +const autoSleep = + import.meta.env.NODE_ENV === "production" && + import.meta.env["FLY_REGION"] !== import.meta.env["PRIMARY_REGION"]; type File = { filename: string;
@@ -283,8 +288,10 @@ Sentry.captureException(error);
log.error("Error", error); return new Response("Something went wrong", { status: status }); } finally { - const seconds = endTimer(); - metrics.requestDuration.observe(seconds); + if (status === 200) { + const seconds = endTimer(); + metrics.requestDuration.observe(seconds); + } transaction.finish(); log.info( request.method,
@@ -293,8 +300,15 @@ hostname,
pathname, newpath ? newpath : "", ); + if (autoSleep && pathname !== "/health") { + keepAwake(); + } } }, } satisfies Serve; + +if (autoSleep) { + keepAwake(); +} export default server;
A src/sleep.ts
@@ -0,0 +1,8 @@
+let sleepTimeout: Timer; + +export function keepAwake() { + if (sleepTimeout) clearTimeout(sleepTimeout); + sleepTimeout = setTimeout(function () { + process.exit(0); + }, 60_000); +}