From 1708b67949ca183b2ee86ef70e9e51c5f721a73e Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 30 Nov 2023 16:12:04 +0800 Subject: [PATCH] Change execSync/spawnSync to async (#4123) * WIP * Add missing await * Update package-lock.json --- package-lock.json | 9 +++++++++ package.json | 1 + server/monitor-types/tailscale-ping.js | 7 ++----- server/notification-providers/apprise.js | 4 ++-- server/server.js | 4 ++-- server/uptime-kuma-server.js | 14 +++++++------- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 248e64a5e..705fe812a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,6 +63,7 @@ "playwright-core": "~1.35.1", "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", + "promisify-child-process": "~4.1.2", "protobufjs": "~7.2.4", "qs": "~6.10.4", "redbean-node": "~0.3.0", @@ -15751,6 +15752,14 @@ "node": ">=10" } }, + "node_modules/promisify-child-process": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/promisify-child-process/-/promisify-child-process-4.1.2.tgz", + "integrity": "sha512-APnkIgmaHNJpkAn7k+CrJSi9WMuff5ctYFbD0CO2XIPkM8yO7d/ShouU2clywbpHV/DUsyc4bpJCsNgddNtx4g==", + "engines": { + "node": ">=8" + } + }, "node_modules/prompts": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", diff --git a/package.json b/package.json index b24f3ec21..a9db8556f 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "playwright-core": "~1.35.1", "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", + "promisify-child-process": "~4.1.2", "protobufjs": "~7.2.4", "qs": "~6.10.4", "redbean-node": "~0.3.0", diff --git a/server/monitor-types/tailscale-ping.js b/server/monitor-types/tailscale-ping.js index 3b2ac487f..1b2be2003 100644 --- a/server/monitor-types/tailscale-ping.js +++ b/server/monitor-types/tailscale-ping.js @@ -1,6 +1,6 @@ const { MonitorType } = require("./monitor-type"); const { UP } = require("../../src/util"); -const childProcess = require("child_process"); +const childProcessAsync = require("promisify-child-process"); /** * A TailscalePing class extends the MonitorType. @@ -38,12 +38,9 @@ class TailscalePing extends MonitorType { */ async runTailscalePing(hostname, interval) { let timeout = interval * 1000 * 0.8; - let res = childProcess.spawnSync("tailscale", [ "ping", hostname ], { + let res = await childProcessAsync.spawn("tailscale", [ "ping", "--c", "1", hostname ], { timeout: timeout }); - if (res.error) { - throw new Error(`Execution error: ${res.error.message}`); - } if (res.stderr && res.stderr.toString()) { throw new Error(`Error in output: ${res.stderr.toString()}`); } diff --git a/server/notification-providers/apprise.js b/server/notification-providers/apprise.js index 887afbf55..1943aaa60 100644 --- a/server/notification-providers/apprise.js +++ b/server/notification-providers/apprise.js @@ -1,5 +1,5 @@ const NotificationProvider = require("./notification-provider"); -const childProcess = require("child_process"); +const childProcessAsync = require("promisify-child-process"); class Apprise extends NotificationProvider { @@ -11,7 +11,7 @@ class Apprise extends NotificationProvider { args.push("-t"); args.push(notification.title); } - const s = childProcess.spawnSync("apprise", args); + const s = await childProcessAsync.spawn("apprise", args); const output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found"; diff --git a/server/server.js b/server/server.js index 9d509b45e..13dd16018 100644 --- a/server/server.js +++ b/server/server.js @@ -1223,9 +1223,9 @@ let needSetup = false; // Update nscd status if (previousNSCDStatus !== data.nscd) { if (data.nscd) { - server.startNSCDServices(); + await server.startNSCDServices(); } else { - server.stopNSCDServices(); + await server.stopNSCDServices(); } } diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 6acc8d4df..28df64615 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -10,7 +10,7 @@ const util = require("util"); const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent"); const { Settings } = require("./settings"); const dayjs = require("dayjs"); -const childProcess = require("child_process"); +const childProcessAsync = require("promisify-child-process"); const path = require("path"); // DO NOT IMPORT HERE IF THE MODULES USED `UptimeKumaServer.getInstance()`, put at the bottom of this file instead. @@ -344,7 +344,7 @@ class UptimeKumaServer { let enable = await Settings.get("nscd"); if (enable || enable === null) { - this.startNSCDServices(); + await this.startNSCDServices(); } } @@ -356,7 +356,7 @@ class UptimeKumaServer { let enable = await Settings.get("nscd"); if (enable || enable === null) { - this.stopNSCDServices(); + await this.stopNSCDServices(); } } @@ -364,11 +364,11 @@ class UptimeKumaServer { * Start all system services (e.g. nscd) * For now, only used in Docker */ - startNSCDServices() { + async startNSCDServices() { if (process.env.UPTIME_KUMA_IS_CONTAINER) { try { log.info("services", "Starting nscd"); - childProcess.execSync("sudo service nscd start", { stdio: "pipe" }); + await childProcessAsync.exec("sudo service nscd start"); } catch (e) { log.info("services", "Failed to start nscd"); } @@ -378,11 +378,11 @@ class UptimeKumaServer { /** * Stop all system services */ - stopNSCDServices() { + async stopNSCDServices() { if (process.env.UPTIME_KUMA_IS_CONTAINER) { try { log.info("services", "Stopping nscd"); - childProcess.execSync("sudo service nscd stop"); + await childProcessAsync.exec("sudo service nscd stop"); } catch (e) { log.info("services", "Failed to stop nscd"); }