about summary refs log tree commit diff stats
path: root/src/app.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.ts')
-rw-r--r--src/app.ts18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/app.ts b/src/app.ts
index 6cc6520..a656ca5 100644
--- a/src/app.ts
+++ b/src/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";
 
@@ -25,6 +26,10 @@ const defaultHeaders = {
   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;
   handle: BunFile;
@@ -283,8 +288,10 @@ export const server = {
       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 @@ export const server = {
         pathname,
         newpath ? newpath : "",
       );
+      if (autoSleep && pathname !== "/health") {
+        keepAwake();
+      }
     }
   },
 } satisfies Serve;
 
+if (autoSleep) {
+  keepAwake();
+}
+
 export default server;