Skip to content

cleanup stray scheduler processes #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions resources/js/electron-plugin/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { app, session, powerMonitor } from "electron";
import { initialize } from "@electron/remote/main/index.js";
import state from "./server/state.js";
import { electronApp, optimizer } from "@electron-toolkit/utils";
import { retrieveNativePHPConfig, retrievePhpIniSettings, runScheduler, startAPI, startPhpApp, } from "./server/index.js";
import { retrieveNativePHPConfig, retrievePhpIniSettings, runScheduler, killScheduler, startAPI, startPhpApp, } from "./server/index.js";
import { notifyLaravel } from "./server/utils.js";
import { resolve } from "path";
import { stopAllProcesses } from "./server/api/childProcess.js";
Expand All @@ -22,8 +22,8 @@ const { autoUpdater } = electronUpdater;
class NativePHP {
constructor() {
this.processes = [];
this.schedulerInterval = undefined;
this.mainWindow = null;
this.schedulerInterval = undefined;
}
bootstrap(app, icon, phpBinary, cert) {
initialize();
Expand Down Expand Up @@ -192,6 +192,7 @@ class NativePHP {
clearInterval(this.schedulerInterval);
this.schedulerInterval = null;
}
killScheduler();
}
startScheduler() {
const now = new Date();
Expand All @@ -206,9 +207,14 @@ class NativePHP {
}, delay);
}
killChildProcesses() {
this.stopScheduler();
this.processes
.filter((p) => p !== undefined)
.forEach((process) => {
if (!process || !process.pid)
return;
if (process.killed && process.exitCode !== null)
return;
try {
killSync(process.pid, 'SIGTERM', true);
ps.kill(process.pid);
Expand Down
10 changes: 9 additions & 1 deletion resources/js/electron-plugin/dist/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import startAPIServer from "./api.js";
import { retrieveNativePHPConfig, retrievePhpIniSettings, serveApp, startScheduler, } from "./php.js";
import { appendCookie } from "./utils.js";
import state from "./state.js";
let schedulerProcess = null;
export function startPhpApp() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield serveApp(state.randomSecret, state.electronApiPort, state.phpIni);
Expand All @@ -20,7 +21,14 @@ export function startPhpApp() {
});
}
export function runScheduler() {
startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
killScheduler();
schedulerProcess = startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
}
export function killScheduler() {
if (schedulerProcess && !schedulerProcess.killed) {
schedulerProcess.kill();
schedulerProcess = null;
}
}
export function startAPI() {
return startAPIServer(state.randomSecret);
Expand Down
26 changes: 18 additions & 8 deletions resources/js/electron-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import CrossProcessExports from "electron";
import { app, session, powerMonitor } from "electron";
import { ChildProcessWithoutNullStreams } from "child_process";
import { initialize } from "@electron/remote/main/index.js";
import state from "./server/state.js";
import { electronApp, optimizer } from "@electron-toolkit/utils";
import {
retrieveNativePHPConfig,
retrievePhpIniSettings,
runScheduler,
killScheduler,
startAPI,
startPhpApp,
} from "./server/index.js";
Expand All @@ -21,9 +23,9 @@ import electronUpdater from 'electron-updater';
const { autoUpdater } = electronUpdater;

class NativePHP {
processes = [];
schedulerInterval = undefined;
processes: ChildProcessWithoutNullStreams[] = [];
mainWindow = null;
schedulerInterval = undefined;

public bootstrap(
app: CrossProcessExports.App,
Expand Down Expand Up @@ -244,12 +246,13 @@ class NativePHP {
}


private stopScheduler() {
if (this.schedulerInterval) {
clearInterval(this.schedulerInterval);
this.schedulerInterval = null;
}
}
private stopScheduler() {
if (this.schedulerInterval) {
clearInterval(this.schedulerInterval);
this.schedulerInterval = null;
}
killScheduler();
}

private startScheduler() {
const now = new Date();
Expand All @@ -270,16 +273,23 @@ class NativePHP {
}

private killChildProcesses() {
this.stopScheduler();

this.processes
.filter((p) => p !== undefined)
.forEach((process) => {
if (!process || !process.pid) return;
if (process.killed && process.exitCode !== null) return;

try {
// @ts-ignore
killSync(process.pid, 'SIGTERM', true); // Kill tree
ps.kill(process.pid); // Sometimes does not kill the subprocess of php server

} catch (err) {
console.error(err);
}

});
}
}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/electron-plugin/src/server/api/childProcess.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import {utilityProcess} from 'electron';
import {utilityProcess, UtilityProcess} from 'electron';
import state from '../state.js';
import {notifyLaravel} from "../utils.js";
import {getAppPath, getDefaultEnvironmentVariables, getDefaultPhpIniSettings, runningSecureBuild} from "../php.js";
Expand Down Expand Up @@ -207,7 +207,7 @@ export function stopAllProcesses() {
}
}

function getProcess(alias) {
function getProcess(alias: string): UtilityProcess | undefined {
return state.processes[alias]?.proc;
}

Expand Down
13 changes: 12 additions & 1 deletion resources/js/electron-plugin/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
} from "./php.js";
import { appendCookie } from "./utils.js";
import state from "./state.js";
import { ChildProcess } from "child_process";

let schedulerProcess: ChildProcess | null = null;

export async function startPhpApp() {
const result = await serveApp(
Expand All @@ -23,7 +26,15 @@ export async function startPhpApp() {
}

export function runScheduler() {
startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
killScheduler();
schedulerProcess = startScheduler(state.randomSecret, state.electronApiPort, state.phpIni);
}

export function killScheduler() {
if (schedulerProcess && !schedulerProcess.killed) {
schedulerProcess.kill();
schedulerProcess = null;
}
}

export function startAPI(): Promise<APIProcess> {
Expand Down