Skip to content
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

#1267 のリファクタリング + ポートの割り当て可能の判定を修正 #1317

Merged
merged 12 commits into from
Jun 2, 2023
40 changes: 21 additions & 19 deletions src/background/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { app, BrowserWindow, dialog } from "electron";

import log from "electron-log";
import { z } from "zod";
import { PortManager } from "./portManager";
import {
findAltPort,
getPidFromPort,
getProcessNameFromPid,
url2HostInfo,
} from "./portManager";
import { ipcMainSend } from "@/electron/ipc";

import {
Expand Down Expand Up @@ -229,48 +234,45 @@ export class EngineManager {
return;
}

const engineInfoUrl = new URL(engineInfo.host);
const portManager = new PortManager(
engineInfoUrl.hostname,
parseInt(engineInfoUrl.port)
);
// { hostname (localhost), port (50021) } <- url (http://localhost:50021)
const engineHostInfo = url2HostInfo(new URL(engineInfo.host));

log.info(
`ENGINE ${engineId}: Checking whether port ${engineInfoUrl.port} is assignable...`
`ENGINE ${engineId}: Checking whether port ${engineHostInfo.port} is assignable...`
);

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

// 代替ポート検索
const altPort = await portManager.findAltPort();
// 代替ポートの検索
const altPort = await findAltPort(engineHostInfo);

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

// 代替ポートの情報
this.altPortInfo[engineId] = {
from: parseInt(engineInfoUrl.port),
from: engineHostInfo.port,
to: altPort,
};

// 代替ポートを設定
engineInfo.host = `http://${engineInfoUrl.hostname}:${altPort}`;
engineInfo.host = `http://${engineHostInfo.hostname}:${altPort}`;
log.warn(
`ENGINE ${engineId}: Applied Alternative Port: ${engineInfoUrl.port} -> ${altPort}`
`ENGINE ${engineId}: Applied Alternative Port: ${engineHostInfo.port} -> ${altPort}`
);
}

Expand Down
Loading