diff --git a/api/app/lib/controllers/metadataAcquisition/task.js b/api/app/lib/controllers/metadataAcquisition/task.js index 8be0a19..f82aabb 100644 --- a/api/app/lib/controllers/metadataAcquisition/task.js +++ b/api/app/lib/controllers/metadataAcquisition/task.js @@ -2,6 +2,8 @@ const schedule = require('node-schedule') const moment = require('moment'); const initJob = require('./initJob'); +const { handleTask } = require('./taskHandle'); + // 新增采集任务 function addAcquisitionTask(app) { return async function (ctx, next) { @@ -153,9 +155,31 @@ function deleteAcquisitionTask(app) { } } +// 采集任务立即执行一次 +function addAcquisitionTask(app) { + return async function (ctx, next) { + + const models = ctx.fs.dc.models; + try { + const { id } = ctx.request.body + const task = await models.AcquisitionTask.findOne({ where: { id, id } }); + if (task) { + await handleTask(app, task); + ctx.status = 204; + ctx.body = { message: '执行成功' } + } + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: '执行失败' } + } + } +} + module.exports = { addAcquisitionTask, getAcquisitionTask, editAcquisitionTask, - deleteAcquisitionTask + deleteAcquisitionTask, + runAcqTask } diff --git a/api/app/lib/controllers/metadataAcquisition/taskHandle.js b/api/app/lib/controllers/metadataAcquisition/taskHandle.js index 1a3bb64..97d45f6 100644 --- a/api/app/lib/controllers/metadataAcquisition/taskHandle.js +++ b/api/app/lib/controllers/metadataAcquisition/taskHandle.js @@ -5,11 +5,12 @@ const schedule = require('node-schedule') const initJob = require('./initJob'); //删除数据库源时 同步删除该数据源资源目录树下元数据 //数据源 数据库配置信息不能更改 若需更改则需要删除数据源重新配置 -let i = 0; +const taskRetryIndex = {} async function handleTask(app, task) { // const transaction = await app.fs.dc.orm.transaction(); const startTime = moment() const { models } = app.fs.dc + if (!taskRetryIndex[task.id]) taskRetryIndex[task.id] = 0; try { const dataSource = await models.DataSource.findOne({ where: { @@ -154,15 +155,16 @@ async function handleTask(app, task) { details: '采集成功', startTime: startTime, endTime: endTime - } //日志记录 await models.AcquisitionLog.create(logBody) + taskRetryIndex[task.id] = 0; } } } - i++ - console.log('===========------------------------------------------------------------' + task.taskName + moment().format('HH:mm:ss')) + + console.log('===========------------------------------------------------------------' + taskRetryIndex[task.id] + '----' + task.taskName + '------' + moment().format('HH:mm:ss')) + } catch (error) { // await transaction.rollback(); const endTime = moment() @@ -174,8 +176,13 @@ async function handleTask(app, task) { endTime: endTime } await models.AcquisitionLog.create(logBody) - // if (app.fs.schedule['taskJobs'][task.id]) schedule.cancelJob(app.fs.schedule['taskJobs'][task.id]) - console.log('===========------------------------------------------------------------' + i) + taskRetryIndex[task.id]++; + //处理采集失败重试 + if (task.retried && task.retryCount && task.retryTime && taskRetryIndex[task.id] < task.retryCount) { + setTimeout(() => { + handleTask(app, task) + }, 1000 * 60 * task.retryCount); + } app.fs.logger.error(`sechedule: handleTask, error: ${error}`); } } diff --git a/api/app/lib/routes/metadataAcquisition/task.js b/api/app/lib/routes/metadataAcquisition/task.js index ace4f65..ce1e0f5 100644 --- a/api/app/lib/routes/metadataAcquisition/task.js +++ b/api/app/lib/routes/metadataAcquisition/task.js @@ -19,4 +19,8 @@ module.exports = function (app, router, opts, AuthCode) { app.fs.api.logAttr['DEL/acq/task/:id'] = { content: '删除采集任务信息', visible: true }; router.del('acq/task/:id', task.deleteAcquisitionTask(app)) + //任务立即执行 + app.fs.api.logAttr['POST/acq/task/run'] = { content: '任务立即执行', visible: true }; + router.put('/acq/task/run', task.runAcqTask(app)) + }; 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 bf46fe9..c1648f6 100644 --- a/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js +++ b/web/client/src/sections/metadataAcquisition/components/steps/postgre/stepThree.js @@ -128,6 +128,7 @@ function StepThree(props) { ]} name="retryTime" label="时间间隔" + fieldProps={{ precision: 0 }} addonAfter={分钟} />