@chevre/domain 26.0.0-alpha.12 → 26.0.0-alpha.14

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.
Files changed (34) hide show
  1. package/lib/chevre/repo/adminTask.d.ts +15 -3
  2. package/lib/chevre/repo/adminTask.js +26 -1
  3. package/lib/chevre/repo/mongoose/schemas/setting.d.ts +1 -2
  4. package/lib/chevre/repo/mongoose/schemas/task.d.ts +1 -4
  5. package/lib/chevre/repo/mongoose/schemas/task.js +5 -1
  6. package/lib/chevre/repo/task.d.ts +4 -74
  7. package/lib/chevre/repo/task.js +11 -189
  8. package/lib/chevre/repo/taskDelayed.d.ts +67 -0
  9. package/lib/chevre/repo/taskDelayed.js +148 -0
  10. package/lib/chevre/repository.d.ts +5 -10
  11. package/lib/chevre/repository.js +11 -22
  12. package/lib/chevre/service/asyncAction.js +1 -1
  13. package/lib/chevre/service/scheduledTask.js +1 -1
  14. package/lib/chevre/service/task/acceptCOAOffer.js +1 -14
  15. package/lib/chevre/service/task/authorizePayment.js +1 -7
  16. package/lib/chevre/service/task/checkMovieTicket.js +1 -7
  17. package/lib/chevre/service/task/publishPaymentUrl.js +1 -10
  18. package/lib/chevre/service/task/syncResourcesFromCOA.js +1 -5
  19. package/lib/chevre/service/task/voidReserveTransaction.js +5 -8
  20. package/lib/chevre/service/task.d.ts +4 -4
  21. package/lib/chevre/service/task.js +7 -7
  22. package/lib/chevre/service/taskHandler/onOperationFailed.d.ts +1 -2
  23. package/lib/chevre/service/taskHandler/onOperationFailed.js +8 -7
  24. package/lib/chevre/service/taskHandler.d.ts +1 -2
  25. package/lib/chevre/service/taskHandler.js +6 -5
  26. package/lib/chevre/service/transaction/placeOrder/exportTasks/factory.d.ts +1 -1
  27. package/lib/chevre/service/transaction/placeOrder/exportTasksById.js +11 -17
  28. package/lib/chevre/service/transaction/returnOrder.js +11 -17
  29. package/lib/chevre/taskSettings.d.ts +4 -2
  30. package/package.json +1 -1
  31. package/lib/chevre/repo/asyncActionLegacy.d.ts +0 -23
  32. package/lib/chevre/repo/asyncActionLegacy.js +0 -59
  33. package/lib/chevre/repo/scheduledTaskLegacy.d.ts +0 -39
  34. package/lib/chevre/repo/scheduledTaskLegacy.js +0 -96
@@ -1,7 +1,7 @@
1
1
  import type { AnyKeys, Connection, FilterQuery, ProjectionType } from 'mongoose';
2
2
  import { factory } from '../factory';
3
3
  import { IDocType } from './mongoose/schemas/task';
4
- type IKeyOfProjection = keyof factory.task.ITask<factory.taskName> | 'expires';
4
+ type IKeyOfProjection = keyof factory.task.ITask<factory.taskName>;
5
5
  /**
6
6
  * タスク管理リポジトリ
7
7
  */
