@kne/fastify-task 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -143,7 +143,10 @@ module.exports = fp(async (fastify, options) => {
143
143
  completedAt: null,
144
144
  context: {}
145
145
  });
146
- executor({ type: task.type, scriptName: task.scriptName, task })
146
+ const addLog = (props) => {
147
+ return log(Object.assign({}, props, { taskId: task.id }));
148
+ };
149
+ executor({ type: task.type, scriptName: task.scriptName, task, log: addLog })
147
150
  .then(async result => {
148
151
  if (result === false) {
149
152
  return;
@@ -385,6 +388,27 @@ module.exports = fp(async (fastify, options) => {
385
388
  }
386
389
  };
387
390
 
391
+ const log = async ({ taskId, data, message = '' }) => {
392
+ const task = await detail({ id: taskId });
393
+ if (!task) {
394
+ throw new Error('任务不存在');
395
+ }
396
+
397
+ const currentOptions = task.options || {};
398
+ const logs = currentOptions.logs || [];
399
+ logs.push({
400
+ data, message, time: new Date()
401
+ });
402
+
403
+ await task.update({
404
+ options: {
405
+ ...currentOptions, logs
406
+ }
407
+ });
408
+
409
+ return task;
410
+ };
411
+
388
412
  Object.assign(fastify[options.name].services, {
389
413
  create,
390
414
  detail,
@@ -394,9 +418,10 @@ module.exports = fp(async (fastify, options) => {
394
418
  runner,
395
419
  resetAll,
396
420
  retry,
421
+ log,
397
422
  executor,
398
423
  processNext,
399
424
  processSystemTask,
400
425
  waitingComplete
401
426
  });
402
- });
427
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {