chore: 目录调整,controller转移到外部单独的目录

This commit is contained in:
xiaojunnuo
2024-10-13 21:59:29 +08:00
parent ccfe72a0d9
commit 417971d15d
37 changed files with 79 additions and 81 deletions
@@ -1,28 +0,0 @@
import { Config, Configuration, Logger } from '@midwayjs/core';
import { ILogger } from '@midwayjs/logger';
import { IMidwayContainer } from '@midwayjs/core';
import { Cron } from './cron.js';
// ... (see below) ...
@Configuration({
namespace: 'cron',
//importConfigs: [join(__dirname, './config')],
})
export class CronConfiguration {
@Config()
config;
@Logger()
logger: ILogger;
cron: Cron;
async onReady(container: IMidwayContainer) {
this.logger.info('cron start');
this.cron = new Cron({
logger: this.logger,
...this.config,
});
container.registerObject('cron', this.cron);
this.cron.start();
this.logger.info('cron started');
}
}
@@ -1,107 +0,0 @@
import parser from 'cron-parser';
import { ILogger } from '@certd/pipeline';
export type CronTaskReq = {
/**
* 为空则为单次执行
*/
cron: string;
job: () => Promise<void>;
name: string;
};
export class CronTask {
logger: ILogger;
cron: string;
job: () => Promise<void>;
name: string;
stoped = false;
nextTime: any;
constructor(req: CronTaskReq, logger: ILogger) {
this.cron = req.cron;
this.job = req.job;
this.name = req.name;
this.logger = logger;
this.logger.info(`[cron] CronTask created [${this.name}], cron:${this.cron}`);
this.genNextTime();
}
genNextTime() {
if (!this.cron) {
return null;
}
if (this.stoped) {
return null;
}
const interval = parser.parseExpression(this.cron);
const next = interval.next().getTime();
this.logger.info(`[cron] [${this.name}], cron:${this.cron}, next run :${new Date(next).toLocaleString()}`);
this.nextTime = next;
return next;
}
stop() {
this.stoped = true;
}
}
export class Cron {
logger: ILogger;
immediateTriggerOnce: boolean;
queue: CronTask[] = [];
constructor(opts: any) {
this.logger = opts.logger;
this.immediateTriggerOnce = opts.immediateTriggerOnce;
}
start() {
this.logger.info('[cron] start');
this.queue.forEach(task => {
task.genNextTime();
});
setInterval(() => {
const now = new Date().getTime();
for (const task of this.queue) {
if (task.nextTime <= now) {
task.job().catch(e => {
this.logger.error(`job execute error : [${task.name}]`, e);
});
task.genNextTime();
}
}
}, 1000 * 60);
}
register(req: CronTaskReq) {
if (!req.cron) {
this.logger.info(`[cron] register once : [${req.name}]`);
req.job().catch(e => {
this.logger.error(`job execute error : [${req.name}]`, e);
});
return;
}
this.logger.info(`[cron] register cron : [${req.name}] ,${req.cron}`);
const task = new CronTask(req, this.logger);
this.queue.push(task);
this.logger.info('当前定时任务数量:', this.getTaskSize());
}
remove(taskName: string) {
this.logger.info(`[cron] remove : [${taskName}]`);
const index = this.queue.findIndex(item => item.name === taskName);
if (index !== -1) {
this.queue[index].stop();
this.queue.splice(index, 1);
}
this.logger.info('当前定时任务数量:', this.getTaskSize());
}
getTaskSize() {
const tasks = Object.keys(this.queue);
return tasks.length;
}
}
@@ -1,5 +0,0 @@
// src/index.ts
export { CronConfiguration as Configuration } from './configuration.js';
// export * from './controller/user';
// export * from './controller/api';
// export * from './service/user';
@@ -0,0 +1,50 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('pi_plugin')
export class PluginEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'name', comment: 'Key' })
name: string;
@Column({ name: 'icon', comment: '图标' })
icon: string;
@Column({ name: 'title', comment: '标题' })
title: string;
@Column({ name: 'group', comment: '分组' })
group: string;
@Column({ name: 'desc', comment: '描述' })
desc: string;
@Column({ comment: '配置', length: 40960 })
setting: string;
@Column({ comment: '系统配置', length: 40960 })
sysSetting: string;
@Column({ comment: '脚本', length: 40960 })
content: string;
@Column({ comment: '类型', length: 100, nullable: true })
type: string; // builtIn | custom
@Column({ comment: '启用/禁用', default: false })
disabled: boolean;
@Column({
name: 'create_time',
comment: '创建时间',
default: () => 'CURRENT_TIMESTAMP',
})
createTime: Date;
@Column({
name: 'update_time',
comment: '修改时间',
default: () => 'CURRENT_TIMESTAMP',
})
updateTime: Date;
}
@@ -0,0 +1,27 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService } from '@certd/lib-server';
import { PluginEntity } from '../entity/plugin.js';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { checkComm } from '@certd/pipeline';
import { BuiltInPluginService } from '../../pipeline/service/plugin-service.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class PluginService extends BaseService<PluginEntity> {
@InjectEntityModel(PluginEntity)
repository: Repository<PluginEntity>;
@Inject()
builtInPluginService: BuiltInPluginService;
//@ts-ignore
getRepository() {
checkComm();
return this.repository;
}
async setDisabled(id: number, disabled: boolean) {
await this.repository.update({ id }, { disabled });
}
}