feat: make infrequent backup of setting file, preventing re-install/upgrade from corrupting the file.

This commit is contained in:
linonetwo 2024-02-05 01:42:51 +08:00
parent 164bae83bc
commit 77efa1ad2c
5 changed files with 27 additions and 21 deletions

View file

@ -67,6 +67,7 @@
"nanoid": "^5.0.4",
"node-fetch": "3.3.2",
"reflect-metadata": "0.2.1",
"rotating-file-stream": "^3.2.1",
"rxjs": "7.8.1",
"semver": "7.5.4",
"source-map-support": "0.5.21",

26
pnpm-lock.yaml generated
View file

@ -117,6 +117,9 @@ dependencies:
reflect-metadata:
specifier: 0.2.1
version: 0.2.1
rotating-file-stream:
specifier: ^3.2.1
version: 3.2.1
rxjs:
specifier: 7.8.1
version: 7.8.1
@ -185,9 +188,6 @@ optionalDependencies:
'@electron-forge/maker-zip':
specifier: 7.2.0
version: 7.2.0
registry-js:
specifier: 1.15.1
version: 1.15.1
sqlite-vss-darwin-arm64:
specifier: 0.1.2
version: 0.1.2
@ -8215,12 +8215,6 @@ packages:
resolution: {integrity: sha512-AGK2yQKIjRuqnc6VkX2Xj5d+QW8xZ87pa1UK6yA6ouUyuxfHuMP6umE5QK7UmTeOAymo+Zx1Fxiuw9rVx8taHQ==}
dev: true
/node-addon-api@3.2.1:
resolution: {integrity: sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==}
requiresBuild: true
dev: false
optional: true
/node-api-version@0.1.4:
resolution: {integrity: sha512-KGXihXdUChwJAOHO53bv9/vXcLmdUsZ6jIptbvYvkpKfth+r7jw44JkVxQFA3kX5nQjzjmGu1uAu/xNNLNlI5g==}
dependencies:
@ -9320,15 +9314,6 @@ packages:
engines: {node: '>=8'}
dev: true
/registry-js@1.15.1:
resolution: {integrity: sha512-aHEHtbzqusxkhprn7+rf2Cu7z0oPHaXVuOVKTpZigBSRpXUTw7+yWBJyuAREpG1bt2doe+qJzSR+kyZ9uWsj7A==}
requiresBuild: true
dependencies:
node-addon-api: 3.2.1
prebuild-install: 7.1.1
dev: false
optional: true
/regjsparser@0.10.0:
resolution: {integrity: sha512-qx+xQGZVsy55CH0a1hiVwHmqjLryfh7wQyF5HO07XJ9f7dQMY/gPQHhlyDkIzJKC+x2fUCpCcUODUUUFrm7SHA==}
hasBin: true
@ -9498,6 +9483,11 @@ packages:
sprintf-js: 1.1.2
optional: true
/rotating-file-stream@3.2.1:
resolution: {integrity: sha512-n2B18CJb+n2VA5Tdle+1NP2toEcRv68CjAOBjHmwcyswNwMVsrN3gVRZ9ymH3sapaiGY8jc9OhhV5b6I5rAeiA==}
engines: {node: '>=14.0'}
dev: false
/run-applescript@5.0.0:
resolution: {integrity: sha512-XcT5rBksx1QdIhlFOCtgZkB99ZEouFZ1E2Kc2LHqNW13U3/74YGdkQRmThTwxy4QIyookibDKYZOPqX//6BlAg==}
engines: {node: '>=12'}

View file

@ -7,3 +7,4 @@ export const DELAY_MENU_REGISTER = 500;
* debounce the usage of electron-settings, to prevent corrupting the file, and improve performance.
*/
export const DEBOUNCE_SAVE_SETTING_FILE = 500;
export const DEBOUNCE_SAVE_SETTING_BACKUP_FILE = 3000;

View file

@ -5,10 +5,11 @@ import fs from 'fs-extra';
import { injectable } from 'inversify';
import { debounce } from 'lodash';
import path from 'path';
import * as rotateFs from 'rotating-file-stream';
import { DataSource } from 'typeorm';
import { CACHE_DATABASE_FOLDER } from '@/constants/appPaths';
import { DEBOUNCE_SAVE_SETTING_FILE } from '@/constants/parameters';
import { DEBOUNCE_SAVE_SETTING_BACKUP_FILE, DEBOUNCE_SAVE_SETTING_FILE } from '@/constants/parameters';
import { PACKAGE_PATH_BASE, SQLITE_BINARY_PATH } from '@/constants/paths';
import { logger } from '@services/libs/log';
import { fixSettingFileWhenError } from './configSetting';
@ -201,10 +202,17 @@ export class DatabaseService implements IDatabaseService {
}
private settingFileContent: ISettingFile = settings.getSync() as unknown as ISettingFile || {};
private readonly settingBackupStream = rotateFs.createStream(`${settings.file()}.bak`, {
size: '10M',
interval: '1d',
maxFiles: 3,
});
public setSetting<K extends keyof ISettingFile>(key: K, value: ISettingFile[K]) {
this.settingFileContent[key] = value;
void this.debouncedStoreSettingsToFile();
// make infrequent backup of setting file, preventing re-install/upgrade from corrupting the file.
void this.debouncedStoreSettingsToBackupFile();
}
public setSettingImmediately<K extends keyof ISettingFile>(key: K, value: ISettingFile[K]) {
@ -217,7 +225,13 @@ export class DatabaseService implements IDatabaseService {
}
private readonly debouncedStoreSettingsToFile = debounce(this.immediatelyStoreSettingsToFile.bind(this), DEBOUNCE_SAVE_SETTING_FILE);
private readonly debouncedStoreSettingsToBackupFile = debounce(this.immediatelyStoreSettingsToBackupFile.bind(this), DEBOUNCE_SAVE_SETTING_BACKUP_FILE);
private storeSettingsToFileLock = false;
public immediatelyStoreSettingsToBackupFile() {
this.settingBackupStream.write(JSON.stringify(this.settingFileContent) + '\n', 'utf8');
}
public async immediatelyStoreSettingsToFile() {
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any */
try {

View file

@ -47,8 +47,8 @@ export const windowDimension: Record<WindowNames, { height?: number; width?: num
height: 768,
},
[WindowNames.menuBar]: {
width: 400,
height: 500,
width: 500,
height: 600,
},
[WindowNames.about]: {
width: 400,