@@ -16,14 +16,26 @@ export declare class AdminTaskRepo {
16
16
  * 検索する
17
17
  */
18
18
  findTasks(params: factory.task.ISearchConditions, inclusion: IKeyOfProjection[]): Promise<factory.task.ITask<factory.taskName>[]>;
19
+ /**
20
+ * cron:notifyLatency専用
21
+ */
22
+ countDelayedTasks(params: {
23
+ delayInSeconds: number;
24
+ name: {
25
+ $nin?: factory.taskName[];
26
+ };
27
+ limit?: number;
28
+ }): Promise<{
29
+ count: number;
30
+ }>;
19
31
  getCursor(conditions: FilterQuery<factory.task.ITask<factory.taskName>>, projection: ProjectionType<factory.task.ITask<factory.taskName>>): import("mongoose").Cursor<import("mongoose").Document<unknown, Record<string, never>, IDocType, import("./mongoose/virtuals").IVirtuals, {}> & Omit<import("@chevre/factory/lib/chevre/task").ITask | import("@chevre/factory/lib/chevre/task/confirmPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/confirmReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/createAccountingReport").ITask | import("@chevre/factory/lib/chevre/task/onAssetTransactionStatusChanged").ITask | import("@chevre/factory/lib/chevre/task/onAuthorizationCreated").ITask | import("@chevre/factory/lib/chevre/task/onEventChanged").ITask | import("@chevre/factory/lib/chevre/task/onResourceDeleted").ITask | import("@chevre/factory/lib/chevre/task/onResourceUpdated").ITask | import("@chevre/factory/lib/chevre/task/onOrderPaymentCompleted").ITask | import("@chevre/factory/lib/chevre/task/placeOrder").ITask | import("@chevre/factory/lib/chevre/task/returnOrder").ITask | import("@chevre/factory/lib/chevre/task/returnPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/returnReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/sendEmailMessage").ITask | import("@chevre/factory/lib/chevre/task/sendOrder").ITask | import("@chevre/factory/lib/chevre/task/triggerWebhook").ITask | import("@chevre/factory/lib/chevre/task/useReservation").ITask | import("@chevre/factory/lib/chevre/task/voidPayTransaction").ITask, "id"> & {
20
- expires?: Date;
32
+ expires?: never;
21
33
  } & {
22
34
  _id: import("mongoose").Types.ObjectId;
23
35
  } & {
24
36
  __v: number;
25
37
  }, import("mongoose").QueryOptions<IDocType>, (import("mongoose").Document<unknown, Record<string, never>, IDocType, import("./mongoose/virtuals").IVirtuals, {}> & Omit<import("@chevre/factory/lib/chevre/task").ITask | import("@chevre/factory/lib/chevre/task/confirmPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/confirmReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/createAccountingReport").ITask | import("@chevre/factory/lib/chevre/task/onAssetTransactionStatusChanged").ITask | import("@chevre/factory/lib/chevre/task/onAuthorizationCreated").ITask | import("@chevre/factory/lib/chevre/task/onEventChanged").ITask | import("@chevre/factory/lib/chevre/task/onResourceDeleted").ITask | import("@chevre/factory/lib/chevre/task/onResourceUpdated").ITask | import("@chevre/factory/lib/chevre/task/onOrderPaymentCompleted").ITask | import("@chevre/factory/lib/chevre/task/placeOrder").ITask | import("@chevre/factory/lib/chevre/task/returnOrder").ITask | import("@chevre/factory/lib/chevre/task/returnPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/returnReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/sendEmailMessage").ITask | import("@chevre/factory/lib/chevre/task/sendOrder").ITask | import("@chevre/factory/lib/chevre/task/triggerWebhook").ITask | import("@chevre/factory/lib/chevre/task/useReservation").ITask | import("@chevre/factory/lib/chevre/task/voidPayTransaction").ITask, "id"> & {
26
- expires?: Date;
38
+ expires?: never;
27
39
  } & {
28
40
  _id: import("mongoose").Types.ObjectId;
29
41
  } & {
@@ -1,9 +1,14 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.AdminTaskRepo = void 0;
7
+ const moment_1 = __importDefault(require("moment"));
4
8
  const factory_1 = require("../factory");
5
9
  const settings_1 = require("../settings");
6
10
  const task_1 = require("./mongoose/schemas/task");
11
+ // | 'expires'; // 非同期アクションは移行につきexpires廃止(2026-08-05~)
7
12
  const AVAILABLE_PROJECT_FIELDS = [
8
13
  'alternateName',
9
14
  'identifier',
@@ -19,7 +24,7 @@ const AVAILABLE_PROJECT_FIELDS = [
19
24
  'executor',
20
25
  'data',
21
26
  'dateAborted',
22
- 'expires'
27
+ // 'expires' // 非同期アクションは移行につきexpires廃止(2026-08-05~)
23
28
  ];
24
29
  /**
25
30
  * タスク管理リポジトリ
@@ -150,6 +155,26 @@ class AdminTaskRepo {
150
155
  .lean() // lean(2024-09-26~)
151
156
  .exec();
152
157
  }
158
+ /**
159
+ * cron:notifyLatency専用
160
+ */
161
+ async countDelayedTasks(params) {
162
+ const { limit } = params;
163
+ const runsAtLt = (0, moment_1.default)()
164
+ .add(-params.delayInSeconds, 'seconds')
165
+ .toDate();
166
+ const query = this.taskModel.countDocuments({
167
+ status: { $eq: factory_1.factory.taskStatus.Ready },
168
+ runsAt: { $lt: runsAtLt },
169
+ ...(Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined
170
+ });
171
+ if (typeof limit === 'number' && limit >= 0) {
172
+ query.limit(limit);
173
+ }
174
+ const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
175
+ .exec();
176
+ return { count };
177
+ }
153
178
  getCursor(conditions, projection) {
154
179
  return this.taskModel.find(conditions, projection)
155
180
  .sort({ runsAt: factory_1.factory.sortType.Ascending })
@@ -32,8 +32,7 @@ interface IOnEventChanged {
32
32
  informEvent2agg?: factory.project.IInformParams[];
33
33
  }
34
34
  export interface IStorageSettings {
35
- useScheduledDeleteTransactionTask?: boolean;
36
- deleteTransactionTaskExpiresInDays?: number;
35
+ deleteTransactionTaskExpiresInDays: number;
37
36
  /**
38
37
  * 取引保管期間(Confirmed)
39
38
  * default:365
@@ -2,10 +2,7 @@ import { IndexDefinition, IndexOptions, Model, Schema, SchemaDefinition } from '
2
2
  import { IVirtuals } from '../virtuals';
3
3
  import { factory } from '../../../factory';
4
4
  type IDocType = Omit<factory.task.ITask<factory.taskName>, 'id'> & {
5
- /**
6
- * 非同期アクション移行前のタスクには残っている(2026-07-29時点)
7
- */
8
- expires?: Date;
5
+ expires?: never;
9
6
  };
10
7
  type IModel = Model<IDocType, Record<string, never>, Record<string, never>, IVirtuals>;
11
8
  type ISchemaDefinition = SchemaDefinition<IDocType>;
@@ -22,7 +22,7 @@ const schemaDefinition = {
22
22
  executor: mongoose_1.SchemaTypes.Mixed,
23
23
  data: mongoose_1.SchemaTypes.Mixed,
24
24
  dateAborted: Date,
25
- expires: Date // add(2024-04-23~)
25
+ // expires: Date // add(2024-04-23~) // 非同期アクションは移行につきexpires廃止(2026-08-05~)
26
26
  };
27
27
  const schemaOptions = {
28
28
  autoIndex: settings_1.MONGO_AUTO_INDEX,
@@ -178,6 +178,10 @@ const indexes = [
178
178
  { status: 1, runsAt: 1, name: 1 },
179
179
  { name: 'executeOneByNameIfExists' } // add(2025-03-08~)
180
180
  ],
181
+ [
182
+ { status: 1, name: 1, runsAt: 1 },
183
+ { name: 'runDelayedTaskByName' } // add(2026-08-05~)
184
+ ],
181
185
  [
182
186
  { status: 1, remainingNumberOfTries: 1, lastTriedAt: 1 },
183
187
  {
@@ -1,5 +1,4 @@
1
1
  import type { Connection, UpdateWriteOpResult } from 'mongoose';
2
- import { INextFunction } from '../eventEmitter/task';
3
2
  import { factory } from '../factory';
4
3
  import { IDocType } from './mongoose/schemas/task';
5
4
  import type { IExecutableTask } from '../taskSettings';
@@ -7,7 +6,7 @@ import type { IExecutableTask } from '../taskSettings';
7
6
  * リトライ可能なタスク作成属性
8
7
  */
9
8
  type ICreatingTask = Pick<factory.task.IAttributes<factory.taskName>, 'data' | 'executionResults' | 'name' | 'numberOfTried' | 'project' | 'remainingNumberOfTries' | 'runsAt' | 'status' | 'identifier' | 'description'> & {
10
- expires?: Date;
9
+ expires?: never;
11
10
  };
12
11
  /**
13
12
  * タスクリポジトリ
@@ -42,7 +41,7 @@ export declare class TaskRepo {
42
41
  id: string;
43
42
  };
44
43
  }): Promise<(import("mongoose").Document<unknown, Record<string, never>, IDocType, import("./mongoose/virtuals").IVirtuals, {}> & Omit<import("@chevre/factory/lib/chevre/task").ITask | import("@chevre/factory/lib/chevre/task/confirmPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/confirmReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/createAccountingReport").ITask | import("@chevre/factory/lib/chevre/task/onAssetTransactionStatusChanged").ITask | import("@chevre/factory/lib/chevre/task/onAuthorizationCreated").ITask | import("@chevre/factory/lib/chevre/task/onEventChanged").ITask | import("@chevre/factory/lib/chevre/task/onResourceDeleted").ITask | import("@chevre/factory/lib/chevre/task/onResourceUpdated").ITask | import("@chevre/factory/lib/chevre/task/onOrderPaymentCompleted").ITask | import("@chevre/factory/lib/chevre/task/placeOrder").ITask | import("@chevre/factory/lib/chevre/task/returnOrder").ITask | import("@chevre/factory/lib/chevre/task/returnPayTransaction").ITask | import("@chevre/factory/lib/chevre/task/returnReserveTransaction").ITask | import("@chevre/factory/lib/chevre/task/sendEmailMessage").ITask | import("@chevre/factory/lib/chevre/task/sendOrder").ITask | import("@chevre/factory/lib/chevre/task/triggerWebhook").ITask | import("@chevre/factory/lib/chevre/task/useReservation").ITask | import("@chevre/factory/lib/chevre/task/voidPayTransaction").ITask, "id"> & {
45
- expires?: Date;
44
+ expires?: never;
46
45
  } & {
47
46
  _id: import("mongoose").Types.ObjectId;
48
47
  } & {
@@ -69,66 +68,8 @@ export declare class TaskRepo {
69
68
  executor: {
70
69
  name: string;
71
70
  };
72
- expires?: Date;
71
+ expires?: never;
73
72
  }): Promise<IExecutableTask<factory.taskName> | null>;
74
- /**
75
- * 潜在的に実行されるべきタスクをカウントする
76
- * add(2025-03-16~)
77
- */
78
- countPotentiallyRunning(params: {
79
- name?: {
80
- $eq?: factory.taskName;
81
- $in?: factory.taskName[];
82
- };
83
- runsAt: {
84
- $lt: Date;
85
- };
86
- limit: number;
87
- }): Promise<{
88
- count: number;
89
- }>;
90
- /**
91
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更する
92
- */
93
- private runDelayedTask;
94
- /**
95
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
96
- */
97
- emitRunningIfExistsByName(params: {
98
- /**
99
- * 必ずタスク名指定で実行する
100
- */
101
- name: {
102
- $eq: factory.taskName;
103
- $in?: never;
104
- } | {
105
- $eq?: never;
106
- $in: factory.taskName[];
107
- };
108
- runsAt: {
109
- $lt: Date;
110
- };
111
- sort: {
112
- numberOfTried?: factory.sortType;
113
- runsAt?: factory.sortType;
114
- };
115
- executor: {
116
- name: string;
117
- };
118
- }): Promise<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'> | null>;
119
- /**
120
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
121
- * taskNameで絞らない
122
- * 2026-08-04~
123
- */
124
- emitRunningIfExistsNoName(params: {
125
- runsAt: {
126
- $lt: Date;
127
- };
128
- executor: {
129
- name: string;
130
- };
131
- }): Promise<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'> | null>;
132
73
  /**
133
74
  * Runningのまま一定期間超過し、かつ、remainingNumberOfTries>0のタスクをReadyに変更する
134
75
  */
@@ -167,12 +108,10 @@ export declare class TaskRepo {
167
108
  * タスクID
168
109
  */
169
110
  id: string;
170
- remainingNumberOfTries: number;
171
- name: factory.taskName;
172
111
  }, update: {
173
112
  status: factory.taskStatus.Executed | factory.taskStatus.Running | factory.taskStatus.Aborted;
174
113
  executionResult: factory.task.IExecutionResult;
175
- }, next?: INextFunction): Promise<void>;
114
+ }): Promise<void>;
176
115
  deleteByProject(params: {
177
116
  project: {
178
117
  id: string;
@@ -186,14 +125,5 @@ export declare class TaskRepo {
186
125
  $lt: Date;
187
126
  };
188
127
  }): Promise<import("mongodb").DeleteResult>;
189
- countDelayedTasks(params: {
190
- delayInSeconds: number;
191
- name: {
192
- $nin?: factory.taskName[];
193
- };
194
- limit?: number;
195
- }): Promise<{
196
- count: number;
197
- }>;
198
128
  }
199
129
  export {};
@@ -21,7 +21,7 @@ const executableTaskProjection = {
21
21
  project: 1,
22
22
  remainingNumberOfTries: 1,
23
23
  runsAt: 1,
24
- expires: 1
24
+ // expires: 1 // 非同期アクションは移行につきexpires廃止(2026-08-05~)
25
25
  };
26
26
  /**
27
27
  * タスクリポジトリ
@@ -229,7 +229,7 @@ class TaskRepo {
229
229
  _id: { $eq: params.id },
230
230
  status: { $eq: params.status },
231
231
  runsAt: { $lt: now },
232
- ...(params.expires instanceof Date) ? { expires: { $gt: now } } : undefined
232
+ // ...(params.expires instanceof Date) ? { expires: { $gt: now } } : undefined // 非同期アクションは移行につきexpires廃止(2026-08-05~)
233
233
  }, executableTaskProjection)
234
234
  .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
235
235
  .lean()
@@ -240,7 +240,7 @@ class TaskRepo {
240
240
  _id: { $eq: params.id },
241
241
  status: { $eq: params.status },
242
242
  runsAt: { $lt: now },
243
- ...(params.expires instanceof Date) ? { expires: { $gt: now } } : undefined
243
+ // ...(params.expires instanceof Date) ? { expires: { $gt: now } } : undefined // 非同期アクションは移行につきexpires廃止(2026-08-05~)
244
244
  }, {
245
245
  $set: {
246
246
  status: factory_1.factory.taskStatus.Running, // 実行中に変更
@@ -324,166 +324,6 @@ class TaskRepo {
324
324
  // }
325
325
  // return doc;
326
326
  // }
327
- /**
328
- * 潜在的に実行されるべきタスクをカウントする
329
- * add(2025-03-16~)
330
- */
331
- async countPotentiallyRunning(params) {
332
- const { runsAt, limit, name } = params;
333
- const nameEq = name?.$eq;
334
- const nameIn = name?.$in;
335
- if (!(runsAt.$lt instanceof Date)) {
336
- throw new factory_1.factory.errors.Argument('runsAt.$lt', 'must be Date');
337
- }
338
- const query = this.taskModel.countDocuments({
339
- status: { $eq: factory_1.factory.taskStatus.Ready },
340
- runsAt: { $lt: params.runsAt.$lt },
341
- ...(typeof nameEq === 'string' || Array.isArray(nameIn))
342
- ? {
343
- name: {
344
- ...(typeof nameEq === 'string') ? { $eq: nameEq } : undefined,
345
- ...(Array.isArray(nameIn)) ? { $in: nameIn } : undefined
346
- }
347
- }
348
- : undefined
349
- });
350
- if (typeof limit === 'number' && limit >= 0) {
351
- query.limit(limit);
352
- }
353
- const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
354
- .exec();
355
- return { count };
356
- }
357
- /**
358
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更する
359
- */
360
- async runDelayedTask(params) {
361
- const { filter } = params;
362
- const projection = {
363
- _id: 0,
364
- id: { $toString: '$_id' },
365
- name: 1
366
- };
367
- return this.taskModel.findOneAndUpdate(filter, {
368
- $set: {
369
- status: factory_1.factory.taskStatus.Running, // 実行中に変更
370
- lastTriedAt: new Date(),
371
- executor: params.executor
372
- },
373
- $inc: {
374
- remainingNumberOfTries: -1, // 残りトライ可能回数減らす
375
- numberOfTried: 1 // トライ回数増やす
376
- }
377
- }, {
378
- new: true,
379
- projection,
380
- ...(typeof params.sort.numberOfTried === 'number' || typeof params.sort.runsAt === 'number')
381
- ? { sort: params.sort }
382
- : undefined
383
- })
384
- .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
385
- .lean()
386
- .exec();
387
- }
388
- /**
389
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
390
- */
391
- async emitRunningIfExistsByName(params) {
392
- if (!(params.runsAt.$lt instanceof Date)) {
393
- throw new factory_1.factory.errors.Argument('runsAt.$lt', 'must be Date');
394
- }
395
- // const projection: ProjectionType<IDocType> = {
396
- // _id: 0,
397
- // id: { $toString: '$_id' },
398
- // name: 1
399
- // };
400
- // インデックス'executeOneByNameIfExists'を想定したfilter
401
- const filter = {
402
- status: { $eq: factory_1.factory.taskStatus.Ready },
403
- runsAt: { $lt: params.runsAt.$lt },
404
- name: params.name
405
- };
406
- const doc = await this.runDelayedTask({
407
- filter,
408
- sort: params.sort,
409
- executor: params.executor
410
- });
411
- // const doc = await this.taskModel.findOneAndUpdate(
412
- // filter,
413
- // {
414
- // $set: {
415
- // status: factory.taskStatus.Running, // 実行中に変更
416
- // lastTriedAt: new Date(),
417
- // executor: params.executor
418
- // },
419
- // $inc: {
420
- // remainingNumberOfTries: -1, // 残りトライ可能回数減らす
421
- // numberOfTried: 1 // トライ回数増やす
422
- // }
423
- // },
424
- // {
425
- // new: true,
426
- // projection,
427
- // ...(typeof params.sort.numberOfTried === 'number' || typeof params.sort.runsAt === 'number')
428
- // ? { sort: params.sort }
429
- // : undefined
430
- // }
431
- // )
432
- // .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
433
- // .lean<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'>>()
434
- // .exec();
435
- if (doc === null) {
436
- return null;
437
- }
438
- const nameEq = params.name?.$eq;
439
- let changedTask;
440
- if (typeof nameEq === 'string') {
441
- changedTask = {
442
- id: doc.id,
443
- status: factory_1.factory.taskStatus.Running,
444
- name: nameEq
445
- };
446
- }
447
- else {
448
- changedTask = {
449
- id: doc.id,
450
- status: factory_1.factory.taskStatus.Running
451
- };
452
- }
453
- task_1.taskEventEmitter.emitTaskStatusChanged(changedTask);
454
- return doc;
455
- }
456
- /**
457
- * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
458
- * taskNameで絞らない
459
- * 2026-08-04~
460
- */
461
- async emitRunningIfExistsNoName(params) {
462
- if (!(params.runsAt.$lt instanceof Date)) {
463
- throw new factory_1.factory.errors.Argument('runsAt.$lt', 'must be Date');
464
- }
465
- // インデックス'executeOneByNameIfExists'を想定したfilter
466
- const filter = {
467
- status: { $eq: factory_1.factory.taskStatus.Ready },
468
- runsAt: { $lt: params.runsAt.$lt }
469
- };
470
- const doc = await this.runDelayedTask({
471
- filter,
472
- sort: {
473
- runsAt: factory_1.factory.sortType.Ascending // ソートはrunsAtのみにしてみる(2026-08-04~)
474
- },
475
- executor: params.executor
476
- });
477
- if (doc === null) {
478
- return null;
479
- }
480
- const changedTask = {
481
- id: doc.id,
482
- status: factory_1.factory.taskStatus.Running
483
- };
484
- task_1.taskEventEmitter.emitTaskStatusChanged(changedTask);
485
- return doc;
486
- }
487
327
  /**
488
328
  * Runningのまま一定期間超過し、かつ、remainingNumberOfTries>0のタスクをReadyに変更する
489
329
  */
@@ -596,10 +436,8 @@ class TaskRepo {
596
436
  * タスクIDから実行結果とステータスを保管する
597
437
  * Abortedの場合、dateAbortedもセットする
598
438
  */
599
- async setExecutionResultAndStatus(params, update,
600
- // support customr function(2025-05-25~)
601
- next) {
602
- const { id, remainingNumberOfTries, name } = params;
439
+ async setExecutionResultAndStatus(params, update) {
440
+ const { id } = params;
603
441
  const { status, executionResult } = update;
604
442
  await this.taskModel.updateOne({ _id: { $eq: id } }, {
605
443
  $set: {
@@ -609,11 +447,12 @@ class TaskRepo {
609
447
  $push: { executionResults: executionResult }
610
448
  })
611
449
  .exec();
612
- // emit event(2025-05-26~)
613
- if (typeof next === 'function') {
614
- const changedTask = { id, name, status, remainingNumberOfTries, executionResult };
615
- task_1.taskEventEmitter.emitTaskStatusChanged(changedTask, next);
616
- }
450
+ // discontinue next(2026-08-05~)
451
+ // // emit event(2025-05-26~)
452
+ // if (typeof next === 'function') {
453
+ // const changedTask: IExecutedTask = { id, name, status, remainingNumberOfTries, executionResult };
454
+ // taskEventEmitter.emitTaskStatusChanged(changedTask, next);
455
+ // }
617
456
  }
618
457
  // 非同期アクションへ移行したので不要(2026-08-04~)
619
458
  // /**
@@ -677,22 +516,5 @@ class TaskRepo {
677
516
  })
678
517
  .exec();
679
518
  }
680
- async countDelayedTasks(params) {
681
- const { limit } = params;
682
- const runsAtLt = (0, moment_1.default)()
683
- .add(-params.delayInSeconds, 'seconds')
684
- .toDate();
685
- const query = this.taskModel.countDocuments({
686
- status: { $eq: factory_1.factory.taskStatus.Ready },
687
- runsAt: { $lt: runsAtLt },
688
- ...(Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined
689
- });
690
- if (typeof limit === 'number' && limit >= 0) {
691
- query.limit(limit);
692
- }
693
- const count = await query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
694
- .exec();
695
- return { count };
696
- }
697
519
  }
698
520
  exports.TaskRepo = TaskRepo;
@@ -0,0 +1,67 @@
1
+ import type { Connection } from 'mongoose';
2
+ import { factory } from '../factory';
3
+ /**
4
+ * 遅延タスクリポジトリ
5
+ */
6
+ export declare class TaskDelayedRepo {
7
+ private readonly taskModel;
8
+ constructor(connection: Connection);
9
+ /**
10
+ * 潜在的に実行されるべきタスクをカウントする
11
+ * add(2025-03-16~)
12
+ */
13
+ countPotentiallyRunning(params: {
14
+ name?: {
15
+ $eq?: factory.taskName;
16
+ $in?: factory.taskName[];
17
+ };
18
+ runsAt: {
19
+ $lt: Date;
20
+ };
21
+ limit: number;
22
+ }): Promise<{
23
+ count: number;
24
+ }>;
25
+ /**
26
+ * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更する
27
+ */
28
+ private runDelayedTask;
29
+ /**
30
+ * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
31
+ */
32
+ emitRunningIfExistsByName(params: {
33
+ /**
34
+ * 必ずタスク名指定で実行する
35
+ */
36
+ name: {
37
+ $eq: factory.taskName;
38
+ $in?: never;
39
+ } | {
40
+ $eq?: never;
41
+ $in: factory.taskName[];
42
+ };
43
+ runsAt: {
44
+ $lt: Date;
45
+ };
46
+ sort: {
47
+ numberOfTried?: factory.sortType;
48
+ runsAt?: factory.sortType;
49
+ };
50
+ executor: {
51
+ name: string;
52
+ };
53
+ }): Promise<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'> | null>;
54
+ /**
55
+ * 実行日時を一定期間過ぎたReadyタスクについて、Runningステータスに変更した上で、Runningイベントを発生させる
56
+ * taskNameで絞らない
57
+ * 2026-08-04~
58
+ */
59
+ emitRunningIfExistsNoName(params: {
60
+ runsAt: {
61
+ $lt: Date;
62
+ };
63
+ executor: {
64
+ name: string;
65
+ };
66
+ }): Promise<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'> | null>;
67
+ }