Skip to content

Commit

Permalink
Change execSync/spawnSync to async (louislam#4123)
Browse files Browse the repository at this point in the history
* WIP

* Add missing await

* Update package-lock.json
  • Loading branch information
louislam authored Nov 30, 2023
1 parent 73239d4 commit 1708b67
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 2 additions & 5 deletions server/monitor-types/tailscale-ping.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()}`);
}
Expand Down
4 changes: 2 additions & 2 deletions server/notification-providers/apprise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const NotificationProvider = require("./notification-provider");
const childProcess = require("child_process");
const childProcessAsync = require("promisify-child-process");

class Apprise extends NotificationProvider {

Expand All @@ -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";

Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
14 changes: 7 additions & 7 deletions server/uptime-kuma-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -344,7 +344,7 @@ class UptimeKumaServer {
let enable = await Settings.get("nscd");

if (enable || enable === null) {
this.startNSCDServices();
await this.startNSCDServices();
}
}

Expand All @@ -356,19 +356,19 @@ class UptimeKumaServer {
let enable = await Settings.get("nscd");

if (enable || enable === null) {
this.stopNSCDServices();
await this.stopNSCDServices();
}
}

/**
* 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");
}
Expand All @@ -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");
}
Expand Down

0 comments on commit 1708b67

Please sign in to comment.