From 49632697a0eb54a6ce93b8478cdf8d5f597cbedd Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 7 Apr 2025 18:42:24 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=20job=20?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/api/infra/job-log/index.ts | 36 +++ apps/web-antd/src/api/infra/job/index.ts | 68 ++++++ apps/web-antd/src/views/infra/job/data.ts | 222 ++++++++++++++++++ apps/web-antd/src/views/infra/job/index.vue | 208 ++++++++++++++++ .../src/views/infra/job/logger/data.ts | 143 +++++++++++ .../src/views/infra/job/logger/index.vue | 100 ++++++++ .../views/infra/job/logger/modules/detail.vue | 68 ++++++ .../src/views/infra/job/modules/detail.vue | 77 ++++++ .../src/views/infra/job/modules/form.vue | 80 +++++++ packages/@core/base/icons/src/lucide.ts | 1 + packages/@core/base/shared/src/utils/date.ts | 4 +- 11 files changed, 1005 insertions(+), 2 deletions(-) create mode 100644 apps/web-antd/src/api/infra/job-log/index.ts create mode 100644 apps/web-antd/src/api/infra/job/index.ts create mode 100644 apps/web-antd/src/views/infra/job/data.ts create mode 100644 apps/web-antd/src/views/infra/job/index.vue create mode 100644 apps/web-antd/src/views/infra/job/logger/data.ts create mode 100644 apps/web-antd/src/views/infra/job/logger/index.vue create mode 100644 apps/web-antd/src/views/infra/job/logger/modules/detail.vue create mode 100644 apps/web-antd/src/views/infra/job/modules/detail.vue create mode 100644 apps/web-antd/src/views/infra/job/modules/form.vue diff --git a/apps/web-antd/src/api/infra/job-log/index.ts b/apps/web-antd/src/api/infra/job-log/index.ts new file mode 100644 index 000000000..f2c756d61 --- /dev/null +++ b/apps/web-antd/src/api/infra/job-log/index.ts @@ -0,0 +1,36 @@ +import type { PageParam, PageResult } from '@vben/request'; + +import { requestClient } from '#/api/request'; + +export namespace InfraJobLogApi { + /** 任务日志信息 */ + export interface InfraJobLog { + id?: number; + jobId: number; + handlerName: string; + handlerParam: string; + cronExpression: string; + executeIndex: string; + beginTime: Date; + endTime: Date; + duration: string; + status: number; + createTime?: string; + result: string; + } +} + +/** 查询任务日志列表 */ +export function getJobLogPage(params: PageParam) { + return requestClient.get>('/infra/job-log/page', { params }); +} + +/** 查询任务日志详情 */ +export function getJobLog(id: number) { + return requestClient.get(`/infra/job-log/get?id=${id}`); +} + +/** 导出定时任务日志 */ +export function exportJobLog(params: any) { + return requestClient.download('/infra/job-log/export-excel', { params }); +} diff --git a/apps/web-antd/src/api/infra/job/index.ts b/apps/web-antd/src/api/infra/job/index.ts new file mode 100644 index 000000000..702446921 --- /dev/null +++ b/apps/web-antd/src/api/infra/job/index.ts @@ -0,0 +1,68 @@ +import type { PageParam, PageResult } from '@vben/request'; + +import { requestClient } from '#/api/request'; + +export namespace InfraJobApi { + /** 任务信息 */ + export interface InfraJob { + id?: number; + name: string; + status: number; + handlerName: string; + handlerParam: string; + cronExpression: string; + retryCount: number; + retryInterval: number; + monitorTimeout: number; + createTime?: Date; + } +} + +/** 查询任务列表 */ +export function getJobPage(params: PageParam) { + return requestClient.get>('/infra/job/page', { params }); +} + +/** 查询任务详情 */ +export function getJob(id: number) { + return requestClient.get(`/infra/job/get?id=${id}`); +} + +/** 新增任务 */ +export function createJob(data: InfraJobApi.InfraJob) { + return requestClient.post('/infra/job/create', data); +} + +/** 修改定时任务调度 */ +export function updateJob(data: InfraJobApi.InfraJob) { + return requestClient.put('/infra/job/update', data); +} + +/** 删除定时任务调度 */ +export function deleteJob(id: number) { + return requestClient.delete(`/infra/job/delete?id=${id}`); +} + +/** 导出定时任务调度 */ +export function exportJob(params: any) { + return requestClient.download('/infra/job/export-excel', { params }); +} + +/** 任务状态修改 */ +export function updateJobStatus(id: number, status: number) { + const params = { + id, + status + }; + return requestClient.put('/infra/job/update-status', { params }); +} + +/** 定时任务立即执行一次 */ +export function runJob(id: number) { + return requestClient.put(`/infra/job/trigger?id=${id}`); +} + +/** 获得定时任务的下 n 次执行时间 */ +export function getJobNextTimes(id: number) { + return requestClient.get(`/infra/job/get_next_times?id=${id}`); +} diff --git a/apps/web-antd/src/views/infra/job/data.ts b/apps/web-antd/src/views/infra/job/data.ts new file mode 100644 index 000000000..af1bde63a --- /dev/null +++ b/apps/web-antd/src/views/infra/job/data.ts @@ -0,0 +1,222 @@ +import type { VbenFormSchema } from '#/adapter/form'; +import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table'; +import type { InfraJobApi } from '#/api/infra/job'; + +import { DICT_TYPE, getDictOptions } from '#/utils/dict'; +import { useAccess } from '@vben/access'; +import { InfraJobStatusEnum } from '#/utils/constants'; + +const { hasAccessByCodes } = useAccess(); + +/** 新增/修改的表单 */ +export function useFormSchema(): VbenFormSchema[] { + return [ + { + fieldName: 'id', + component: 'Input', + dependencies: { + triggerFields: [''], + show: () => false, + }, + }, + { + fieldName: 'name', + label: '任务名称', + component: 'Input', + componentProps: { + placeholder: '请输入任务名称', + }, + rules: 'required', + }, + { + fieldName: 'handlerName', + label: '处理器的名字', + component: 'Input', + componentProps: { + placeholder: '请输入处理器的名字', + // readonly: ({ values }) => !!values.id, + }, + rules: 'required', + // TODO @芋艿:在修改场景下,禁止调整 + }, + { + fieldName: 'handlerParam', + label: '处理器的参数', + component: 'Input', + componentProps: { + placeholder: '请输入处理器的参数', + }, + }, + { + fieldName: 'cronExpression', + label: 'CRON 表达式', + component: 'Input', + componentProps: { + placeholder: '请输入 CRON 表达式', + }, + rules: 'required', + // TODO @芋艿:未来支持动态的 CRON 表达式选择 + }, + { + fieldName: 'retryCount', + label: '重试次数', + component: 'InputNumber', + componentProps: { + placeholder: '请输入重试次数。设置为 0 时,不进行重试', + min: 0, + class: 'w-full', + }, + rules: 'required', + }, + { + fieldName: 'retryInterval', + label: '重试间隔', + component: 'InputNumber', + componentProps: { + placeholder: '请输入重试间隔,单位:毫秒。设置为 0 时,无需间隔', + min: 0, + class: 'w-full' + }, + rules: 'required', + }, + { + fieldName: 'monitorTimeout', + label: '监控超时时间', + component: 'InputNumber', + componentProps: { + placeholder: '请输入监控超时时间,单位:毫秒', + min: 0, + class: 'w-full', + }, + }, + ]; +} + +/** 列表的搜索表单 */ +export function useGridFormSchema(): VbenFormSchema[] { + return [ + { + fieldName: 'name', + label: '任务名称', + component: 'Input', + componentProps: { + allowClear: true, + placeholder: '请输入任务名称', + }, + }, + { + fieldName: 'status', + label: '任务状态', + component: 'Select', + componentProps: { + options: getDictOptions(DICT_TYPE.INFRA_JOB_STATUS, 'number'), + allowClear: true, + placeholder: '请选择任务状态', + }, + }, + { + fieldName: 'handlerName', + label: '处理器的名字', + component: 'Input', + componentProps: { + allowClear: true, + placeholder: '请输入处理器的名字', + }, + }, + ]; +} + +/** 表格列配置 */ +export function useGridColumns( + onActionClick: OnActionClickFn, +): VxeTableGridOptions['columns'] { + return [ + { + field: 'id', + title: '任务编号', + minWidth: 80, + }, + { + field: 'name', + title: '任务名称', + minWidth: 120, + }, + { + field: 'status', + title: '任务状态', + minWidth: 100, + cellRender: { + name: 'CellDict', + props: { type: DICT_TYPE.INFRA_JOB_STATUS, }, + }, + }, + { + field: 'handlerName', + title: '处理器的名字', + minWidth: 180, + }, + { + field: 'handlerParam', + title: '处理器的参数', + minWidth: 140, + }, + { + field: 'cronExpression', + title: 'CRON 表达式', + minWidth: 120, + }, + { + field: 'operation', + title: '操作', + width: 280, + fixed: 'right', + align: 'center', + cellRender: { + attrs: { + nameField: 'name', + nameTitle: '任务', + onClick: onActionClick, + }, + name: 'CellOperation', + options: [ + { + code: 'edit', + show: hasAccessByCodes(['infra:job:update']), + }, + { + code: 'update-status', + text: '开启', + show: (row: any) => hasAccessByCodes(['infra:job:update']) + && row.status === InfraJobStatusEnum.STOP, + }, + { + code: 'update-status', + text: '暂停', + show: (row: any) => hasAccessByCodes(['infra:job:update']) + && row.status == InfraJobStatusEnum.NORMAL, + }, + { + code: 'trigger', + text: '执行', + show: hasAccessByCodes(['infra:job:trigger']), + }, + // TODO @芋艿:增加一个“更多”选项 + { + code: 'view', + text: '详细', + show: hasAccessByCodes(['infra:job:query']), + }, + { + code: 'log', + text: '日志', + show: hasAccessByCodes(['infra:job:query']), + }, + { + code: 'delete', + show: hasAccessByCodes(['infra:job:delete']), + }, + ], + }, + }, + ]; +} diff --git a/apps/web-antd/src/views/infra/job/index.vue b/apps/web-antd/src/views/infra/job/index.vue new file mode 100644 index 000000000..4299daec8 --- /dev/null +++ b/apps/web-antd/src/views/infra/job/index.vue @@ -0,0 +1,208 @@ + + + diff --git a/apps/web-antd/src/views/infra/job/logger/data.ts b/apps/web-antd/src/views/infra/job/logger/data.ts new file mode 100644 index 000000000..d8c7f92ac --- /dev/null +++ b/apps/web-antd/src/views/infra/job/logger/data.ts @@ -0,0 +1,143 @@ +import type { VbenFormSchema } from '#/adapter/form'; +import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table'; +import type { InfraJobLogApi } from '#/api/infra/job-log'; + +import { DICT_TYPE, getDictOptions } from '#/utils/dict'; +import { useAccess } from '@vben/access'; +import dayjs from 'dayjs'; +import { formatDateTime } from '@vben/utils'; + +const { hasAccessByCodes } = useAccess(); + +/** 列表的搜索表单 */ +export function useGridFormSchema(): VbenFormSchema[] { + return [ + { + fieldName: 'handlerName', + label: '处理器的名字', + component: 'Input', + componentProps: { + allowClear: true, + placeholder: '请输入处理器的名字', + }, + }, + { + fieldName: 'beginTime', + label: '开始执行时间', + component: 'DatePicker', + componentProps: { + allowClear: true, + placeholder: '选择开始执行时间', + valueFormat: 'YYYY-MM-DD HH:mm:ss', + showTime: { + format: 'HH:mm:ss', + defaultValue: dayjs('00:00:00', 'HH:mm:ss'), + }, + }, + }, + { + fieldName: 'endTime', + label: '结束执行时间', + component: 'DatePicker', + componentProps: { + allowClear: true, + placeholder: '选择结束执行时间', + valueFormat: 'YYYY-MM-DD HH:mm:ss', + showTime: { + format: 'HH:mm:ss', + defaultValue: dayjs('23:59:59', 'HH:mm:ss'), + }, + }, + }, + { + fieldName: 'status', + label: '任务状态', + component: 'Select', + componentProps: { + options: getDictOptions(DICT_TYPE.INFRA_JOB_LOG_STATUS, 'number'), + allowClear: true, + placeholder: '请选择任务状态', + }, + }, + ]; +} + +/** 表格列配置 */ +export function useGridColumns( + onActionClick: OnActionClickFn, +): VxeTableGridOptions['columns'] { + return [ + { + field: 'id', + title: '日志编号', + minWidth: 80, + }, + { + field: 'jobId', + title: '任务编号', + minWidth: 80, + }, + { + field: 'handlerName', + title: '处理器的名字', + minWidth: 180, + }, + { + field: 'handlerParam', + title: '处理器的参数', + minWidth: 140, + }, + { + field: 'executeIndex', + title: '第几次执行', + minWidth: 100, + }, + { + field: 'beginTime', + title: '执行时间', + minWidth: 280, + formatter: ({ row }) => { + return `${formatDateTime(row.beginTime)} ~ ${formatDateTime(row.endTime)}`; + }, + }, + { + field: 'duration', + title: '执行时长', + minWidth: 120, + formatter: ({ row }) => { + return `${row.duration} 毫秒`; + }, + }, + { + field: 'status', + title: '任务状态', + minWidth: 100, + cellRender: { + name: 'CellDict', + props: { type: DICT_TYPE.INFRA_JOB_LOG_STATUS }, + }, + }, + { + field: 'operation', + title: '操作', + width: 80, + fixed: 'right', + align: 'center', + cellRender: { + attrs: { + nameField: 'id', + nameTitle: '日志', + onClick: onActionClick, + }, + name: 'CellOperation', + options: [ + { + code: 'detail', + text: '详细', + show: hasAccessByCodes(['infra:job:query']), + }, + ], + }, + }, + ]; +} diff --git a/apps/web-antd/src/views/infra/job/logger/index.vue b/apps/web-antd/src/views/infra/job/logger/index.vue new file mode 100644 index 000000000..4b1698bf9 --- /dev/null +++ b/apps/web-antd/src/views/infra/job/logger/index.vue @@ -0,0 +1,100 @@ + + + diff --git a/apps/web-antd/src/views/infra/job/logger/modules/detail.vue b/apps/web-antd/src/views/infra/job/logger/modules/detail.vue new file mode 100644 index 000000000..7caa255be --- /dev/null +++ b/apps/web-antd/src/views/infra/job/logger/modules/detail.vue @@ -0,0 +1,68 @@ + + + diff --git a/apps/web-antd/src/views/infra/job/modules/detail.vue b/apps/web-antd/src/views/infra/job/modules/detail.vue new file mode 100644 index 000000000..3823664e2 --- /dev/null +++ b/apps/web-antd/src/views/infra/job/modules/detail.vue @@ -0,0 +1,77 @@ + + + diff --git a/apps/web-antd/src/views/infra/job/modules/form.vue b/apps/web-antd/src/views/infra/job/modules/form.vue new file mode 100644 index 000000000..c9f72e1ec --- /dev/null +++ b/apps/web-antd/src/views/infra/job/modules/form.vue @@ -0,0 +1,80 @@ + + + diff --git a/packages/@core/base/icons/src/lucide.ts b/packages/@core/base/icons/src/lucide.ts index 81225ba4b..a2e6da46a 100644 --- a/packages/@core/base/icons/src/lucide.ts +++ b/packages/@core/base/icons/src/lucide.ts @@ -67,4 +67,5 @@ export { X, Download, Upload, + History, } from 'lucide-vue-next'; diff --git a/packages/@core/base/shared/src/utils/date.ts b/packages/@core/base/shared/src/utils/date.ts index 3736b9ad5..17e5848f3 100644 --- a/packages/@core/base/shared/src/utils/date.ts +++ b/packages/@core/base/shared/src/utils/date.ts @@ -1,6 +1,6 @@ import dayjs from 'dayjs'; -export function formatDate(time: number | string, format = 'YYYY-MM-DD') { +export function formatDate(time: number | string | Date, format = 'YYYY-MM-DD') { try { const date = dayjs(time); if (!date.isValid()) { @@ -13,7 +13,7 @@ export function formatDate(time: number | string, format = 'YYYY-MM-DD') { } } -export function formatDateTime(time: number | string) { +export function formatDateTime(time: number | string | Date) { return formatDate(time, 'YYYY-MM-DD HH:mm:ss'); }