修复文件判断引起服务异常

This commit is contained in:
whyour 2021-11-12 14:22:21 +08:00
parent 18cd7bfa6f
commit 7b7bcf600d

View File

@ -13,8 +13,19 @@ interface Dbs {
const db: Dbs = {} as any; const db: Dbs = {} as any;
async function truncateDb() { async function fileExist(file: any) {
return new Promise((resolve) => { return new Promise((resolve) => {
try {
fs.accessSync(file);
resolve(true);
} catch (error) {
resolve(false);
}
});
}
async function truncateDb() {
return new Promise(async (resolve) => {
const files = [ const files = [
config.cronDbFile, config.cronDbFile,
config.dependenceDbFile, config.dependenceDbFile,
@ -24,7 +35,8 @@ async function truncateDb() {
]; ];
for (const file of files) { for (const file of files) {
if (fs.statSync(file).size >= 1024 * 1024 * 500) { const _fileExist = await fileExist(file);
if (_fileExist && fs.statSync(file).size >= 1024 * 1024 * 500) {
fs.truncateSync(file, 1024 * 1024 * 500); fs.truncateSync(file, 1024 * 1024 * 500);
} }
} }
@ -35,11 +47,14 @@ export default async () => {
try { try {
await truncateDb(); await truncateDb();
db.cronDb = new DataStore({ filename: config.cronDbFile }); db.cronDb = new DataStore({ filename: config.cronDbFile, autoload: true });
db.dependenceDb = new DataStore({ filename: config.dependenceDbFile }); db.dependenceDb = new DataStore({
db.envDb = new DataStore({ filename: config.envDbFile }); filename: config.dependenceDbFile,
db.appDb = new DataStore({ filename: config.appDbFile }); autoload: true,
db.authDb = new DataStore({ filename: config.authDbFile }); });
db.envDb = new DataStore({ filename: config.envDbFile, autoload: true });
db.appDb = new DataStore({ filename: config.appDbFile, autoload: true });
db.authDb = new DataStore({ filename: config.authDbFile, autoload: true });
// compaction data file // compaction data file
db.cronDb.persistence.compactDatafile(); db.cronDb.persistence.compactDatafile();
@ -48,22 +63,6 @@ export default async () => {
db.appDb.persistence.compactDatafile(); db.appDb.persistence.compactDatafile();
db.authDb.persistence.compactDatafile(); db.authDb.persistence.compactDatafile();
db.cronDb.loadDatabase((err) => {
if (err) throw err;
});
db.envDb.loadDatabase((err) => {
if (err) throw err;
});
db.dependenceDb.loadDatabase((err) => {
if (err) throw err;
});
db.appDb.loadDatabase((err) => {
if (err) throw err;
});
db.authDb.loadDatabase((err) => {
if (err) throw err;
});
Logger.info('✌️ DB loaded'); Logger.info('✌️ DB loaded');
} catch (error) { } catch (error) {
Logger.info('✌️ DB load failed'); Logger.info('✌️ DB load failed');