Skip to content

Commit ccf3ff0

Browse files
#1267 のリファクタリング + ポートの割り当て可能の判定を修正 (#1317)
Co-authored-by: sabonerune <[email protected]>
1 parent 39d52fc commit ccf3ff0

File tree

9 files changed

+298
-189
lines changed

9 files changed

+298
-189
lines changed

src/background.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ async function start() {
524524
}
525525
store.set("engineSettings", engineSettings);
526526

527-
await createWindow();
528527
await engineManager.runEngineAll(win);
528+
await createWindow();
529529
}
530530

531531
const menuTemplateForMac: Electron.MenuItemConstructorOptions[] = [

src/background/engineManager.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { app, BrowserWindow, dialog } from "electron";
99

1010
import log from "electron-log";
1111
import { z } from "zod";
12-
import { PortManager } from "./portManager";
12+
import {
13+
findAltPort,
14+
getPidFromPort,
15+
getProcessNameFromPid,
16+
url2HostInfo,
17+
} from "./portManager";
1318
import { ipcMainSend } from "@/electron/ipc";
1419

1520
import {
@@ -229,48 +234,45 @@ export class EngineManager {
229234
return;
230235
}
231236

232-
const engineInfoUrl = new URL(engineInfo.host);
233-
const portManager = new PortManager(
234-
engineInfoUrl.hostname,
235-
parseInt(engineInfoUrl.port)
236-
);
237+
// { hostname (localhost), port (50021) } <- url (http://localhost:50021)
238+
const engineHostInfo = url2HostInfo(new URL(engineInfo.host));
237239

238240
log.info(
239-
`ENGINE ${engineId}: Checking whether port ${engineInfoUrl.port} is assignable...`
241+
`ENGINE ${engineId}: Checking whether port ${engineHostInfo.port} is assignable...`
240242
);
241243

242-
// ポートを既に割り当てられているプロセスidの取得: undefined → ポートが空いている
243-
const processId = await portManager.getProcessIdFromPort();
244-
if (processId !== undefined) {
245-
const processName = await portManager.getProcessNameFromPid(processId);
244+
// ポートを既に割り当てているプロセスidの取得: undefined → ポートが空いている
245+
const pid = await getPidFromPort(engineHostInfo);
246+
if (pid != undefined) {
247+
const processName = await getProcessNameFromPid(engineHostInfo, pid);
246248
log.warn(
247-
`ENGINE ${engineId}: Port ${engineInfoUrl.port} has already been assigned by ${processName} (pid=${processId})`
249+
`ENGINE ${engineId}: Port ${engineHostInfo.port} has already been assigned by ${processName} (pid=${pid})`
248250
);
249251

250-
// 代替ポート検索
251-
const altPort = await portManager.findAltPort();
252+
// 代替ポートの検索
253+
const altPort = await findAltPort(engineHostInfo);
252254

253255
// 代替ポートが見つからないとき
254-
if (!altPort) {
256+
if (altPort == undefined) {
255257
log.error(`ENGINE ${engineId}: No Alternative Port Found`);
256258
dialog.showErrorBox(
257259
`${engineInfo.name} の起動に失敗しました`,
258-
`${engineInfoUrl.port}番ポートの代わりに利用可能なポートが見つかりませんでした。PCを再起動してください。`
260+
`${engineHostInfo.port}番ポートの代わりに利用可能なポートが見つかりませんでした。PCを再起動してください。`
259261
);
260262
app.exit(1);
261263
throw new Error("No Alternative Port Found");
262264
}
263265

264266
// 代替ポートの情報
265267
this.altPortInfo[engineId] = {
266-
from: parseInt(engineInfoUrl.port),
268+
from: engineHostInfo.port,
267269
to: altPort,
268270
};
269271

270272
// 代替ポートを設定
271-
engineInfo.host = `http://${engineInfoUrl.hostname}:${altPort}`;
273+
engineInfo.host = `${engineHostInfo.protocol}//${engineHostInfo.hostname}:${altPort}`;
272274
log.warn(
273-
`ENGINE ${engineId}: Applied Alternative Port: ${engineInfoUrl.port} -> ${altPort}`
275+
`ENGINE ${engineId}: Applied Alternative Port: ${engineHostInfo.port} -> ${altPort}`
274276
);
275277
}
276278

0 commit comments

Comments
 (0)