@chevre/domain 22.10.0-alpha.18 → 22.10.0-alpha.19

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.
@@ -14,19 +14,35 @@ interface IExecuteSettings {
14
14
  }
15
15
  type IOperationExecute<T> = (settings: IExecuteSettings) => Promise<T>;
16
16
  type INextFunction = (task: Pick<factory.task.ITask<factory.taskName>, 'id'>) => IOperationExecute<void>;
17
- interface IListenArgsOnTaskStatusChanged {
17
+ interface IReadyTask {
18
18
  id: string;
19
19
  name?: factory.taskName;
20
- status: factory.taskStatus.Ready | factory.taskStatus.Running;
20
+ status: factory.taskStatus.Ready;
21
21
  expires?: Date;
22
+ executionResult?: never;
22
23
  }
23
- type IOnTaskStatusChangedListener = (listenArgs: IListenArgsOnTaskStatusChanged, next: INextFunction) => void;
24
+ interface IRunningTask {
25
+ id: string;
26
+ status: factory.taskStatus.Running;
27
+ name?: never;
28
+ expires?: never;
29
+ executionResult?: never;
30
+ }
31
+ interface IExecutedTask {
32
+ id: string;
33
+ status: factory.taskStatus;
34
+ executionResult: factory.task.IExecutionResult;
35
+ name?: never;
36
+ expires?: never;
37
+ }
38
+ type IChangedTask = IReadyTask | IRunningTask | IExecutedTask;
39
+ type IOnTaskStatusChangedListener = (task: IChangedTask, next?: INextFunction) => void;
24
40
  /**
25
41
  * タスクイベントエミッター
26
42
  */
27
43
  declare class TaskEventEmitter extends EventEmitter {
28
- onTaskStatusChanged(listner: IOnTaskStatusChangedListener): void;
29
- emitTaskStatusChanged(args: IListenArgsOnTaskStatusChanged, next?: INextFunction): void;
44
+ onTaskStatusChanged(listener: IOnTaskStatusChangedListener): void;
45
+ emitTaskStatusChanged(task: IChangedTask, next?: INextFunction): void;
30
46
  }
31
47
  declare const taskEventEmitter: TaskEventEmitter;
32
- export { IExecuteSettings, INextFunction, IListenArgsOnTaskStatusChanged, IOnTaskStatusChangedListener, taskEventEmitter };
48
+ export { IChangedTask, IReadyTask, IRunningTask, IExecutedTask, IExecuteSettings, INextFunction, IOnTaskStatusChangedListener, taskEventEmitter };
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.taskEventEmitter = void 0;
13
4
  const events_1 = require("events");
@@ -15,21 +6,16 @@ var EventName;
15
6
  (function (EventName) {
16
7
  EventName["OnTaskStatusChanged"] = "OnTaskStatusChanged";
17
8
  })(EventName || (EventName = {}));
18
- const defaultNextFunction = () => {
19
- return () => __awaiter(void 0, void 0, void 0, function* () {
20
- // no op
21
- });
22
- };
23
9
  /**
24
10
  * タスクイベントエミッター
25
11
  */
26
12
  class TaskEventEmitter extends events_1.EventEmitter {
27
- onTaskStatusChanged(listner) {
28
- this.on(EventName.OnTaskStatusChanged, listner);
13
+ onTaskStatusChanged(listener) {
14
+ this.on(EventName.OnTaskStatusChanged, listener);
29
15
  }
30
16
  // support next function required(2025-05-25~)
31
- emitTaskStatusChanged(args, next) {
32
- this.emit(EventName.OnTaskStatusChanged, args, (typeof next === 'function') ? next : defaultNextFunction);
17
+ emitTaskStatusChanged(task, next) {
18
+ this.emit(EventName.OnTaskStatusChanged, task, (typeof next === 'function') ? next : undefined);
33
19
  }
34
20
  }
35
21
  const taskEventEmitter = new TaskEventEmitter();
@@ -24,7 +24,6 @@ interface IOptionOnCreate {
24
24
  }
25
25
  export type IExecutableTaskKeys = 'data' | 'id' | 'name' | 'status' | 'numberOfTried' | 'project' | 'remainingNumberOfTries' | 'runsAt' | 'expires';
26
26
  export type IExecutableTask<T extends factory.taskName> = Pick<factory.task.ITask<T>, IExecutableTaskKeys>;
27
- type IDelayedTask = Pick<factory.task.ITask<factory.taskName>, 'id' | 'name' | 'status'>;
28
27
  type IKeyOfProjection = keyof factory.task.ITask<factory.taskName>;
29
28
  type ICreatingTask = Pick<factory.task.IAttributes<factory.taskName>, 'data' | 'executionResults' | 'name' | 'numberOfTried' | 'project' | 'remainingNumberOfTries' | 'runsAt' | 'status' | 'identifier' | 'description'>;
30
29
  /**
@@ -135,24 +134,6 @@ export declare class TaskRepo {
135
134
  /**
136
135
  * emit OnTaskStatusChanged on delayed tasks
137
136
  */
138
- emitDelayedTasksEvent(params: {
139
- now: Date;
140
- /**
141
- * 指定期間遅延しているタスクを実行する
142
- */
143
- delayInSeconds: number;
144
- limit: number;
145
- name: {
146
- /**
147
- * 指定タスクのみ実行する
148
- */
149
- $in?: factory.taskName[];
150
- /**
151
- * 指定タスク以外のみ実行する
152
- */
153
- $nin?: factory.taskName[];
154
- };
155
- }): Promise<IDelayedTask[]>;
156
137
  /**
157
138
  * make tasks expired
158
139
  */
@@ -182,11 +163,13 @@ export declare class TaskRepo {
182
163
  /**
183
164
  * 実行結果を保管する
184
165
  */
185
- pushExecutionResultById(
186
- /**
187
- * タスクID
188
- */
189
- id: string, status: factory.taskStatus, executionResult: factory.task.IExecutionResult): Promise<void>;
166
+ pushExecutionResultById(params: {
167
+ /**
168
+ * タスクID
169
+ */
170
+ id: string;
171
+ status: factory.taskStatus;
172
+ }, executionResult: factory.task.IExecutionResult, next?: INextFunction): Promise<void>;
190
173
  /**
191
174
  * 特定タスク検索
192
175
  */
@@ -625,38 +625,62 @@ class TaskRepo {
625
625
  return doc;
626
626
  });
627
627
  }
628
+ // discontinue(2025-05-26~)
628
629
  /**
629
630
  * emit OnTaskStatusChanged on delayed tasks
630
631
  */
631
- emitDelayedTasksEvent(params) {
632
- return __awaiter(this, void 0, void 0, function* () {
633
- const runsAtLt = moment(params.now)
634
- .add(-params.delayInSeconds, 'seconds')
635
- .toDate();
636
- const projection = {
637
- _id: 0,
638
- id: { $toString: '$_id' },
639
- name: 1,
640
- status: 1
641
- };
642
- const delayedTasks = yield this.taskModel.find(Object.assign(Object.assign({ status: { $eq: factory.taskStatus.Ready }, runsAt: { $lt: runsAtLt } }, (Array.isArray(params.name.$in)) ? { name: { $in: params.name.$in } } : undefined), (Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined), projection)
643
- .limit(params.limit)
644
- .sort(sortOrder4executionOfTasks) // sort(2025-03-03~)
645
- .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
646
- .lean() // lean(2024-09-26~)
647
- .exec();
648
- if (delayedTasks.length > 0) {
649
- delayedTasks.forEach((delayedTask) => {
650
- task_1.taskEventEmitter.emitTaskStatusChanged({
651
- id: delayedTask.id,
652
- name: delayedTask.name,
653
- status: factory.taskStatus.Ready
654
- });
655
- });
656
- }
657
- return delayedTasks;
658
- });
659
- }
632
+ // public async emitDelayedTasksEvent(params: {
633
+ // now: Date;
634
+ // /**
635
+ // * 指定期間遅延しているタスクを実行する
636
+ // */
637
+ // delayInSeconds: number;
638
+ // limit: number;
639
+ // name: {
640
+ // /**
641
+ // * 指定タスクのみ実行する
642
+ // */
643
+ // $in?: factory.taskName[];
644
+ // /**
645
+ // * 指定タスク以外のみ実行する
646
+ // */
647
+ // $nin?: factory.taskName[];
648
+ // };
649
+ // }) {
650
+ // const runsAtLt = moment(params.now)
651
+ // .add(-params.delayInSeconds, 'seconds')
652
+ // .toDate();
653
+ // const projection: ProjectionType<factory.task.ITask<factory.taskName>> = {
654
+ // _id: 0,
655
+ // id: { $toString: '$_id' },
656
+ // name: 1,
657
+ // status: 1
658
+ // };
659
+ // const delayedTasks = await this.taskModel.find(
660
+ // {
661
+ // status: { $eq: factory.taskStatus.Ready },
662
+ // runsAt: { $lt: runsAtLt },
663
+ // ...(Array.isArray(params.name.$in)) ? { name: { $in: params.name.$in } } : undefined,
664
+ // ...(Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined
665
+ // },
666
+ // projection
667
+ // )
668
+ // .limit(params.limit)
669
+ // .sort(sortOrder4executionOfTasks) // sort(2025-03-03~)
670
+ // .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
671
+ // .lean<IDelayedTask[]>() // lean(2024-09-26~)
672
+ // .exec();
673
+ // if (delayedTasks.length > 0) {
674
+ // delayedTasks.forEach((delayedTask) => {
675
+ // taskEventEmitter.emitTaskStatusChanged({
676
+ // id: delayedTask.id,
677
+ // name: delayedTask.name,
678
+ // status: factory.taskStatus.Ready
679
+ // });
680
+ // });
681
+ // }
682
+ // return delayedTasks;
683
+ // }
660
684
  /**
661
685
  * make tasks expired
662
686
  */
@@ -772,17 +796,20 @@ class TaskRepo {
772
796
  /**
773
797
  * 実行結果を保管する
774
798
  */
775
- pushExecutionResultById(
776
- /**
777
- * タスクID
778
- */
779
- id, status, executionResult) {
799
+ pushExecutionResultById(params, executionResult,
800
+ // support customr function(2025-05-25~)
801
+ next) {
780
802
  return __awaiter(this, void 0, void 0, function* () {
803
+ const { id, status } = params;
781
804
  yield this.taskModel.updateOne({ _id: { $eq: id } }, {
782
805
  $set: { status },
783
806
  $push: { executionResults: executionResult }
784
807
  })
785
808
  .exec();
809
+ // emit event(2025-05-26~)
810
+ if (typeof next === 'function') {
811
+ task_1.taskEventEmitter.emitTaskStatusChanged({ id, status, executionResult }, next);
812
+ }
786
813
  });
787
814
  }
788
815
  /**
@@ -1,5 +1,5 @@
1
1
  import { SendGridCredentials } from '../credentials/sendGrid';
2
- import type { IExecuteSettings as IMinimumExecuteSettings } from '../eventEmitter/task';
2
+ import type { IExecuteSettings as IMinimumExecuteSettings, INextFunction, IReadyTask, IRunningTask } from '../eventEmitter/task';
3
3
  import * as factory from '../factory';
4
4
  import type { IExecutableTask, IExecutableTaskKeys, TaskRepo } from '../repo/task';
5
5
  import { Settings } from '../settings';
@@ -20,17 +20,17 @@ type IOperation<T> = (settings: IExecuteSettings) => Promise<T>;
20
20
  type IOperationExecute<T> = (settings: IExecuteSettings, options: IExecuteOptions) => Promise<T>;
21
21
  type ICallResult = void | Pick<factory.task.IExecutionResult, 'error'>;
22
22
  export type ICallableTaskOperation = (task: IExecutableTask<factory.taskName>) => IOperationExecute<ICallResult>;
23
- declare function executeById(params: {
24
- id: string;
25
- status: factory.taskStatus.Ready | factory.taskStatus.Running;
23
+ /**
24
+ * タスク変更イベントを受けて実行する
25
+ */
26
+ declare function executeById(params: (IReadyTask | IRunningTask) & {
26
27
  executor: {
27
28
  /**
28
29
  * タスク実行者名称
29
30
  */
30
31
  name: string;
31
32
  };
32
- expires?: Date;
33
- }): IOperation<void>;
33
+ }, next?: INextFunction): IOperation<void>;
34
34
  /**
35
35
  * support no name(2025-03-04~)
36
36
  */
@@ -52,7 +52,7 @@ declare function executeOneIfExists(params: {
52
52
  /**
53
53
  * タスクを実行する
54
54
  */
55
- declare function execute(task: IExecutableTask<factory.taskName>): IOperationExecute<void>;
55
+ declare function execute(task: IExecutableTask<factory.taskName>, next?: INextFunction): IOperationExecute<void>;
56
56
  /**
57
57
  * add(2025-03-13~)
58
58
  */
@@ -21,7 +21,10 @@ const moment = require("moment");
21
21
  const factory = require("../factory");
22
22
  const factory_1 = require("./notification/factory");
23
23
  const debug = createDebug('chevre-domain:service:task');
24
- function executeById(params) {
24
+ /**
25
+ * タスク変更イベントを受けて実行する
26
+ */
27
+ function executeById(params, next) {
25
28
  return (settings) => __awaiter(this, void 0, void 0, function* () {
26
29
  const taskRepo = new (yield Promise.resolve().then(() => require('../repo/task'))).TaskRepo(settings.connection);
27
30
  const { id, status, executor, expires } = params;
@@ -39,7 +42,7 @@ function executeById(params) {
39
42
  }
40
43
  // タスクがなければ終了
41
44
  if (task !== null) {
42
- yield execute(task)(settings, {
45
+ yield execute(task, (typeof next === 'function') ? next : undefined)(settings, {
43
46
  executeById: true,
44
47
  executeByName: false
45
48
  });
@@ -77,7 +80,7 @@ function executeOneIfExists(params) {
77
80
  /**
78
81
  * タスクを実行する
79
82
  */
80
- function execute(task) {
83
+ function execute(task, next) {
81
84
  const now = new Date();
82
85
  debug('executing an executableTask...', task, now);
83
86
  return (settings, options) => __awaiter(this, void 0, void 0, function* () {
@@ -129,7 +132,10 @@ function execute(task) {
129
132
  error: ((callResult === null || callResult === void 0 ? void 0 : callResult.error) instanceof Error)
130
133
  ? Object.assign(Object.assign({}, callResult.error), { message: callResult.error.message }) : ''
131
134
  };
132
- yield taskRepo.pushExecutionResultById(task.id, factory.taskStatus.Executed, result);
135
+ yield taskRepo.pushExecutionResultById({
136
+ id: task.id,
137
+ status: factory.taskStatus.Executed
138
+ }, result, (typeof next === 'function') ? next : undefined);
133
139
  }
134
140
  catch (error) {
135
141
  debug('service.task.execute throwed an error. task:', task.name, task.id, 'error:', error === null || error === void 0 ? void 0 : error.name, error === null || error === void 0 ? void 0 : error.message);
@@ -143,7 +149,10 @@ function execute(task) {
143
149
  error: Object.assign(Object.assign({}, error), { code: error.code, message: error.message, name: error.name, stack: error.stack })
144
150
  };
145
151
  // 失敗してもここではステータスを戻さない(Runningのまま待機)
146
- yield taskRepo.pushExecutionResultById(task.id, task.status, result);
152
+ yield taskRepo.pushExecutionResultById({
153
+ id: task.id,
154
+ status: task.status
155
+ }, result, (typeof next === 'function') ? next : undefined);
147
156
  }
148
157
  });
149
158
  }
package/package.json CHANGED
@@ -113,5 +113,5 @@
113
113
  "postversion": "git push origin --tags",
114
114
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
115
115
  },
116
- "version": "22.10.0-alpha.18"
116
+ "version": "22.10.0-alpha.19"
117
117
  }