Skip to content

Commit ed873d7

Browse files
authored
cleanup stray scheduler processes (#245)
1 parent c093f8a commit ed873d7

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

resources/js/electron-plugin/dist/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { app, session, powerMonitor } from "electron";
1111
import { initialize } from "@electron/remote/main/index.js";
1212
import state from "./server/state.js";
1313
import { electronApp, optimizer } from "@electron-toolkit/utils";
14-
import { retrieveNativePHPConfig, retrievePhpIniSettings, runScheduler, startAPI, startPhpApp, } from "./server/index.js";
14+
import { retrieveNativePHPConfig, retrievePhpIniSettings, runScheduler, killScheduler, startAPI, startPhpApp, } from "./server/index.js";
1515
import { notifyLaravel } from "./server/utils.js";
1616
import { resolve } from "path";
1717
import { stopAllProcesses } from "./server/api/childProcess.js";
@@ -22,8 +22,8 @@ const { autoUpdater } = electronUpdater;
2222
class NativePHP {
2323
constructor() {
2424
this.processes = [];
25-
this.schedulerInterval = undefined;
2625
this.mainWindow = null;
26+
this.schedulerInterval = undefined;
2727
}
2828
bootstrap(app, icon, phpBinary, cert) {
2929
initialize();
@@ -192,6 +192,7 @@ class NativePHP {
192192
clearInterval(this.schedulerInterval);
193193
this.schedulerInterval = null;
194194
}
195+
killScheduler();
195196
}
196197
startScheduler() {
197198
const now = new Date();
@@ -206,9 +207,14 @@ class NativePHP {
206207
}, delay);
207208
}
208209
killChildProcesses() {
210+
this.stopScheduler();
209211
this.processes
210212
.filter((p) => p !== undefined)
211213
.forEach((process) => {
214+
if (!process || !process.pid)
215+
return;
216+
if (process.killed && process.exitCode !== null)
217+
return;
212218
try {
213219
killSync(process.pid, 'SIGTERM', true);
214220
ps.kill(process.pid);

resources/js/electron-plugin/dist/server/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import startAPIServer from "./api.js";
1111
import { retrieveNativePHPConfig, retrievePhpIniSettings, serveApp, startScheduler, } from "./php.js";
1212
import { appendCookie } from "./utils.js";
1313
import state from "./state.js";
14+
let schedulerProcess = null;
1415
export function startPhpApp() {
1516
return __awaiter(this, void 0, void 0, function* () {
1617
const result = yield serveApp(state.randomSecret, state.electronApiPort, state.phpIni);
@@ -20,7 +21,14 @@ export function startPhpApp() {
2021
});
2122
}
2223
export function runScheduler() {
23-
startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
24+
killScheduler();
25+
schedulerProcess = startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
26+
}
27+
export function killScheduler() {
28+
if (schedulerProcess && !schedulerProcess.killed) {
29+
schedulerProcess.kill();
30+
schedulerProcess = null;
31+
}
2432
}
2533
export function startAPI() {
2634
return startAPIServer(state.randomSecret);

resources/js/electron-plugin/src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import CrossProcessExports from "electron";
22
import { app, session, powerMonitor } from "electron";
3+
import { ChildProcessWithoutNullStreams } from "child_process";
34
import { initialize } from "@electron/remote/main/index.js";
45
import state from "./server/state.js";
56
import { electronApp, optimizer } from "@electron-toolkit/utils";
67
import {
78
retrieveNativePHPConfig,
89
retrievePhpIniSettings,
910
runScheduler,
11+
killScheduler,
1012
startAPI,
1113
startPhpApp,
1214
} from "./server/index.js";
@@ -21,9 +23,9 @@ import electronUpdater from 'electron-updater';
2123
const { autoUpdater } = electronUpdater;
2224

2325
class NativePHP {
24-
processes = [];
25-
schedulerInterval = undefined;
26+
processes: ChildProcessWithoutNullStreams[] = [];
2627
mainWindow = null;
28+
schedulerInterval = undefined;
2729

2830
public bootstrap(
2931
app: CrossProcessExports.App,
@@ -244,12 +246,13 @@ class NativePHP {
244246
}
245247

246248

247-
private stopScheduler() {
248-
if (this.schedulerInterval) {
249-
clearInterval(this.schedulerInterval);
250-
this.schedulerInterval = null;
251-
}
252-
}
249+
private stopScheduler() {
250+
if (this.schedulerInterval) {
251+
clearInterval(this.schedulerInterval);
252+
this.schedulerInterval = null;
253+
}
254+
killScheduler();
255+
}
253256

254257
private startScheduler() {
255258
const now = new Date();
@@ -270,16 +273,23 @@ class NativePHP {
270273
}
271274

272275
private killChildProcesses() {
276+
this.stopScheduler();
277+
273278
this.processes
274279
.filter((p) => p !== undefined)
275280
.forEach((process) => {
281+
if (!process || !process.pid) return;
282+
if (process.killed && process.exitCode !== null) return;
283+
276284
try {
277285
// @ts-ignore
278286
killSync(process.pid, 'SIGTERM', true); // Kill tree
279287
ps.kill(process.pid); // Sometimes does not kill the subprocess of php server
288+
280289
} catch (err) {
281290
console.error(err);
282291
}
292+
283293
});
284294
}
285295
}

resources/js/electron-plugin/src/server/api/childProcess.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from 'express';
2-
import {utilityProcess} from 'electron';
2+
import {utilityProcess, UtilityProcess} from 'electron';
33
import state from '../state.js';
44
import {notifyLaravel} from "../utils.js";
55
import {getAppPath, getDefaultEnvironmentVariables, getDefaultPhpIniSettings, runningSecureBuild} from "../php.js";
@@ -207,7 +207,7 @@ export function stopAllProcesses() {
207207
}
208208
}
209209

210-
function getProcess(alias) {
210+
function getProcess(alias: string): UtilityProcess | undefined {
211211
return state.processes[alias]?.proc;
212212
}
213213

resources/js/electron-plugin/src/server/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
} from "./php.js";
88
import { appendCookie } from "./utils.js";
99
import state from "./state.js";
10+
import { ChildProcess } from "child_process";
11+
12+
let schedulerProcess: ChildProcess | null = null;
1013

1114
export async function startPhpApp() {
1215
const result = await serveApp(
@@ -23,7 +26,15 @@ export async function startPhpApp() {
2326
}
2427

2528
export function runScheduler() {
26-
startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
29+
killScheduler();
30+
schedulerProcess = startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
31+
}
32+
33+
export function killScheduler() {
34+
if (schedulerProcess && !schedulerProcess.killed) {
35+
schedulerProcess.kill();
36+
schedulerProcess = null;
37+
}
2738
}
2839

2940
export function startAPI(): Promise<APIProcess> {

0 commit comments

Comments
 (0)