@chevre/domain 25.2.0-alpha.49 → 25.2.0-alpha.50
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/lib/chevre/repo/asyncAction.d.ts +2 -22
- package/lib/chevre/repo/asyncAction.js +50 -63
- package/lib/chevre/repo/mongoose/schemas/asyncAction.d.ts +1 -1
- package/lib/chevre/repo/mongoose/schemas/asyncAction.js +12 -82
- package/lib/chevre/service/asyncActionHandler/onOperationFailed.d.ts +0 -2
- package/lib/chevre/service/asyncActionHandler/onOperationFailed.js +2 -2
- package/lib/chevre/service/asyncActionHandler.js +1 -2
- package/lib/chevre/service/taskHandler/onOperationFailed.js +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Connection
|
|
1
|
+
import type { Connection } from 'mongoose';
|
|
2
2
|
import { factory } from '../factory';
|
|
3
3
|
import { IModel, IDocType } from './mongoose/schemas/asyncAction';
|
|
4
4
|
import type { IExecutableTask } from '../taskSettings';
|
|
5
|
-
type ISavingTask = Pick<factory.task.IAttributes<factory.taskName>, '
|
|
5
|
+
type ISavingTask = Pick<factory.task.IAttributes<factory.taskName>, 'data' | 'executionResults' | 'expires' | 'name' | 'project' | 'runsAt' | 'status'> & {
|
|
6
6
|
expires: Date;
|
|
7
7
|
};
|
|
8
8
|
export type IExecutableAsyncAction = Pick<IExecutableTask<factory.taskName>, 'data' | 'expires' | 'id' | 'name' | 'project' | 'runsAt' | 'status'>;
|
|
@@ -47,18 +47,6 @@ export declare class AsyncActionRepo {
|
|
|
47
47
|
}, inclusion: IKeyOfProjection[]): Promise<(IDocType & {
|
|
48
48
|
id: string;
|
|
49
49
|
} | undefined)>;
|
|
50
|
-
/**
|
|
51
|
-
* Readyのままで期限切れのタスクをExpiredに変更する
|
|
52
|
-
*/
|
|
53
|
-
makeExpiredMany(params: {
|
|
54
|
-
expiresLt: Date;
|
|
55
|
-
}): Promise<UpdateWriteOpResult>;
|
|
56
|
-
/**
|
|
57
|
-
* taskNameに関わらず、Runningのまま放置された非同期アクションをAbortedに変更する
|
|
58
|
-
*/
|
|
59
|
-
abortMany(params: {
|
|
60
|
-
intervalInMinutes: number;
|
|
61
|
-
}): Promise<UpdateWriteOpResult>;
|
|
62
50
|
/**
|
|
63
51
|
* タスクIDから実行結果とステータスを保管する
|
|
64
52
|
* Abortedの場合、dateAbortedもセットする
|
|
@@ -72,13 +60,5 @@ export declare class AsyncActionRepo {
|
|
|
72
60
|
status: factory.taskStatus.Executed | factory.taskStatus.Aborted;
|
|
73
61
|
executionResult: factory.task.IExecutionResult;
|
|
74
62
|
}): Promise<void>;
|
|
75
|
-
/**
|
|
76
|
-
* 不要なタスクを削除する
|
|
77
|
-
*/
|
|
78
|
-
deleteRunsAtPassedCertainPeriod(params: {
|
|
79
|
-
runsAt: {
|
|
80
|
-
$lt: Date;
|
|
81
|
-
};
|
|
82
|
-
}): Promise<import("mongodb").DeleteResult>;
|
|
83
63
|
}
|
|
84
64
|
export {};
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.AsyncActionRepo = void 0;
|
|
7
|
-
const moment_1 = __importDefault(require("moment"));
|
|
8
4
|
const factory_1 = require("../factory");
|
|
9
5
|
const settings_1 = require("../settings");
|
|
10
6
|
const asyncAction_1 = require("./mongoose/schemas/asyncAction");
|
|
@@ -20,9 +16,6 @@ const executableTaskProjection = {
|
|
|
20
16
|
expires: 1
|
|
21
17
|
};
|
|
22
18
|
const AVAILABLE_PROJECT_FIELDS = [
|
|
23
|
-
'alternateName',
|
|
24
|
-
'identifier',
|
|
25
|
-
'description',
|
|
26
19
|
'project',
|
|
27
20
|
'name',
|
|
28
21
|
'status',
|
|
@@ -71,7 +64,7 @@ class AsyncActionRepo {
|
|
|
71
64
|
_id: { $eq: params.id },
|
|
72
65
|
status: { $eq: factory_1.factory.taskStatus.Ready }, // Readyのものしか実行しない
|
|
73
66
|
runsAt: { $lt: now },
|
|
74
|
-
expires: { $
|
|
67
|
+
expires: { $gt: now }, // ワンタイムタスクなのでexpiresは必ず存在する
|
|
75
68
|
}, {
|
|
76
69
|
$set: {
|
|
77
70
|
status: factory_1.factory.taskStatus.Running, // 実行中に変更
|
|
@@ -116,45 +109,55 @@ class AsyncActionRepo {
|
|
|
116
109
|
}
|
|
117
110
|
return doc;
|
|
118
111
|
}
|
|
119
|
-
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
async makeExpiredMany(params
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
112
|
+
// /**
|
|
113
|
+
// * Readyのままで期限切れのタスクをExpiredに変更する
|
|
114
|
+
// */
|
|
115
|
+
// public async makeExpiredMany(params: {
|
|
116
|
+
// expiresLt: Date;
|
|
117
|
+
// }): Promise<UpdateWriteOpResult> {
|
|
118
|
+
// const { expiresLt } = params;
|
|
119
|
+
// if (!(expiresLt instanceof Date)) {
|
|
120
|
+
// throw new factory.errors.Argument('expiresLt', 'must be Date');
|
|
121
|
+
// }
|
|
122
|
+
// return this.asyncActionModel.updateMany(
|
|
123
|
+
// {
|
|
124
|
+
// status: { $eq: factory.taskStatus.Ready },
|
|
125
|
+
// expires: { $lt: expiresLt }
|
|
126
|
+
// },
|
|
127
|
+
// {
|
|
128
|
+
// $set: {
|
|
129
|
+
// status: factory.taskStatus.Expired
|
|
130
|
+
// }
|
|
131
|
+
// }
|
|
132
|
+
// )
|
|
133
|
+
// .exec();
|
|
134
|
+
// }
|
|
135
|
+
// /**
|
|
136
|
+
// * taskNameに関わらず、Runningのまま放置された非同期アクションをAbortedに変更する
|
|
137
|
+
// */
|
|
138
|
+
// public async abortMany(params: {
|
|
139
|
+
// intervalInMinutes: number;
|
|
140
|
+
// }): Promise<UpdateWriteOpResult> {
|
|
141
|
+
// const lastTriedAtShoudBeLessThan = moment()
|
|
142
|
+
// .add(-params.intervalInMinutes, 'minutes')
|
|
143
|
+
// .toDate();
|
|
144
|
+
// return this.asyncActionModel.updateMany(
|
|
145
|
+
// {
|
|
146
|
+
// status: { $eq: factory.taskStatus.Running },
|
|
147
|
+
// lastTriedAt: {
|
|
148
|
+
// $exists: true,
|
|
149
|
+
// $lt: lastTriedAtShoudBeLessThan
|
|
150
|
+
// }
|
|
151
|
+
// },
|
|
152
|
+
// {
|
|
153
|
+
// $set: {
|
|
154
|
+
// status: factory.taskStatus.Aborted,
|
|
155
|
+
// dateAborted: new Date()
|
|
156
|
+
// }
|
|
157
|
+
// }
|
|
158
|
+
// )
|
|
159
|
+
// .exec();
|
|
160
|
+
// }
|
|
158
161
|
/**
|
|
159
162
|
* タスクIDから実行結果とステータスを保管する
|
|
160
163
|
* Abortedの場合、dateAbortedもセットする
|
|
@@ -171,21 +174,5 @@ class AsyncActionRepo {
|
|
|
171
174
|
})
|
|
172
175
|
.exec();
|
|
173
176
|
}
|
|
174
|
-
/**
|
|
175
|
-
* 不要なタスクを削除する
|
|
176
|
-
*/
|
|
177
|
-
async deleteRunsAtPassedCertainPeriod(params) {
|
|
178
|
-
return this.asyncActionModel.deleteMany({
|
|
179
|
-
runsAt: { $lt: params.runsAt.$lt },
|
|
180
|
-
status: {
|
|
181
|
-
$in: [
|
|
182
|
-
factory_1.factory.taskStatus.Aborted,
|
|
183
|
-
factory_1.factory.taskStatus.Executed,
|
|
184
|
-
factory_1.factory.taskStatus.Expired
|
|
185
|
-
]
|
|
186
|
-
}
|
|
187
|
-
})
|
|
188
|
-
.exec();
|
|
189
|
-
}
|
|
190
177
|
}
|
|
191
178
|
exports.AsyncActionRepo = AsyncActionRepo;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IndexDefinition, IndexOptions, Model, Schema, SchemaDefinition } from 'mongoose';
|
|
2
2
|
import { IVirtuals } from '../virtuals';
|
|
3
3
|
import { factory } from '../../../factory';
|
|
4
|
-
type IDocType = Pick<factory.task.ITask<factory.taskName>, '
|
|
4
|
+
type IDocType = Pick<factory.task.ITask<factory.taskName>, 'data' | 'dateAborted' | 'executionResults' | 'executor' | 'expires' | 'lastTriedAt' | 'name' | 'project' | 'runsAt' | 'status'>;
|
|
5
5
|
type IModel = Model<IDocType, Record<string, never>, Record<string, never>, IVirtuals>;
|
|
6
6
|
type ISchemaDefinition = SchemaDefinition<IDocType>;
|
|
7
7
|
type ISchema = Schema<IDocType, IModel, Record<string, never>, Record<string, never>, IVirtuals, Record<string, never>, ISchemaDefinition, IDocType>;
|
|
@@ -8,9 +8,6 @@ const settings_1 = require("../../../settings");
|
|
|
8
8
|
const modelName = 'AsyncAction';
|
|
9
9
|
exports.modelName = modelName;
|
|
10
10
|
const schemaDefinition = {
|
|
11
|
-
alternateName: String, // add(2025-03-28~)
|
|
12
|
-
identifier: String,
|
|
13
|
-
description: String, // add(2025-03-30~)
|
|
14
11
|
project: { type: mongoose_1.SchemaTypes.Mixed, required: true },
|
|
15
12
|
name: { type: String, required: true },
|
|
16
13
|
status: { type: String, required: true },
|
|
@@ -48,98 +45,31 @@ const schemaOptions = {
|
|
|
48
45
|
versionKey: false
|
|
49
46
|
}
|
|
50
47
|
};
|
|
48
|
+
const EXPIRE_AFTER_SECONDS = 3600; // 1 hour
|
|
51
49
|
const indexes = [
|
|
52
50
|
[
|
|
53
|
-
{
|
|
54
|
-
{ name: 'projectId' }
|
|
55
|
-
],
|
|
56
|
-
[
|
|
57
|
-
{ name: 1, runsAt: -1 },
|
|
58
|
-
{ name: 'asyncActionName' }
|
|
59
|
-
],
|
|
60
|
-
[
|
|
61
|
-
{ status: 1, runsAt: -1 },
|
|
62
|
-
{ name: 'status' }
|
|
63
|
-
],
|
|
64
|
-
[
|
|
65
|
-
{ runsAt: -1 },
|
|
66
|
-
{ name: 'runsAt' }
|
|
67
|
-
],
|
|
68
|
-
[
|
|
69
|
-
{ identifier: 1, runsAt: -1 },
|
|
51
|
+
{ expires: 1 },
|
|
70
52
|
{
|
|
71
|
-
name: '
|
|
72
|
-
|
|
73
|
-
identifier: { $exists: true }
|
|
74
|
-
}
|
|
53
|
+
name: 'ttlByExpires',
|
|
54
|
+
expireAfterSeconds: EXPIRE_AFTER_SECONDS
|
|
75
55
|
}
|
|
76
56
|
],
|
|
77
57
|
[
|
|
78
|
-
{
|
|
79
|
-
{
|
|
80
|
-
name: 'dateAborted',
|
|
81
|
-
partialFilterExpression: {
|
|
82
|
-
dateAborted: { $exists: true }
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
],
|
|
86
|
-
[
|
|
87
|
-
{ lastTriedAt: 1, runsAt: -1 },
|
|
88
|
-
{
|
|
89
|
-
name: 'lastTriedAt',
|
|
90
|
-
partialFilterExpression: {
|
|
91
|
-
lastTriedAt: { $exists: true }
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
],
|
|
95
|
-
[
|
|
96
|
-
{ expires: 1, runsAt: -1 },
|
|
97
|
-
{
|
|
98
|
-
name: 'expires',
|
|
99
|
-
partialFilterExpression: {
|
|
100
|
-
expires: { $exists: true }
|
|
101
|
-
}
|
|
102
|
-
}
|
|
58
|
+
{ runsAt: -1 },
|
|
59
|
+
{ name: 'runsAt' }
|
|
103
60
|
],
|
|
104
61
|
[
|
|
105
|
-
{
|
|
106
|
-
{
|
|
107
|
-
name: 'abortMany',
|
|
108
|
-
partialFilterExpression: {
|
|
109
|
-
lastTriedAt: { $exists: true }
|
|
110
|
-
}
|
|
111
|
-
}
|
|
62
|
+
{ 'project.id': 1, runsAt: -1 },
|
|
63
|
+
{ name: 'projectId' }
|
|
112
64
|
],
|
|
113
65
|
[
|
|
114
|
-
{
|
|
115
|
-
{
|
|
116
|
-
name: 'makeExpiredMany',
|
|
117
|
-
partialFilterExpression: {
|
|
118
|
-
expires: { $exists: true }
|
|
119
|
-
}
|
|
120
|
-
}
|
|
66
|
+
{ name: 1, runsAt: -1 },
|
|
67
|
+
{ name: 'asyncActionName' }
|
|
121
68
|
],
|
|
122
69
|
[
|
|
123
|
-
|
|
124
|
-
{
|
|
125
|
-
{
|
|
126
|
-
unique: true,
|
|
127
|
-
name: 'uniqueAlternateName',
|
|
128
|
-
partialFilterExpression: {
|
|
129
|
-
alternateName: { $exists: true }
|
|
130
|
-
}
|
|
131
|
-
}
|
|
70
|
+
{ status: 1, runsAt: -1 },
|
|
71
|
+
{ name: 'status' }
|
|
132
72
|
],
|
|
133
|
-
[
|
|
134
|
-
{ identifier: 1 },
|
|
135
|
-
{
|
|
136
|
-
unique: true,
|
|
137
|
-
name: 'uniqueIdentifier',
|
|
138
|
-
partialFilterExpression: {
|
|
139
|
-
identifier: { $exists: true }
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
]
|
|
143
73
|
];
|
|
144
74
|
exports.indexes = indexes;
|
|
145
75
|
/**
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { IExecutedTask } from '../../eventEmitter/task';
|
|
2
|
-
import type { SettingRepo } from '../../repo/setting';
|
|
3
2
|
import type { AsyncActionRepo, IExecutableAsyncAction } from '../../repo/asyncAction';
|
|
4
3
|
/**
|
|
5
4
|
* タスク実行失敗時処理
|
|
@@ -9,7 +8,6 @@ declare function onOperationFailed(params: {
|
|
|
9
8
|
now: Date;
|
|
10
9
|
error: any;
|
|
11
10
|
}): (repos: {
|
|
12
|
-
setting: SettingRepo;
|
|
13
11
|
asyncAction: AsyncActionRepo;
|
|
14
12
|
}) => Promise<{
|
|
15
13
|
executedTask: IExecutedTask;
|
|
@@ -9,7 +9,7 @@ function onOperationFailed(params) {
|
|
|
9
9
|
return async (repos) => {
|
|
10
10
|
let error = params.error;
|
|
11
11
|
const { task, now } = params;
|
|
12
|
-
if (typeof error !== 'object') {
|
|
12
|
+
if (error === null || typeof error !== 'object') {
|
|
13
13
|
error = { message: String(error) };
|
|
14
14
|
}
|
|
15
15
|
const endDate = new Date();
|
|
@@ -34,7 +34,7 @@ function onOperationFailed(params) {
|
|
|
34
34
|
id: task.id,
|
|
35
35
|
remainingNumberOfTries: 0,
|
|
36
36
|
name: task.name,
|
|
37
|
-
status:
|
|
37
|
+
status: factory_1.factory.taskStatus.Aborted,
|
|
38
38
|
executionResult: result
|
|
39
39
|
};
|
|
40
40
|
return { executedTask };
|
|
@@ -22,7 +22,6 @@ function executeAsyncAction(task, next) {
|
|
|
22
22
|
const now = new Date();
|
|
23
23
|
debug('executing an executableTask...', task, now);
|
|
24
24
|
return async (settings, options) => {
|
|
25
|
-
const settingRepo = new (await import('../repo/setting.js')).SettingRepo(settings.connection);
|
|
26
25
|
const asyncActionRepo = new (await import('../repo/asyncAction.js')).AsyncActionRepo(settings.connection);
|
|
27
26
|
let executedTask;
|
|
28
27
|
try {
|
|
@@ -69,7 +68,7 @@ function executeAsyncAction(task, next) {
|
|
|
69
68
|
catch (error) {
|
|
70
69
|
const onOperationFailedResult = await (0, onOperationFailed_1.onOperationFailed)({
|
|
71
70
|
task, now, error,
|
|
72
|
-
})({
|
|
71
|
+
})({ asyncAction: asyncActionRepo });
|
|
73
72
|
executedTask = onOperationFailedResult.executedTask;
|
|
74
73
|
}
|
|
75
74
|
finally {
|
|
@@ -10,7 +10,7 @@ function onOperationFailed(params) {
|
|
|
10
10
|
return async (repos) => {
|
|
11
11
|
let error = params.error;
|
|
12
12
|
const { task, now, next } = params;
|
|
13
|
-
if (typeof error !== 'object') {
|
|
13
|
+
if (error === null || typeof error !== 'object') {
|
|
14
14
|
error = { message: String(error) };
|
|
15
15
|
}
|
|
16
16
|
const endDate = new Date();
|
package/package.json
CHANGED