From 5189dbe8dab81973303cac0c770b7bc9c2d3d0fb Mon Sep 17 00:00:00 2001 From: "peng.peng" Date: Wed, 22 Mar 2023 17:06:08 +0800 Subject: [PATCH] =?UTF-8?q?(*)api=E6=A0=B9=E6=8D=AE=E9=87=87=E9=9B=86?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=A1=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E9=87=87?= =?UTF-8?q?=E9=9B=86=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=20=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E4=BB=BB=E5=8A=A1=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metadataAcquisition/initJob.js | 28 ++++++++++++ .../controllers/metadataAcquisition/task.js | 45 +++++++++++++++++-- .../lib/routes/metadataAcquisition/task.js | 2 +- api/app/lib/schedule/acqTask.js | 41 +++++++++++++++++ api/app/lib/schedule/index.js | 4 +- .../components/steps/postgre/stepThree.js | 40 +++-------------- .../containers/acquisitionTask.js | 3 +- web/package.json | 1 + 8 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 api/app/lib/controllers/metadataAcquisition/initJob.js create mode 100644 api/app/lib/schedule/acqTask.js diff --git a/api/app/lib/controllers/metadataAcquisition/initJob.js b/api/app/lib/controllers/metadataAcquisition/initJob.js new file mode 100644 index 0000000..b52f764 --- /dev/null +++ b/api/app/lib/controllers/metadataAcquisition/initJob.js @@ -0,0 +1,28 @@ +const moment = require('moment') +// let TEST = false +TEST = true + +module.exports = async function (app, task) { + + let job = null + try { + job = app.fs.scheduleInit( + { + interval: task.cron, + immediate: TEST, + proRun: !TEST, + }, + async () => { + try { + console.log(task.taskName, moment().format('YYYY-MM-DD HH:mm:ss')); + } catch (error) { + app.fs.logger.error(`sechedule: taskJobs, error: ${error}`) + } + }); + + } catch (error) { + app.fs.logger.error(`sechedule: taskJobs, error: ${error}`); + } + + return job; +} \ No newline at end of file diff --git a/api/app/lib/controllers/metadataAcquisition/task.js b/api/app/lib/controllers/metadataAcquisition/task.js index 6242aed..739d450 100644 --- a/api/app/lib/controllers/metadataAcquisition/task.js +++ b/api/app/lib/controllers/metadataAcquisition/task.js @@ -1,5 +1,6 @@ 'use strict'; - +const schedule = require('node-schedule') +const moment = require('moment'); // 新增采集任务 function addAcquisitionTask(opts) { return async function (ctx, next) { @@ -74,9 +75,34 @@ function getAcquisitionTask(opts) { } } +const TEST = true; +async function createJob(app, task) { + let job = null + try { + job = app.fs.scheduleInit( + { + interval: task.cron, + immediate: TEST, + proRun: !TEST, + }, + async () => { + try { + console.log(task.taskName, moment().format('YYYY-MM-DD HH:mm:ss')); + } catch (error) { + app.fs.logger.error(`sechedule: taskJobs, error: ${error}`) + } + }); + + } catch (error) { + app.fs.logger.error(`sechedule: taskJobs, error: ${error}`); + } + + return { job } +} + // 修改采集任务 -function editAcquisitionTask(opts) { +function editAcquisitionTask(opts, app) { return async function (ctx, next) { try { @@ -93,6 +119,19 @@ function editAcquisitionTask(opts) { body, { where: { id: id, } } ) + const task = await models.AcquisitionTask.findOne({ where: { id: id } }); + + //编辑采集任务 更新采集定时任务状态 + if ((task.enabled && (body.cron || body.enabled))) { + const job = await require(`./initJob.js`)(app, task) + if (app.fs.schedule['taskJobs'][id]) schedule.cancelJob(app.fs.schedule['taskJobs'][id]) + app.fs.schedule['taskJobs'][id] = job + } else if (!task.enabled) { + if (app.fs.schedule['taskJobs'][id]) { + schedule.cancelJob(app.fs.schedule['taskJobs'][id]) + } + } + ctx.status = 204; ctx.body = { message: '修改采集任务成功' } } @@ -131,4 +170,4 @@ module.exports = { getAcquisitionTask, editAcquisitionTask, deleteAcquisitionTask -} \ No newline at end of file +} diff --git a/api/app/lib/routes/metadataAcquisition/task.js b/api/app/lib/routes/metadataAcquisition/task.js index cb14543..b1217ed 100644 --- a/api/app/lib/routes/metadataAcquisition/task.js +++ b/api/app/lib/routes/metadataAcquisition/task.js @@ -13,7 +13,7 @@ module.exports = function (app, router, opts, AuthCode) { // 修改采集任务信息 app.fs.api.logAttr['PUT/acq/task/:id'] = { content: '修改采集任务信息', visible: true }; - router.put('/acq/task/:id', task.editAcquisitionTask(opts)) + router.put('/acq/task/:id', task.editAcquisitionTask(opts, app)) // 删除采集任务信息 app.fs.api.logAttr['DEL/acq/task/:id'] = { content: '删除采集任务信息', visible: true }; diff --git a/api/app/lib/schedule/acqTask.js b/api/app/lib/schedule/acqTask.js new file mode 100644 index 0000000..9248e2e --- /dev/null +++ b/api/app/lib/schedule/acqTask.js @@ -0,0 +1,41 @@ +const moment = require('moment') +const rimraf = require('rimraf'); +const fs = require("fs"); +const path = require("path") + +let TEST = false +// TEST = true + +module.exports = async function (app, opts) { + + const jobs = {} + try { + const { models } = app.fs.dc + const tasks = await models.AcquisitionTask.findAll({ + where: { + enabled: true + } + }) + tasks.map(s => { + const job = app.fs.scheduleInit( + { + interval: s.cron, + immediate: TEST, + proRun: !TEST, + }, + async () => { + try { + console.log(s.taskName, moment().format('YYYY-MM-DD HH:mm:ss')); + } catch (error) { + app.fs.logger.error(`sechedule: taskJobs, error: ${error}`) + } + }); + jobs[s.id] = job; + }) + + } catch (error) { + app.fs.logger.error(`sechedule: acqTask, error: ${error}`); + } + + return { taskJobs: jobs } +} \ No newline at end of file diff --git a/api/app/lib/schedule/index.js b/api/app/lib/schedule/index.js index 121b8a9..9fdeb7f 100644 --- a/api/app/lib/schedule/index.js +++ b/api/app/lib/schedule/index.js @@ -21,9 +21,9 @@ module.exports = async function (app, opts) { app.fs.scheduleInit = scheduleInit - fs.readdirSync(__dirname).forEach((filename) => { + fs.readdirSync(__dirname).forEach(async (filename) => { if (!['index.js'].some(f => filename == f)) { - const scheduleList = require(`./${filename}`)(app, opts) + const scheduleList = await require(`./${filename}`)(app, opts) for (let k of Object.keys(scheduleList)) { console.info(`定时任务 ${k} 启动`); } diff --git a/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js b/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js index 735195c..bf46fe9 100644 --- a/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js +++ b/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js @@ -10,29 +10,16 @@ import { ProFormDependency } from '@ant-design/pro-form'; import { BellOutlined } from '@ant-design/icons' -import { parseHumanReadable } from 'cron-js-parser'; +var parser = require('cron-parser'); +import moment from 'moment'; import '../../style.less'; function StepThree(props) { const { prev, next, dataSourceFilter, editData } = props; const formRef = React.createRef(); const initialValues = editData ? editData : {} - const testCron = () => { + const testCron = (value) => { try { - let lang = 'zh_CN' //english - let obj = { - atSeconds: [1, 5, 10], - runEveryXMins: { - startAt: 10, - every: 10 - }, - runEveryHourInRange: { - from: 2, - to: 20 - }, - isEveryDay: true, - atYears: [2020, 2022] - }; - console.log(parseHumanReadable("0 15 10 ? * *", obj, lang)) + var interval = parser.parseExpression(value); } catch (err) { console.log('Error: ' + err.message); } @@ -40,21 +27,8 @@ function StepThree(props) { const checkCron = async (rule, value) => { try { - let lang = 'zh_CN' //english - let obj = { - atSeconds: [1, 5, 10], - runEveryXMins: { - startAt: 10, - every: 10 - }, - runEveryHourInRange: { - from: 2, - to: 20 - }, - isEveryDay: true, - atYears: [2020, 2022] - }; - console.log(parseHumanReadable(value, obj, lang)) + var interval = parser.parseExpression(value); + console.log('Date: ', moment(interval.next().toString()).format('YYYY-MM-DD HH:mm:ss')); return Promise.resolve(); } catch (err) { return Promise.reject(new Error('请输入正确的cron表达式!')); @@ -119,7 +93,7 @@ function StepThree(props) { ]} name="cron" label="请输入cron表达式" - addonAfter={} + // addonAfter={} /> { // return {`共${Math.ceil(total / pageSize)}页,${total}项`} // }, diff --git a/web/package.json b/web/package.json index 1f3db63..a03e6b3 100644 --- a/web/package.json +++ b/web/package.json @@ -74,6 +74,7 @@ "antd": "^4.24.5", "args": "^5.0.1", "cron-js-parser": "^1.0.17", + "cron-parser": "^4.8.1", "cross-env": "^7.0.3", "crypto-js": "^4.1.1", "echarts": "^5.4.1",