@chevre/domain 22.11.0-alpha.24 → 22.11.0-alpha.26
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.
- package/example/src/chevre/task/countTasks.ts +2 -1
- package/lib/chevre/repo/task.d.ts +6 -6
- package/lib/chevre/repo/task.js +13 -111
- package/lib/chevre/service/task.d.ts +6 -6
- package/lib/chevre/service/task.js +6 -4
- package/package.json +1 -1
- package/example/src/chevre/executeOneTask.ts +0 -41
- package/example/src/chevre/executeTaskIfExists.ts +0 -80
|
@@ -36,7 +36,8 @@ async function main() {
|
|
|
36
36
|
|
|
37
37
|
const runningTasksCountResult = await taskRepo.count({
|
|
38
38
|
limit: 100,
|
|
39
|
-
status: { $eq: chevre.factory.taskStatus.Running }
|
|
39
|
+
status: { $eq: chevre.factory.taskStatus.Running },
|
|
40
|
+
runsThrough: new Date()
|
|
40
41
|
});
|
|
41
42
|
console.log('runningTasksCountResult:', runningTasksCountResult);
|
|
42
43
|
},
|
|
@@ -86,9 +86,8 @@ export declare class TaskRepo {
|
|
|
86
86
|
* Readyのタスクをname指定でひとつRunningに変更する
|
|
87
87
|
*/
|
|
88
88
|
executeOneIfExists(params: {
|
|
89
|
-
name
|
|
90
|
-
$eq
|
|
91
|
-
$nin?: factory.taskName[];
|
|
89
|
+
name: {
|
|
90
|
+
$eq: factory.taskName;
|
|
92
91
|
};
|
|
93
92
|
executor: {
|
|
94
93
|
name: string;
|
|
@@ -96,6 +95,10 @@ export declare class TaskRepo {
|
|
|
96
95
|
runsAt: {
|
|
97
96
|
$lt: Date;
|
|
98
97
|
};
|
|
98
|
+
sort: {
|
|
99
|
+
numberOfTried?: factory.sortType;
|
|
100
|
+
runsAt?: factory.sortType;
|
|
101
|
+
};
|
|
99
102
|
}): Promise<IExecutableTask<factory.taskName> | null>;
|
|
100
103
|
/**
|
|
101
104
|
* add(2025-03-16~)
|
|
@@ -130,9 +133,6 @@ export declare class TaskRepo {
|
|
|
130
133
|
runsAt: factory.sortType;
|
|
131
134
|
};
|
|
132
135
|
}, next?: INextFunction): Promise<Pick<factory.task.ITask<factory.taskName>, 'id' | 'name'> | null>;
|
|
133
|
-
/**
|
|
134
|
-
* emit OnTaskStatusChanged on delayed tasks
|
|
135
|
-
*/
|
|
136
136
|
/**
|
|
137
137
|
* Readyのままで期限切れのタスクをExpiredに変更する
|
|
138
138
|
*/
|
package/lib/chevre/repo/task.js
CHANGED
|
@@ -30,10 +30,10 @@ const task_2 = require("./mongoose/schemas/task");
|
|
|
30
30
|
/**
|
|
31
31
|
* タスク実行時のソート条件
|
|
32
32
|
*/
|
|
33
|
-
const sortOrder4executionOfTasks = {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
33
|
+
// const sortOrder4executionOfTasks: { [key in keyof factory.task.IAttributes<factory.taskName>]?: factory.sortType } = {
|
|
34
|
+
// numberOfTried: factory.sortType.Ascending, // トライ回数の少なさ優先
|
|
35
|
+
// runsAt: factory.sortType.Ascending // 実行予定日時の早さ優先
|
|
36
|
+
// };
|
|
37
37
|
const executableTaskProjection = {
|
|
38
38
|
_id: 0,
|
|
39
39
|
id: { $toString: '$_id' },
|
|
@@ -394,15 +394,17 @@ class TaskRepo {
|
|
|
394
394
|
*/
|
|
395
395
|
executeOneIfExists(params) {
|
|
396
396
|
return __awaiter(this, void 0, void 0, function* () {
|
|
397
|
-
var _a
|
|
397
|
+
var _a;
|
|
398
398
|
if (!(params.runsAt.$lt instanceof Date)) {
|
|
399
399
|
throw new factory.errors.Argument('runsAt.$lt', 'must be Date');
|
|
400
400
|
}
|
|
401
401
|
const nameEq = (_a = params.name) === null || _a === void 0 ? void 0 : _a.$eq;
|
|
402
|
-
const nameNin =
|
|
403
|
-
const doc = yield this.taskModel.findOneAndUpdate(Object.assign({ status: { $eq: factory.taskStatus.Ready }, runsAt: { $lt: params.runsAt.$lt } }, (typeof nameEq === 'string'
|
|
402
|
+
// const nameNin = params.name?.$nin;
|
|
403
|
+
const doc = yield this.taskModel.findOneAndUpdate(Object.assign({ status: { $eq: factory.taskStatus.Ready }, runsAt: { $lt: params.runsAt.$lt } }, (typeof nameEq === 'string')
|
|
404
404
|
? {
|
|
405
|
-
name: Object.assign(
|
|
405
|
+
name: Object.assign({}, (typeof nameEq === 'string') ? { $eq: nameEq } : undefined
|
|
406
|
+
// ...(Array.isArray(nameNin)) ? { $nin: nameNin } : undefined
|
|
407
|
+
)
|
|
406
408
|
}
|
|
407
409
|
: undefined), {
|
|
408
410
|
$set: {
|
|
@@ -414,8 +416,9 @@ class TaskRepo {
|
|
|
414
416
|
remainingNumberOfTries: -1, // 残りトライ可能回数減らす
|
|
415
417
|
numberOfTried: 1 // トライ回数増やす
|
|
416
418
|
}
|
|
417
|
-
}, { new: true, projection: executableTaskProjection })
|
|
418
|
-
.sort
|
|
419
|
+
}, Object.assign({ new: true, projection: executableTaskProjection }, (typeof params.sort.numberOfTried === 'number' || typeof params.sort.runsAt === 'number')
|
|
420
|
+
? { sort: params.sort }
|
|
421
|
+
: undefined))
|
|
419
422
|
.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
|
|
420
423
|
.lean() // lean(2024-09-26~)
|
|
421
424
|
.exec();
|
|
@@ -499,107 +502,6 @@ class TaskRepo {
|
|
|
499
502
|
return doc;
|
|
500
503
|
});
|
|
501
504
|
}
|
|
502
|
-
// public async findExecutableOne(params: {
|
|
503
|
-
// name?: {
|
|
504
|
-
// $eq?: factory.taskName;
|
|
505
|
-
// $nin?: factory.taskName[];
|
|
506
|
-
// };
|
|
507
|
-
// runsAt: {
|
|
508
|
-
// $lt: Date;
|
|
509
|
-
// };
|
|
510
|
-
// }): Promise<IExecutableTask<factory.taskName> | null> {
|
|
511
|
-
// if (!(params.runsAt.$lt instanceof Date)) {
|
|
512
|
-
// throw new factory.errors.Argument('runsAt.$lt', 'must be Date');
|
|
513
|
-
// }
|
|
514
|
-
// const nameEq = params.name?.$eq;
|
|
515
|
-
// const nameNin = params.name?.$nin;
|
|
516
|
-
// const query = this.taskModel.findOne(
|
|
517
|
-
// {
|
|
518
|
-
// status: { $eq: factory.taskStatus.Ready },
|
|
519
|
-
// runsAt: { $lt: params.runsAt.$lt },
|
|
520
|
-
// ...(typeof nameEq === 'string' || Array.isArray(nameNin))
|
|
521
|
-
// ? {
|
|
522
|
-
// name: {
|
|
523
|
-
// ...(typeof nameEq === 'string') ? { $eq: nameEq } : undefined,
|
|
524
|
-
// ...(Array.isArray(nameNin)) ? { $nin: nameNin } : undefined
|
|
525
|
-
// }
|
|
526
|
-
// }
|
|
527
|
-
// : undefined
|
|
528
|
-
// },
|
|
529
|
-
// executableTaskProjection
|
|
530
|
-
// )
|
|
531
|
-
// .sort({
|
|
532
|
-
// runsAt: factory.sortType.Ascending
|
|
533
|
-
// });
|
|
534
|
-
// // .hint('executeOneByName');
|
|
535
|
-
// // const explainResult = await query.explain();
|
|
536
|
-
// // console.dir(explainResult, { depth: null });
|
|
537
|
-
// // console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage));
|
|
538
|
-
// const doc = await query.setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
|
|
539
|
-
// .lean<IExecutableTask<factory.taskName>>()
|
|
540
|
-
// .exec();
|
|
541
|
-
// if (doc === null) {
|
|
542
|
-
// // tslint:disable-next-line:no-null-keyword
|
|
543
|
-
// return null;
|
|
544
|
-
// }
|
|
545
|
-
// return doc;
|
|
546
|
-
// }
|
|
547
|
-
// discontinue(2025-05-26~)
|
|
548
|
-
/**
|
|
549
|
-
* emit OnTaskStatusChanged on delayed tasks
|
|
550
|
-
*/
|
|
551
|
-
// public async emitDelayedTasksEvent(params: {
|
|
552
|
-
// now: Date;
|
|
553
|
-
// /**
|
|
554
|
-
// * 指定期間遅延しているタスクを実行する
|
|
555
|
-
// */
|
|
556
|
-
// delayInSeconds: number;
|
|
557
|
-
// limit: number;
|
|
558
|
-
// name: {
|
|
559
|
-
// /**
|
|
560
|
-
// * 指定タスクのみ実行する
|
|
561
|
-
// */
|
|
562
|
-
// $in?: factory.taskName[];
|
|
563
|
-
// /**
|
|
564
|
-
// * 指定タスク以外のみ実行する
|
|
565
|
-
// */
|
|
566
|
-
// $nin?: factory.taskName[];
|
|
567
|
-
// };
|
|
568
|
-
// }) {
|
|
569
|
-
// const runsAtLt = moment(params.now)
|
|
570
|
-
// .add(-params.delayInSeconds, 'seconds')
|
|
571
|
-
// .toDate();
|
|
572
|
-
// const projection: ProjectionType<factory.task.ITask<factory.taskName>> = {
|
|
573
|
-
// _id: 0,
|
|
574
|
-
// id: { $toString: '$_id' },
|
|
575
|
-
// name: 1,
|
|
576
|
-
// status: 1
|
|
577
|
-
// };
|
|
578
|
-
// const delayedTasks = await this.taskModel.find(
|
|
579
|
-
// {
|
|
580
|
-
// status: { $eq: factory.taskStatus.Ready },
|
|
581
|
-
// runsAt: { $lt: runsAtLt },
|
|
582
|
-
// ...(Array.isArray(params.name.$in)) ? { name: { $in: params.name.$in } } : undefined,
|
|
583
|
-
// ...(Array.isArray(params.name.$nin)) ? { name: { $nin: params.name.$nin } } : undefined
|
|
584
|
-
// },
|
|
585
|
-
// projection
|
|
586
|
-
// )
|
|
587
|
-
// .limit(params.limit)
|
|
588
|
-
// .sort(sortOrder4executionOfTasks) // sort(2025-03-03~)
|
|
589
|
-
// .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS })
|
|
590
|
-
// .lean<IDelayedTask[]>() // lean(2024-09-26~)
|
|
591
|
-
// .exec();
|
|
592
|
-
// if (delayedTasks.length > 0) {
|
|
593
|
-
// delayedTasks.forEach((delayedTask) => {
|
|
594
|
-
// taskEventEmitter.emitTaskStatusChanged({
|
|
595
|
-
// id: delayedTask.id,
|
|
596
|
-
// name: delayedTask.name,
|
|
597
|
-
// status: factory.taskStatus.Ready
|
|
598
|
-
// });
|
|
599
|
-
// });
|
|
600
|
-
// }
|
|
601
|
-
// return delayedTasks;
|
|
602
|
-
// }
|
|
603
505
|
/**
|
|
604
506
|
* Readyのままで期限切れのタスクをExpiredに変更する
|
|
605
507
|
*/
|
|
@@ -23,13 +23,9 @@ declare function executeById(params: (IReadyTask | IRunningTask) & {
|
|
|
23
23
|
name: string;
|
|
24
24
|
};
|
|
25
25
|
}, next?: INextFunction): IOperation<void>;
|
|
26
|
-
/**
|
|
27
|
-
* support no name(2025-03-04~)
|
|
28
|
-
*/
|
|
29
26
|
declare function executeOneIfExists(params: {
|
|
30
|
-
name
|
|
31
|
-
$eq
|
|
32
|
-
$nin?: factory.taskName[];
|
|
27
|
+
name: {
|
|
28
|
+
$eq: factory.taskName;
|
|
33
29
|
};
|
|
34
30
|
executor: {
|
|
35
31
|
/**
|
|
@@ -40,5 +36,9 @@ declare function executeOneIfExists(params: {
|
|
|
40
36
|
runsAt: {
|
|
41
37
|
$lt: Date;
|
|
42
38
|
};
|
|
39
|
+
sort: {
|
|
40
|
+
numberOfTried?: factory.sortType;
|
|
41
|
+
runsAt?: factory.sortType;
|
|
42
|
+
};
|
|
43
43
|
}): IOperation<void>;
|
|
44
44
|
export { executeById, executeOneIfExists };
|
|
@@ -45,9 +45,6 @@ function executeById(params, next) {
|
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
/**
|
|
49
|
-
* support no name(2025-03-04~)
|
|
50
|
-
*/
|
|
51
48
|
function executeOneIfExists(params) {
|
|
52
49
|
return (settings) => __awaiter(this, void 0, void 0, function* () {
|
|
53
50
|
var _a;
|
|
@@ -56,7 +53,12 @@ function executeOneIfExists(params) {
|
|
|
56
53
|
// tslint:disable-next-line:no-null-keyword
|
|
57
54
|
let task = null;
|
|
58
55
|
try {
|
|
59
|
-
task = yield taskRepo.executeOneIfExists(
|
|
56
|
+
task = yield taskRepo.executeOneIfExists({
|
|
57
|
+
executor: { name: params.executor.name },
|
|
58
|
+
runsAt: { $lt: params.runsAt.$lt },
|
|
59
|
+
name: params.name,
|
|
60
|
+
sort: params.sort
|
|
61
|
+
});
|
|
60
62
|
debug('executable task found.', task, params.runsAt.$lt);
|
|
61
63
|
}
|
|
62
64
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// tslint:disable:no-console
|
|
2
|
-
import * as moment from 'moment';
|
|
3
|
-
import * as mongoose from 'mongoose';
|
|
4
|
-
|
|
5
|
-
import { chevre } from '../../../lib/index';
|
|
6
|
-
|
|
7
|
-
// const project = { id: String(process.env.PROJECT_ID) };
|
|
8
|
-
|
|
9
|
-
async function main() {
|
|
10
|
-
await mongoose.connect(<string>process.env.MONGOLAB_URI);
|
|
11
|
-
|
|
12
|
-
const taskRepo = await chevre.repository.Task.createInstance(mongoose.connection);
|
|
13
|
-
const task = await taskRepo.executeOneIfExists({
|
|
14
|
-
name: {
|
|
15
|
-
$nin: [
|
|
16
|
-
chevre.factory.taskName.DeleteTransaction,
|
|
17
|
-
chevre.factory.taskName.ImportEventCapacitiesFromCOA,
|
|
18
|
-
chevre.factory.taskName.ImportEventsFromCOA,
|
|
19
|
-
chevre.factory.taskName.AcceptCOAOffer,
|
|
20
|
-
chevre.factory.taskName.CheckMovieTicket,
|
|
21
|
-
chevre.factory.taskName.AuthorizePayment,
|
|
22
|
-
chevre.factory.taskName.PublishPaymentUrl
|
|
23
|
-
]
|
|
24
|
-
},
|
|
25
|
-
executor: { name: 'sample' },
|
|
26
|
-
runsAt: {
|
|
27
|
-
$lt: moment()
|
|
28
|
-
// tslint:disable-next-line:no-magic-numbers
|
|
29
|
-
.add(-2, 'years')
|
|
30
|
-
// .add(-60, 'seconds')
|
|
31
|
-
.toDate()
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// tslint:disable-next-line:no-null-keyword
|
|
36
|
-
console.dir(task, { depth: null });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
main()
|
|
40
|
-
.then()
|
|
41
|
-
.catch(console.error);
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
// tslint:disable:no-console
|
|
2
|
-
import { chevre } from '../../../lib/index';
|
|
3
|
-
|
|
4
|
-
import * as mongoose from 'mongoose';
|
|
5
|
-
import * as redis from 'redis';
|
|
6
|
-
|
|
7
|
-
export async function executeTaskIfExists() {
|
|
8
|
-
const redisClient = redis.createClient<redis.RedisDefaultModules, Record<string, never>, Record<string, never>>({
|
|
9
|
-
socket: {
|
|
10
|
-
port: Number(<string>process.env.REDIS_PORT),
|
|
11
|
-
host: <string>process.env.REDIS_HOST
|
|
12
|
-
},
|
|
13
|
-
password: <string>process.env.REDIS_KEY
|
|
14
|
-
});
|
|
15
|
-
await redisClient.connect();
|
|
16
|
-
|
|
17
|
-
await mongoose.connect(<string>process.env.MONGOLAB_URI);
|
|
18
|
-
|
|
19
|
-
let count = 0;
|
|
20
|
-
|
|
21
|
-
const MAX_NUBMER_OF_PARALLEL_TASKS = 1;
|
|
22
|
-
const INTERVAL_MILLISECONDS = 1000;
|
|
23
|
-
|
|
24
|
-
setInterval(
|
|
25
|
-
async () => {
|
|
26
|
-
if (count > MAX_NUBMER_OF_PARALLEL_TASKS) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
count += 1;
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
console.log('executing...', count);
|
|
34
|
-
await (await chevre.service.task.createService()).executeOneIfExists({
|
|
35
|
-
// name: chevre.factory.taskName.AggregateScreeningEvent,
|
|
36
|
-
executor: { name: 'sample' },
|
|
37
|
-
runsAt: { $lt: new Date() },
|
|
38
|
-
name: {
|
|
39
|
-
$nin: [
|
|
40
|
-
chevre.factory.taskName.DeleteTransaction,
|
|
41
|
-
chevre.factory.taskName.ImportEventCapacitiesFromCOA,
|
|
42
|
-
chevre.factory.taskName.ImportEventsFromCOA,
|
|
43
|
-
chevre.factory.taskName.AcceptCOAOffer,
|
|
44
|
-
chevre.factory.taskName.CheckMovieTicket,
|
|
45
|
-
chevre.factory.taskName.AuthorizePayment,
|
|
46
|
-
chevre.factory.taskName.PublishPaymentUrl
|
|
47
|
-
]
|
|
48
|
-
}
|
|
49
|
-
})({
|
|
50
|
-
connection: mongoose.connection,
|
|
51
|
-
redisClient,
|
|
52
|
-
credentials: {
|
|
53
|
-
sendGrid: await chevre.credentials.SendGrid.createInstance({ apiKey: 'xxx' })
|
|
54
|
-
},
|
|
55
|
-
settings: new chevre.settings.Settings({
|
|
56
|
-
deliverOrderLimit: 1,
|
|
57
|
-
abortedTasksWithoutReport: [],
|
|
58
|
-
coa: { timeout: 1000 },
|
|
59
|
-
gmo: { timeout: 1000, timeoutBackground: 1000, useFetch: true },
|
|
60
|
-
movieticketReserve: { timeout: 1000, timeoutCheck: 1000, minIntervalBetweenPayAndRefund: 0 },
|
|
61
|
-
numTryConfirmReserveTransaction: 10,
|
|
62
|
-
useExperimentalFeature: false
|
|
63
|
-
})
|
|
64
|
-
});
|
|
65
|
-
console.log('executed', count);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
console.error(error);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
count -= 1;
|
|
71
|
-
},
|
|
72
|
-
INTERVAL_MILLISECONDS
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
executeTaskIfExists()
|
|
77
|
-
.then(() => {
|
|
78
|
-
console.log('success!');
|
|
79
|
-
})
|
|
80
|
-
.catch(console.error);
|