Skip to content

Commit

Permalink
Fix swiftray process not terminated in windows (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkdkwizard authored Nov 5, 2024
1 parent 55a7df2 commit 9a55d6f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/node/backend-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import os from 'os';
import path from 'path';
import { ChildProcess, spawn } from 'child_process';
import { ChildProcess, execSync, spawn } from 'child_process';

import WebSocket from 'ws';
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -182,7 +182,7 @@ class BackendManager extends EventEmitter {
let swiftrayExec: string;
if (os.platform() === 'win32') {
swiftrayDir = path.join(process.env.BACKEND_ROOT, 'swiftray');
swiftrayExec = 'swiftray.exe';
swiftrayExec = 'Swiftray.exe';
} else if (os.platform() === 'darwin') {
swiftrayDir = path.join(process.env.BACKEND_ROOT, 'Swiftray.app', 'Contents', 'MacOS');
swiftrayExec = 'Swiftray';
Expand All @@ -202,7 +202,7 @@ class BackendManager extends EventEmitter {
let swiftrayExec: string;
if (os.platform() === 'win32') {
swiftrayDir = path.join(process.env.BACKEND_ROOT, 'swiftray');
swiftrayExec = 'swiftray.exe';
swiftrayExec = 'Swiftray.exe';
this.swiftrayProc = spawn(`"${swiftrayExec}"`, ['--daemon'], { shell: true, cwd: swiftrayDir });
} else if (os.platform() === 'darwin') {
swiftrayDir = path.join(process.env.BACKEND_ROOT, 'Swiftray.app', 'Contents', 'MacOS');
Expand All @@ -226,6 +226,18 @@ class BackendManager extends EventEmitter {
}
}

killSwiftray(): void {
if (this.swiftrayProc) {
if (os.platform() === 'win32' && this.swiftrayProc.pid) {
console.log(this.swiftrayProc.pid);
const res = execSync(`taskkill /PID ${this.swiftrayProc.pid} /T /F`);
console.log(res.toString());
} else {
this.swiftrayProc.kill();
}
}
}

start(): void {
if (!this.isRunning) {
this.isRunning = true;
Expand All @@ -238,7 +250,7 @@ class BackendManager extends EventEmitter {
if (this.isRunning) {
this.isRunning = false;
this.proc?.kill();
this.swiftrayProc?.kill();
this.killSwiftray();
}
}

Expand Down

0 comments on commit 9a55d6f

Please sign in to comment.