From 543e0858d771c183e49e7c53e8c725ee6e2c0ed3 Mon Sep 17 00:00:00 2001 From: Eric Zhang Date: Sun, 22 Dec 2024 15:30:03 -0500 Subject: [PATCH] Fix signal handling in development script rundev.js (#169) --- rundev.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/rundev.js b/rundev.js index 23ecd07..a225c45 100644 --- a/rundev.js +++ b/rundev.js @@ -9,18 +9,15 @@ import { PubSub } from "@google-cloud/pubsub"; import { spawn, spawnSync } from "node:child_process"; import path from "node:path"; +import process from "node:process"; import { fileURLToPath } from "node:url"; // Patch for __dirname not being available in ES modules. const __dirname = path.dirname(fileURLToPath(import.meta.url)); -let build, emulators, app; +console.log("[setwithfriends] Starting development script..."); -process.on("exit", () => { - if (build && !build.killed) build.kill(); - if (emulators && !emulators.killed) emulators.kill(); - if (app && !app.killed) app.kill(); -}); +let build, emulators, app; // Initial build spawnSync("npm", ["run", "build"], { @@ -67,6 +64,28 @@ const pubsub = new PubSub({ projectId: "setwithfriends-dev", }); -setInterval(async () => { - await pubsub.topic("firebase-schedule-clearConnections").publishJSON({}); +const pubsubInterval = setInterval(async () => { + await pubsub + .topic("firebase-schedule-clearConnections") + .publishMessage({ json: {} }); }, 60 * 1000); // every minute + +let shutdownCalled = false; + +async function shutdown() { + if (shutdownCalled) return; + shutdownCalled = true; + + clearInterval(pubsubInterval); + + const waitForChild = (p) => new Promise((resolve) => p.on("exit", resolve)); + await Promise.all([ + waitForChild(build), + waitForChild(emulators), + waitForChild(app), + ]); + console.log("[setwithfriends] Finished development script, goodbye!"); +} + +process.on("SIGINT", shutdown); +process.on("SIGTERM", shutdown);