Skip to content

Commit

Permalink
* ファイル書き出しクラスに機能を集約
Browse files Browse the repository at this point in the history
* 変数名、コメントの修正
  • Loading branch information
nmori committed Feb 12, 2024
1 parent af2cad2 commit 94d56f8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 64 deletions.
7 changes: 5 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ if (!fs.existsSync(vvppEngineDir)) {
fs.mkdirSync(vvppEngineDir);
}

const runtimeInfoPath = new RuntimeInfoManager(app.getPath("userData"));
const runtimeInfoManager = new RuntimeInfoManager(
path.join(app.getPath("userData"), "runtime-info.json"),
app.getVersion()
);

const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => {
const engineId = engineInfo.uuid;
Expand All @@ -170,7 +173,7 @@ const engineManager = new EngineManager({
defaultEngineDir: appDirPath,
vvppEngineDir,
onEngineProcessError,
runtimeInfoPath,
runtimeInfoManager,
});
const vvppManager = new VvppManager({ vvppEngineDir });

Expand Down
54 changes: 11 additions & 43 deletions src/background/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import fs from "fs";
import treeKill from "tree-kill";
import shlex from "shlex";

import AsyncLock from "async-lock";

import { app, dialog } from "electron"; // FIXME: ここでelectronをimportするのは良くない

import log from "electron-log/main";
Expand All @@ -27,10 +25,7 @@ import {
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/shared/ConfigManager";
import {
OutputInfoDataFor3rdParty,
RuntimeInfoManager,
} from "@/shared/RuntimeInfoManager";
import { RuntimeInfoManager } from "@/shared/RuntimeInfoManager";

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

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

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

await this.writeEngineInfosFor3rdParty();
this.runtimeInfoManager.setEngineInfos(engineInfos);
await this.runtimeInfoManager.exportFile();
}

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

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

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

Expand Down Expand Up @@ -557,35 +554,6 @@ export class EngineManager {
}
return "ok";
}

private lock = new AsyncLock({
timeout: 1000,
});
private lockKey = "write";

/**
* サードパーティ向けの設定ファイルを書き出す
*/
async writeEngineInfosFor3rdParty() {
await this.lock.acquire(this.lockKey, async () => {
log.info(`Engine information file (runtime-info.json) has been updated.`);

const engineInfos = this.fetchEngineInfos();
const outputInfoData = new OutputInfoDataFor3rdParty(app.getVersion());

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

export default EngineManager;
93 changes: 74 additions & 19 deletions src/shared/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import path from "path";
import fs from "fs";
import AsyncLock from "async-lock";
import log from "electron-log/main";
import { EngineId, EngineInfo } from "@/type/preload";

/**
* サードパーティ向けランタイム情報のエクスポート情報クラス
*/
export class RuntimeInfoManager {
private runtimeInfoPath: string;
private appVersion: string;

constructor(userDataPath: string) {
this.runtimeInfoPath = path.join(userDataPath, "runtime-info.json");
constructor(userDataPath: string, appVersion: string) {
this.runtimeInfoPath = userDataPath;
this.appVersion = appVersion;
}

/**
Expand All @@ -16,11 +21,65 @@ export class RuntimeInfoManager {
public getRuntimeInfoPath(): string {
return this.runtimeInfoPath;
}

/**
* ファイルロック用のインスタンス
*/
private lock = new AsyncLock({
timeout: 1000,
});
private lockKey = "write";

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

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

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

/**
* サードパーティ向けの設定ファイルを書き出す
*/
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
);

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

/**
* サードパーティ向けランタイム情報のレコード定義
* Note:変更時はOutputInfoDataFor3rdPartyのバージョン定義も変更すること
* サードパーティ向けエンジン情報のレコード定義
* Note:変更時はRuntimeInfoManager.fileFormatVersionも変更すること
*/
export interface EngineInfoRecordFor3rdParty {
uuid: EngineId;
Expand All @@ -30,9 +89,9 @@ export interface EngineInfoRecordFor3rdParty {

/**
* サードパーティ向けランタイム情報のレコード定義
* Note:変更時はOutputInfoDataFor3rdPartyのバージョン定義も変更すること
* Note:変更時はRuntimeInfoManager.fileFormatVersionも変更すること
*/
export interface EngineInfoFormatFor3rdParty {
export interface RuntimeInfoFormatFor3rdParty {
formatVersion: number;
appVersion: string;
engineInfos: EngineInfoRecordFor3rdParty[];
Expand All @@ -42,24 +101,20 @@ export interface EngineInfoFormatFor3rdParty {
* サードパーティ向けランタイム情報のエクスポートフォーマットクラス
*/
export class OutputInfoDataFor3rdParty {
private VOICEVOXVersion: string;
private appVersion: string;
private fileFormatVersion: number;

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

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

/**
* サードパーティ向けに提供するデータを取得
*/
public getOutputInfoData(
engineInfos: EngineInfo[]
): EngineInfoFormatFor3rdParty {
): RuntimeInfoFormatFor3rdParty {
const engineInfoList: EngineInfoRecordFor3rdParty[] = engineInfos.map(
(engineInfo) => {
return {
Expand All @@ -70,9 +125,9 @@ export class OutputInfoDataFor3rdParty {
}
);

const engineInfoFormatFor3rdParty: EngineInfoFormatFor3rdParty = {
const engineInfoFormatFor3rdParty: RuntimeInfoFormatFor3rdParty = {
formatVersion: this.fileFormatVersion,
appVersion: this.VOICEVOXVersion,
appVersion: this.appVersion,
engineInfos: engineInfoList,
};

Expand Down

0 comments on commit 94d56f8

Please sign in to comment.