mirror of
https://github.com/certd/certd.git
synced 2026-04-24 12:27:25 +08:00
perf: 优化定时器
This commit is contained in:
@@ -2,28 +2,28 @@ import * as api from "./api";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ref, shallowRef } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { dict } from "@fast-crud/fast-crud";
|
||||
import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, DialogOpenOption, dict, EditReq, UserPageQuery, UserPageRes } from "@fast-crud/fast-crud";
|
||||
import { statusUtil } from "/@/views/certd/pipeline/pipeline/utils/util.status";
|
||||
import { nanoid } from "nanoid";
|
||||
import { message } from "ant-design-vue";
|
||||
export default function ({ expose, certdFormRef }) {
|
||||
export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOptionsProps): CreateCrudOptionsRet {
|
||||
const router = useRouter();
|
||||
const { t } = useI18n();
|
||||
const lastResRef = ref();
|
||||
const pageRequest = async (query) => {
|
||||
const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
|
||||
return await api.GetList(query);
|
||||
};
|
||||
const editRequest = async ({ form, row }) => {
|
||||
const editRequest = async ({ form, row }: EditReq) => {
|
||||
form.id = row.id;
|
||||
const res = await api.UpdateObj(form);
|
||||
lastResRef.value = res;
|
||||
return res;
|
||||
};
|
||||
const delRequest = async ({ row }) => {
|
||||
const delRequest = async ({ row }: DelReq) => {
|
||||
return await api.DelObj(row.id);
|
||||
};
|
||||
|
||||
const addRequest = async ({ form }) => {
|
||||
const addRequest = async ({ form }: AddReq) => {
|
||||
form.content = JSON.stringify({
|
||||
title: form.title
|
||||
});
|
||||
@@ -32,7 +32,7 @@ export default function ({ expose, certdFormRef }) {
|
||||
return res;
|
||||
};
|
||||
function addCertdPipeline() {
|
||||
certdFormRef.value.open(async ({ form }) => {
|
||||
certdFormRef.value.open(async ({ form }: any) => {
|
||||
// 添加certd pipeline
|
||||
const pipeline = {
|
||||
title: form.domains[0] + "证书自动化",
|
||||
@@ -176,8 +176,8 @@ export default function ({ expose, certdFormRef }) {
|
||||
type: "dict-switch",
|
||||
dict: dict({
|
||||
data: [
|
||||
{ value: true, label: "禁用" },
|
||||
{ value: false, label: "启用" }
|
||||
{ value: false, label: "启用" },
|
||||
{ value: true, label: "禁用" }
|
||||
]
|
||||
}),
|
||||
form: {
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
</fs-page>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import { defineComponent, ref, onMounted } from "vue";
|
||||
import { useCrud } from "@fast-crud/fast-crud";
|
||||
import { useCrud, useFs } from "@fast-crud/fast-crud";
|
||||
import createCrudOptions from "./crud";
|
||||
import { useExpose } from "@fast-crud/fast-crud";
|
||||
import PiCertdForm from "./certd-form/index.vue";
|
||||
@@ -20,25 +20,14 @@ export default defineComponent({
|
||||
components: { PiCertdForm },
|
||||
setup() {
|
||||
const certdFormRef = ref();
|
||||
|
||||
// crud组件的ref
|
||||
const crudRef = ref();
|
||||
// crud 配置的ref
|
||||
const crudBinding = ref();
|
||||
|
||||
// 暴露的方法
|
||||
const { expose } = useExpose({ crudRef, crudBinding });
|
||||
// 你的crud配置
|
||||
const { crudOptions } = createCrudOptions({ expose, certdFormRef });
|
||||
// 初始化crud配置
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
|
||||
const { resetCrudOptions } = useCrud({ expose, crudOptions });
|
||||
// 你可以调用此方法,重新初始化crud配置
|
||||
// resetCrudOptions(options)
|
||||
const context: any = {
|
||||
certdFormRef
|
||||
};
|
||||
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions, context });
|
||||
|
||||
// 页面打开后获取列表数据
|
||||
onMounted(() => {
|
||||
expose.doRefresh();
|
||||
crudExpose.doRefresh();
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ export default defineComponent({
|
||||
name: "PiStatusShow",
|
||||
props: {
|
||||
status: {
|
||||
type: String,
|
||||
type: [String, Number],
|
||||
default: ""
|
||||
},
|
||||
type: {
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
const StatusEnum = {
|
||||
export type StatusEnumItem = {
|
||||
value: string;
|
||||
label: string;
|
||||
color: string;
|
||||
icon: string;
|
||||
spin?: boolean;
|
||||
};
|
||||
export type StatusEnumType = {
|
||||
[key: string]: StatusEnumItem;
|
||||
};
|
||||
|
||||
const StatusEnum: StatusEnumType = {
|
||||
success: {
|
||||
value: "success",
|
||||
label: "成功",
|
||||
|
||||
@@ -4,4 +4,4 @@ CREATE TABLE "pi_history_log" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
|
||||
CREATE TABLE "pi_pipeline" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL, "title" integer NOT NULL, "content" varchar(40960) NOT NULL, "keep_history_count" integer, "remark" varchar(100), "status" varchar(100), "disabled" boolean DEFAULT (0), "last_history_time" integer, "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP))
|
||||
|
||||
CREATE TABLE "pi_storage" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL, "scope" varchar NOT NULL, "namespace" varchar NOT NULL, "key" varchar(100), "value" varchar(40960), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP))
|
||||
CREATE TABLE "pi_storage" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL, "scope" varchar NOT NULL, "namespace" varchar NOT NULL, "version" varchar(100),"key" varchar(100), "value" varchar(40960), "create_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "update_time" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP))
|
||||
|
||||
@@ -52,6 +52,7 @@ export class PipelineController extends CrudController<PipelineService> {
|
||||
await this.service.checkUserId(bean.id, this.ctx.user.id);
|
||||
}
|
||||
await this.service.save(bean);
|
||||
await this.service.registerTriggerById(bean.id);
|
||||
return this.ok(bean.id);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ export class StorageEntity {
|
||||
@Column({ name: 'namespace', comment: '命名空间' })
|
||||
namespace: string;
|
||||
|
||||
@Column({ comment: 'version', length: 100, nullable: true })
|
||||
version: string;
|
||||
|
||||
@Column({ comment: 'key', length: 100, nullable: true })
|
||||
key: string;
|
||||
|
||||
|
||||
@@ -12,15 +12,26 @@ export class DbStorage implements IStorage {
|
||||
this.storageService = storageService;
|
||||
}
|
||||
|
||||
remove(
|
||||
scope: string,
|
||||
namespace: string,
|
||||
version: string,
|
||||
key: string
|
||||
): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
async get(
|
||||
scope: string,
|
||||
namespace: string,
|
||||
version: string,
|
||||
key: string
|
||||
): Promise<string | null> {
|
||||
const storageEntity = await this.storageService.get({
|
||||
userId: this.userId,
|
||||
scope: scope,
|
||||
namespace: namespace,
|
||||
version,
|
||||
key,
|
||||
});
|
||||
|
||||
@@ -33,6 +44,7 @@ export class DbStorage implements IStorage {
|
||||
async set(
|
||||
scope: string,
|
||||
namespace: string,
|
||||
version: string,
|
||||
key: string,
|
||||
value: string
|
||||
): Promise<void> {
|
||||
@@ -40,6 +52,7 @@ export class DbStorage implements IStorage {
|
||||
userId: this.userId,
|
||||
scope: scope,
|
||||
namespace: namespace,
|
||||
version,
|
||||
key,
|
||||
value,
|
||||
});
|
||||
|
||||
@@ -42,11 +42,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
|
||||
async update(entity) {
|
||||
await super.update(entity);
|
||||
|
||||
await this.registerTriggerById(entity.id);
|
||||
}
|
||||
|
||||
private async registerTriggerById(pipelineId) {
|
||||
public async registerTriggerById(pipelineId) {
|
||||
if (pipelineId == null) {
|
||||
return;
|
||||
}
|
||||
@@ -70,13 +68,13 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
const pipeline = JSON.parse(bean.content);
|
||||
bean.title = pipeline.title;
|
||||
await this.addOrUpdate(bean);
|
||||
await this.registerTriggerById(bean.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用启动后初始加载记录
|
||||
*/
|
||||
async onStartup() {
|
||||
logger.info('加载定时trigger开始');
|
||||
const idEntityList = await this.repository.find({
|
||||
select: {
|
||||
id: true,
|
||||
@@ -111,7 +109,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
this.registerTriggers(pipeline);
|
||||
}
|
||||
}
|
||||
logger.info('定时器数量:', this.cron.getList());
|
||||
logger.info('定时器数量:', this.cron.getListSize());
|
||||
}
|
||||
|
||||
registerTriggers(pipeline?: Pipeline) {
|
||||
@@ -121,6 +119,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
for (const trigger of pipeline.triggers) {
|
||||
this.registerCron(pipeline.id, trigger);
|
||||
}
|
||||
logger.info('当前定时器数量:', this.cron.getListSize());
|
||||
}
|
||||
|
||||
async trigger(id) {
|
||||
@@ -128,10 +127,11 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
name: `pipeline.${id}.trigger.once`,
|
||||
cron: null,
|
||||
job: async () => {
|
||||
logger.info('job准备启动,当前定时器数量:', this.cron.getListSize());
|
||||
await this.run(id, null);
|
||||
},
|
||||
});
|
||||
logger.info('定时器数量:', this.cron.getList());
|
||||
logger.info('定时器数量:', this.cron.getListSize());
|
||||
}
|
||||
|
||||
registerCron(pipelineId, trigger) {
|
||||
@@ -141,10 +141,11 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
}
|
||||
if (cron.startsWith('*')) {
|
||||
cron = '0' + cron.substring(1, cron.length);
|
||||
return;
|
||||
}
|
||||
const name = this.buildCronKey(pipelineId, trigger.id);
|
||||
this.cron.remove(name);
|
||||
this.cron.register({
|
||||
name: this.buildCronKey(pipelineId, trigger.id),
|
||||
name,
|
||||
cron: cron,
|
||||
job: async () => {
|
||||
logger.info('定时任务触发:', pipelineId, trigger.id);
|
||||
@@ -191,8 +192,13 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
accessService: this.accessService,
|
||||
storage: new DbStorage(userId, this.storageService),
|
||||
});
|
||||
|
||||
await executor.run(historyId, triggerType);
|
||||
try {
|
||||
await executor.init();
|
||||
await executor.run(historyId, triggerType);
|
||||
} catch (e) {
|
||||
logger.error('执行失败:', e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private getTriggerType(triggerId, pipeline) {
|
||||
@@ -230,7 +236,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
//修改pipeline状态
|
||||
const pipelineEntity = new PipelineEntity();
|
||||
pipelineEntity.id = parseInt(history.pipeline.id);
|
||||
pipelineEntity.status = history.pipeline.status.status;
|
||||
pipelineEntity.status = history.pipeline.status.status + '';
|
||||
pipelineEntity.lastHistoryTime = history.pipeline.status.startTime;
|
||||
await this.update(pipelineEntity);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Provide, Scope, ScopeEnum } from "@midwayjs/decorator";
|
||||
import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BaseService } from '../../../basic/base-service';
|
||||
@@ -20,6 +20,7 @@ export class StorageService extends BaseService<StorageEntity> {
|
||||
scope: any;
|
||||
namespace: any;
|
||||
userId: number;
|
||||
version: string;
|
||||
key: string;
|
||||
}) {
|
||||
if (where.userId == null) {
|
||||
@@ -35,6 +36,7 @@ export class StorageService extends BaseService<StorageEntity> {
|
||||
scope: any;
|
||||
namespace: any;
|
||||
userId: number;
|
||||
version: string;
|
||||
value: string;
|
||||
key: string;
|
||||
}) {
|
||||
|
||||
@@ -23,15 +23,20 @@ export class 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>;
|
||||
tasks.delete(taskName);
|
||||
const node = tasks.get(taskName);
|
||||
if (node) {
|
||||
node.stop();
|
||||
tasks.delete(taskName);
|
||||
}
|
||||
}
|
||||
|
||||
getList() {
|
||||
getListSize() {
|
||||
const tasks = cron.getTasks();
|
||||
return tasks.size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user