mirror of
https://github.com/whyour/qinglong.git
synced 2026-06-30 20:35:09 +08:00
使用sqlite替换nedb
This commit is contained in:
@@ -3,6 +3,12 @@ import config from '../config';
|
||||
import Logger from './logger';
|
||||
import fs from 'fs';
|
||||
import { fileExist } from '../config/util';
|
||||
import { EnvModel } from '../data/env';
|
||||
import { CrontabModel } from '../data/cron';
|
||||
import { DependenceModel } from '../data/dependence';
|
||||
import { AppModel } from '../data/open';
|
||||
import { AuthModel } from '../data/auth';
|
||||
import { sequelize } from '../data';
|
||||
|
||||
interface Dbs {
|
||||
cronDb: DataStore;
|
||||
@@ -53,6 +59,43 @@ export default async () => {
|
||||
db.appDb.persistence.compactDatafile();
|
||||
db.authDb.persistence.compactDatafile();
|
||||
|
||||
try {
|
||||
await sequelize.sync();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// migrate db to sqlite
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const count = await CrontabModel.count();
|
||||
if (count !== 0) {
|
||||
return;
|
||||
}
|
||||
db.cronDb.find({}).exec(async (err, docs) => {
|
||||
await CrontabModel.bulkCreate(docs);
|
||||
});
|
||||
|
||||
db.dependenceDb.find({}).exec(async (err, docs) => {
|
||||
await DependenceModel.bulkCreate(docs);
|
||||
});
|
||||
|
||||
db.envDb.find({}).exec(async (err, docs) => {
|
||||
await EnvModel.bulkCreate(docs);
|
||||
});
|
||||
|
||||
db.appDb.find({}).exec(async (err, docs) => {
|
||||
await AppModel.bulkCreate(docs);
|
||||
});
|
||||
|
||||
db.authDb.find({}).exec(async (err, docs) => {
|
||||
await AuthModel.bulkCreate(docs);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
Logger.info('✌️ DB loaded');
|
||||
} catch (error) {
|
||||
Logger.info('✌️ DB load failed');
|
||||
|
||||
+22
-41
@@ -1,33 +1,31 @@
|
||||
import DependenceService from '../services/dependence';
|
||||
import { exec } from 'child_process';
|
||||
import { Container } from 'typedi';
|
||||
import { Crontab, CrontabStatus } from '../data/cron';
|
||||
import { Crontab, CrontabModel, CrontabStatus } from '../data/cron';
|
||||
import CronService from '../services/cron';
|
||||
import EnvService from '../services/env';
|
||||
import _ from 'lodash';
|
||||
import { dbs } from '../loaders/db';
|
||||
import { DependenceModel } from '../data/dependence';
|
||||
import { Op } from 'sequelize';
|
||||
|
||||
export default async () => {
|
||||
const cronService = Container.get(CronService);
|
||||
const envService = Container.get(EnvService);
|
||||
const dependenceService = Container.get(DependenceService);
|
||||
const cronDb = dbs.cronDb;
|
||||
const dependenceDb = dbs.dependenceDb;
|
||||
|
||||
// 初始化更新所有任务状态为空闲
|
||||
cronDb.update(
|
||||
{ status: { $in: [CrontabStatus.running, CrontabStatus.queued] } },
|
||||
{ $set: { status: CrontabStatus.idle } },
|
||||
{ multi: true },
|
||||
await CrontabModel.update(
|
||||
{ status: CrontabStatus.idle },
|
||||
{ where: { status: [CrontabStatus.running, CrontabStatus.queued] } },
|
||||
);
|
||||
|
||||
// 初始化时安装所有处于安装中,安装成功,安装失败的依赖
|
||||
dependenceDb.find({ status: { $in: [0, 1, 2] } }).exec(async (err, docs) => {
|
||||
DependenceModel.findAll({ where: {} }).then(async (docs) => {
|
||||
const groups = _.groupBy(docs, 'type');
|
||||
for (const key in groups) {
|
||||
if (Object.prototype.hasOwnProperty.call(groups, key)) {
|
||||
const group = groups[key];
|
||||
const depIds = group.map((x) => x._id);
|
||||
const depIds = group.map((x) => x.id);
|
||||
for (const dep of depIds) {
|
||||
await dependenceService.reInstall([dep]);
|
||||
}
|
||||
@@ -36,38 +34,21 @@ export default async () => {
|
||||
});
|
||||
|
||||
// 初始化时执行一次所有的ql repo 任务
|
||||
cronDb
|
||||
.find({
|
||||
command: /ql (repo|raw)/,
|
||||
isDisabled: { $ne: 1 },
|
||||
})
|
||||
.exec((err, docs) => {
|
||||
for (let i = 0; i < docs.length; i++) {
|
||||
const doc = docs[i];
|
||||
if (doc) {
|
||||
exec(doc.command);
|
||||
}
|
||||
CrontabModel.findAll({
|
||||
where: {
|
||||
isDisabled: { [Op.ne]: 1 },
|
||||
command: {
|
||||
[Op.or]: [{ [Op.like]: `%ql repo%` }, { [Op.like]: `%$ql raw%` }],
|
||||
},
|
||||
},
|
||||
}).then((docs) => {
|
||||
for (let i = 0; i < docs.length; i++) {
|
||||
const doc = docs[i];
|
||||
if (doc) {
|
||||
exec(doc.command);
|
||||
}
|
||||
});
|
||||
|
||||
// patch 禁用状态字段改变
|
||||
cronDb
|
||||
.find({
|
||||
status: CrontabStatus.disabled,
|
||||
})
|
||||
.exec((err, docs) => {
|
||||
if (docs.length > 0) {
|
||||
const ids = docs.map((x) => x._id);
|
||||
cronDb.update(
|
||||
{ _id: { $in: ids } },
|
||||
{ $set: { status: CrontabStatus.idle, isDisabled: 1 } },
|
||||
{ multi: true },
|
||||
(err) => {
|
||||
cronService.autosave_crontab();
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化保存一次ck和定时任务数据
|
||||
await cronService.autosave_crontab();
|
||||
|
||||
Reference in New Issue
Block a user