pref: 调整插件目录,增加一些帮助说明

This commit is contained in:
xiaojunnuo
2024-05-27 18:38:41 +08:00
parent dd730f6beb
commit 20bc5aa6c7
164 changed files with 1160 additions and 3573 deletions
@@ -0,0 +1,27 @@
import { Config, Configuration, Logger } from '@midwayjs/decorator';
import { ILogger } from '@midwayjs/logger';
import { IMidwayContainer } from '@midwayjs/core';
import { Cron } from './cron';
// ... (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.logger.info('cron started');
}
}
@@ -0,0 +1,43 @@
import cron from 'node-cron';
export type CronTask = {
/**
* 为空则为单次执行
*/
cron: string;
job: () => Promise<void>;
name: string;
};
export class Cron {
logger;
constructor(opts) {
this.logger = opts.logger;
}
register(task: CronTask) {
if (!task.cron) {
this.logger.info(`[cron] register once : [${task.name}]`);
task.job();
return;
}
this.logger.info(`[cron] register cron : [${task.name}] ,${task.cron}`);
cron.schedule(task.cron, task.job, {
name: task.name,
});
this.logger.info('当前定时任务数量:', this.getListSize());
}
remove(taskName: string) {
this.logger.info(`[cron] remove : [${taskName}]`);
const tasks = cron.getTasks() as Map<any, any>;
const node = tasks.get(taskName);
if (node) {
node.stop();
tasks.delete(taskName);
}
}
getListSize() {
const tasks = cron.getTasks();
return tasks.size;
}
}
@@ -0,0 +1,5 @@
// src/index.ts
export { CronConfiguration as Configuration } from './configuration';
// export * from './controller/user';
// export * from './controller/api';
// export * from './service/user';