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

サードパーティがエンジンへのアクセス情報を得るための設定書き出し機能 #1765

Merged
merged 21 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0972e5e
未対応エンジン追加時にリストが消える件(#1168)
nmori Feb 5, 2023
cb5552c
lintチェックエラー部分の修正
nmori Feb 5, 2023
00b7673
コードレビューの反映 (ref #1179)
nmori Feb 5, 2023
4b21b85
コードレビュー分の反映② ref #1179
nmori Feb 5, 2023
ad8c1de
Merge branch 'main' of https://github.com/voicevox/voicevox
nmori Feb 9, 2023
6e91471
* コンフリクトの修正
nmori Feb 10, 2023
4e8cde0
Merge branch 'main' of https://github.com/voicevox/voicevox
nmori Feb 11, 2023
3ce28d9
Merge branch 'main' of https://github.com/VOICEVOX/voicevox
nmori Feb 18, 2023
a72f3b7
Merge branch 'main' of https://github.com/VOICEVOX/voicevox
nmori Feb 20, 2023
aac80a5
Merge branch 'main' of https://github.com/VOICEVOX/voicevox
nmori Feb 23, 2023
a37c7a2
Merge branch 'main' of https://github.com/VOICEVOX/voicevox
nmori Jan 26, 2024
80008bc
サードパーティがエンジンへのアクセス情報を得るための設定書き出し機能(ref #1738)
nmori Jan 26, 2024
351a3ab
* 関数名の変更 : writeEngineInfoFor3rdParty
nmori Jan 27, 2024
7cff6f0
* コンストラクタ引数でファイルパスを渡すように
nmori Jan 30, 2024
c93a819
* エクスポートファイパスを渡す所を引数にした
nmori Feb 1, 2024
af2cad2
議論 #1738 に基づき、最小項目の書き出しに変更
nmori Feb 8, 2024
94d56f8
* ファイル書き出しクラスに機能を集約
nmori Feb 12, 2024
a824ee3
RuntimeInfoManager.tsをブラッシュアップ
Hiroshiba Feb 13, 2024
437c87e
EngineManagerとRuntimeInfoManagerを疎結合に
Hiroshiba Feb 13, 2024
ddfda40
データ構造調整、テスト追加
Hiroshiba Feb 13, 2024
762bc6a
Apply suggestions from code review
Hiroshiba Feb 13, 2024
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
7 changes: 7 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import EngineManager from "./background/engineManager";
import VvppManager, { isVvppFile } from "./background/vvppManager";
import configMigration014 from "./background/configMigration014";
import { failure, success } from "./type/result";
import { RuntimeInfoManager } from "./shared/RuntimeInfoManager";
import { ipcMainHandle, ipcMainSend } from "@/electron/ipc";
import { getConfigManager } from "@/background/electronConfig";

Expand Down Expand Up @@ -145,6 +146,11 @@ if (!fs.existsSync(vvppEngineDir)) {
fs.mkdirSync(vvppEngineDir);
}

const runtimeInfoManager = new RuntimeInfoManager(
path.join(app.getPath("userData"), "runtime-info.json"),
app.getVersion()
);
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => {
const engineId = engineInfo.uuid;
log.error(`ENGINE ${engineId} ERROR: ${error}`);
Expand All @@ -167,6 +173,7 @@ const engineManager = new EngineManager({
defaultEngineDir: appDirPath,
vvppEngineDir,
onEngineProcessError,
runtimeInfoManager,
});
const vvppManager = new VvppManager({ vvppEngineDir });

Expand Down
12 changes: 11 additions & 1 deletion src/background/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/shared/ConfigManager";
import { RuntimeInfoManager } from "@/shared/RuntimeInfoManager";

type EngineProcessContainer = {
willQuitEngine: boolean;
Expand Down Expand Up @@ -60,6 +61,7 @@ export class EngineManager {
defaultEngineDir: string;
vvppEngineDir: string;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
runtimeInfoManager: RuntimeInfoManager;

defaultEngineInfos: EngineInfo[] = [];
additionalEngineInfos: EngineInfo[] = [];
Expand All @@ -72,17 +74,19 @@ export class EngineManager {
defaultEngineDir,
vvppEngineDir,
onEngineProcessError,
runtimeInfoManager,
}: {
configManager: BaseConfigManager;
defaultEngineDir: string;
vvppEngineDir: string;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
runtimeInfoManager: RuntimeInfoManager;
}) {
this.configManager = configManager;
this.defaultEngineDir = defaultEngineDir;
this.vvppEngineDir = vvppEngineDir;
this.onEngineProcessError = onEngineProcessError;

this.runtimeInfoManager = runtimeInfoManager;
this.engineProcessContainers = {};
}

Expand Down Expand Up @@ -210,6 +214,8 @@ export class EngineManager {
log.info(`ENGINE ${engineInfo.uuid}: Start launching`);
await this.runEngine(engineInfo.uuid);
}
this.runtimeInfoManager.setEngineInfos(engineInfos);
await this.runtimeInfoManager.exportFile();
}

/**
Expand Down Expand Up @@ -469,6 +475,8 @@ export class EngineManager {
);

this.runEngine(engineId);
this.runtimeInfoManager.setEngineInfos(this.fetchEngineInfos());
this.runtimeInfoManager.exportFile();
resolve();
return;
}
Expand All @@ -482,6 +490,8 @@ export class EngineManager {
log.info(`ENGINE ${engineId}: Process killed. Restarting process...`);

this.runEngine(engineId);
this.runtimeInfoManager.setEngineInfos(this.fetchEngineInfos());
this.runtimeInfoManager.exportFile();
resolve();
};

Expand Down
136 changes: 136 additions & 0 deletions src/shared/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import fs from "fs";
import AsyncLock from "async-lock";
import log from "electron-log/main";
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
import { EngineId, EngineInfo } from "@/type/preload";

/**
* サードパーティ向けランタイム情報のエクスポート情報クラス
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
*/
export class RuntimeInfoManager {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
private runtimeInfoPath: string;
private appVersion: string;

constructor(userDataPath: string, appVersion: string) {
this.runtimeInfoPath = userDataPath;
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
this.appVersion = appVersion;
}

/**
* サードパーティ向けランタイム情報のファイルパスを取得する
*/
public getRuntimeInfoPath(): string {
return this.runtimeInfoPath;
}

Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
/**
* ファイルロック用のインスタンス
*/
private lock = new AsyncLock({
timeout: 1000,
});
private lockKey = "write";

/**
* サードパーティ向けランタイム情報のフォーマットバージョン
* Note: 破壊的変更があった場合に数字を上げること
*/
private fileFormatVersion = 1;

/**
* エンジン情報(書き出し用に記憶)
*/
private engineInfos: EngineInfo[] = [];

/**
* エンジン情報を登録する
*/
public setEngineInfos(engineInfos: EngineInfo[]) {
this.engineInfos = engineInfos;
}

/**
* サードパーティ向けの設定ファイルを書き出す
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
*/
public async exportFile() {
await this.lock.acquire(this.lockKey, async () => {
log.info(
`Runtime information file has been updated. : ${this.runtimeInfoPath}`
);

const outputInfoData = new OutputInfoDataFor3rdParty(
this.appVersion,
this.fileFormatVersion
);

// ファイルに書き出すデータをつくる
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
try {
await fs.promises.writeFile(
this.runtimeInfoPath,
JSON.stringify(outputInfoData.getOutputInfoData(this.engineInfos))
);
} catch (e) {
// ディスクの空き容量がない、他ツールからのファイルロック時をトラップ。
// サードパーティ向けなのでVOICEVOX側には通知せず、エラー記録して継続
log.error(`Failed to write file : ${e}`);
}
});
}
}

/**
* サードパーティ向けエンジン情報のレコード定義
* Note:変更時はRuntimeInfoManager.fileFormatVersionも変更すること
*/
export interface EngineInfoRecordFor3rdParty {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
uuid: EngineId;
url: string;
name: string;
}

/**
* サードパーティ向けランタイム情報のレコード定義
* Note:変更時はRuntimeInfoManager.fileFormatVersionも変更すること
*/
export interface RuntimeInfoFormatFor3rdParty {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
formatVersion: number;
appVersion: string;
engineInfos: EngineInfoRecordFor3rdParty[];
}

/**
* サードパーティ向けランタイム情報のエクスポートフォーマットクラス
*/
export class OutputInfoDataFor3rdParty {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
private appVersion: string;
private fileFormatVersion: number;

constructor(appVersion: string, fileFormatVersion: number) {
this.appVersion = appVersion;
this.fileFormatVersion = fileFormatVersion;
}

/**
* サードパーティ向けに提供するデータを取得
*/
public getOutputInfoData(
engineInfos: EngineInfo[]
): RuntimeInfoFormatFor3rdParty {
const engineInfoList: EngineInfoRecordFor3rdParty[] = engineInfos.map(
(engineInfo) => {
return {
uuid: engineInfo.uuid,
url: engineInfo.host,
name: engineInfo.name,
};
}
);

const engineInfoFormatFor3rdParty: RuntimeInfoFormatFor3rdParty = {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
formatVersion: this.fileFormatVersion,
appVersion: this.appVersion,
engineInfos: engineInfoList,
};

return engineInfoFormatFor3rdParty;
}
}
Loading