Skip to content

Commit

Permalink
refactor: エンジン情報のキャッシュを作らないように変更 (#2272)
Browse files Browse the repository at this point in the history
* エンジン情報のキャッシュを作らないように変更

* コメント変更

* 図の変更忘れ
  • Loading branch information
Hiroshiba authored Sep 30, 2024
1 parent f8d966f commit a8cd233
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/res/起動シーケンス図.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ flowchart
870482["store.get registeredEngineDirs"] --> 250263
222321 --> 967432["engine.runEngineAll"]
656570["engine.fetchEngineInfos"] --> 870482
110954["engine.initializeEngineInfosAndAltPortInfo"] --> 656570
110954["engine.initializeAltPortInfo"] --> 656570
967432 --> 302398["runtimeInfo.setEngineInfos"]
302398 --> 321984
subgraph 656570["engine.fetchEngineInfos"]
Expand Down
4 changes: 2 additions & 2 deletions src/backend/electron/engineAndVvppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export class EngineAndVvppController {

// エンジンの準備と起動
async launchEngines() {
// エンジンの追加と削除を反映させるためEngineInfoとAltPortInfosを再生成する
this.engineInfoManager.initializeEngineInfosAndAltPortInfo();
// AltPortInfosを再生成する
this.engineInfoManager.initializeAltPortInfo();

// TODO: デフォルトエンジンの処理をConfigManagerに移してブラウザ版と共通化する
const engineInfos = this.engineInfoManager.fetchEngineInfos();
Expand Down
35 changes: 20 additions & 15 deletions src/backend/electron/manager/engineInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/backend/common/ConfigManager";

/**
* デフォルトエンジンの情報を作成する
* デフォルトエンジンの情報を取得する
*/
function createDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] {
function fetchDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] {
// TODO: envから直接ではなく、envに書いたengine_manifest.jsonから情報を得るようにする
const defaultEngineInfosEnv =
import.meta.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]";
Expand Down Expand Up @@ -48,9 +48,6 @@ export class EngineInfoManager {
defaultEngineDir: string;
vvppEngineDir: string;

defaultEngineInfos: EngineInfo[] = [];
additionalEngineInfos: EngineInfo[] = [];

/** 代替ポート情報 */
public altPortInfos: AltPortInfos = {};

Expand All @@ -65,10 +62,10 @@ export class EngineInfoManager {
}

/**
* 追加エンジンの一覧を作成する
* 追加エンジンの一覧を取得する
* FIXME: store.get("registeredEngineDirs")への副作用をEngineManager外に移動する
*/
private createAdditionalEngineInfos(): EngineInfo[] {
private fetchAdditionalEngineInfos(): EngineInfo[] {
const engines: EngineInfo[] = [];
const addEngine = (engineDir: string, type: "vvpp" | "path") => {
const manifestPath = path.join(engineDir, "engine_manifest.json");
Expand Down Expand Up @@ -139,7 +136,20 @@ export class EngineInfoManager {
* 全てのエンジンの一覧を取得する。デフォルトエンジン+追加エンジン。
*/
fetchEngineInfos(): EngineInfo[] {
return [...this.defaultEngineInfos, ...this.additionalEngineInfos];
const engineInfos = [
...fetchDefaultEngineInfos(this.defaultEngineDir),
...this.fetchAdditionalEngineInfos(),
];
// 代替ポートに置き換える
engineInfos.forEach((engineInfo) => {
const altPortInfo = this.altPortInfos[engineInfo.uuid];
if (altPortInfo) {
const url = new URL(engineInfo.host);
url.port = altPortInfo.to.toString();
engineInfo.host = url.origin;
}
});
return engineInfos;
}

/**
Expand Down Expand Up @@ -170,11 +180,9 @@ export class EngineInfoManager {
}

/**
* EngineInfosとAltPortInfoを初期化する
* AltPortInfoを初期化する
*/
initializeEngineInfosAndAltPortInfo() {
this.defaultEngineInfos = createDefaultEngineInfos(this.defaultEngineDir);
this.additionalEngineInfos = this.createAdditionalEngineInfos();
initializeAltPortInfo() {
this.altPortInfos = {};
}

Expand All @@ -189,9 +197,6 @@ export class EngineInfoManager {
from: Number(url.port),
to: port,
};

url.port = port.toString();
engineInfo.host = url.toString();
}

/**
Expand Down
27 changes: 12 additions & 15 deletions src/backend/electron/manager/engineProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,34 @@ export class EngineProcessManager {
// { hostname (localhost), port (50021) } <- url (http://localhost:50021)
const engineHostInfo = url2HostInfo(new URL(engineInfo.host));

// ポートが塞がっていれば代替ポートを探す
let port = engineHostInfo.port;
log.info(
`ENGINE ${engineId}: Checking whether port ${engineHostInfo.port} is assignable...`,
`ENGINE ${engineId}: Checking whether port ${port} is assignable...`,
);

if (
!(await isAssignablePort(engineHostInfo.port, engineHostInfo.hostname))
) {
if (!(await isAssignablePort(port, engineHostInfo.hostname))) {
// ポートを既に割り当てているプロセスidの取得
const pid = await getPidFromPort(engineHostInfo);
if (pid != undefined) {
const processName = await getProcessNameFromPid(engineHostInfo, pid);
log.warn(
`ENGINE ${engineId}: Port ${engineHostInfo.port} has already been assigned by ${processName} (pid=${pid})`,
`ENGINE ${engineId}: Port ${port} has already been assigned by ${processName} (pid=${pid})`,
);
} else {
// ポートは使用不可能だがプロセスidは見つからなかった
log.warn(
`ENGINE ${engineId}: Port ${engineHostInfo.port} was unavailable`,
);
log.warn(`ENGINE ${engineId}: Port ${port} was unavailable`);
}

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

// 代替ポートが見つからないとき
if (altPort == undefined) {
log.error(`ENGINE ${engineId}: No Alternative Port Found`);
dialog.showErrorBox(
`${engineInfo.name} の起動に失敗しました`,
`${engineHostInfo.port}番ポートの代わりに利用可能なポートが見つかりませんでした。PCを再起動してください。`,
`${port}番ポートの代わりに利用可能なポートが見つかりませんでした。PCを再起動してください。`,
);
app.exit(1);
throw new Error("No Alternative Port Found");
Expand All @@ -129,8 +124,10 @@ export class EngineProcessManager {
// 代替ポート情報を更新
this.engineAltPortUpdater(engineId, altPort);
log.warn(
`ENGINE ${engineId}: Applied Alternative Port: ${engineHostInfo.port} -> ${altPort}`,
`ENGINE ${engineId}: Applied Alternative Port: ${port} -> ${altPort}`,
);

port = altPort;
}

log.info(`ENGINE ${engineId}: Starting process`);
Expand All @@ -157,7 +154,7 @@ export class EngineProcessManager {
"--host",
new URL(engineInfo.host).hostname,
"--port",
new URL(engineInfo.host).port,
port.toString(),
]);

log.info(`ENGINE ${engineId} path: ${enginePath}`);
Expand Down
2 changes: 1 addition & 1 deletion src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export type MinimumEngineManifestType = z.infer<

export type EngineInfo = {
uuid: EngineId;
host: string;
host: string; // NOTE: 実際はorigin(プロトコルとhostnameとport)が入る
name: string;
path?: string; // エンジンディレクトリのパス
executionEnabled: boolean;
Expand Down

0 comments on commit a8cd233

Please sign in to comment.