@clairejs/server 3.28.0 → 3.28.1
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/README.md +1 -1
- package/dist/job/AbstractJobScheduler.d.ts +2 -2
- package/dist/job/AbstractJobScheduler.js +14 -11
- package/dist/job/AwsJobScheduler.d.ts +2 -2
- package/dist/job/AwsJobScheduler.js +5 -5
- package/dist/job/LocalJobScheduler.d.ts +1 -1
- package/dist/job/LocalJobScheduler.js +21 -16
- package/dist/job/interfaces.d.ts +0 -1
- package/dist/system/LambdaWrapper.js +0 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ export declare abstract class AbstractJobScheduler {
|
|
|
25
25
|
*/
|
|
26
26
|
protected abstract cancelJob(id: string): Promise<void>;
|
|
27
27
|
protected abstract retryJob(job: AbstractJob, at: number): Promise<void>;
|
|
28
|
-
protected abstract cleanupJob(
|
|
28
|
+
protected abstract cleanupJob(id: string): Promise<void>;
|
|
29
29
|
/**
|
|
30
30
|
* Remove the scheduled job and prevent if from running in the future
|
|
31
31
|
* @param id The job id returned from scheduleJobAt function
|
|
@@ -35,6 +35,6 @@ export declare abstract class AbstractJobScheduler {
|
|
|
35
35
|
* Execute the scheduled job
|
|
36
36
|
* @param job The schedled job info to execute
|
|
37
37
|
*/
|
|
38
|
-
protected executeJob(
|
|
38
|
+
protected executeJob(jobId: string): Promise<void>;
|
|
39
39
|
}
|
|
40
40
|
export {};
|
|
@@ -39,7 +39,12 @@ export class AbstractJobScheduler {
|
|
|
39
39
|
* Execute the scheduled job
|
|
40
40
|
* @param job The schedled job info to execute
|
|
41
41
|
*/
|
|
42
|
-
async executeJob(
|
|
42
|
+
async executeJob(jobId) {
|
|
43
|
+
const job = await this.jobRepo.getJobById(jobId);
|
|
44
|
+
if (!job) {
|
|
45
|
+
await this.cancelJob(jobId);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
43
48
|
if (job.disabled) {
|
|
44
49
|
return;
|
|
45
50
|
}
|
|
@@ -60,7 +65,7 @@ export class AbstractJobScheduler {
|
|
|
60
65
|
this.logger.debug(`Calling job handler for ${job.id}`);
|
|
61
66
|
await jobHandler.handlerFn(job);
|
|
62
67
|
this.logger.debug(`Job handler for ${job.id} completed`);
|
|
63
|
-
await this.cleanupJob(job).catch((err) => this.logger.error(`Failed to cleanup job: ${job.id}`, err));
|
|
68
|
+
await this.cleanupJob(job.id).catch((err) => this.logger.error(`Failed to cleanup job: ${job.id}`, err));
|
|
64
69
|
//-- job run success, update
|
|
65
70
|
update.lastSuccessAt = Date.now();
|
|
66
71
|
//-- reset retry count if this is cron job
|
|
@@ -78,16 +83,15 @@ export class AbstractJobScheduler {
|
|
|
78
83
|
update.failCount = (job.failCount || 0) + 1;
|
|
79
84
|
update.lastError = String(err);
|
|
80
85
|
//-- job run error, check retry
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
86
|
+
const maxRetry = job.maxRetry || 0;
|
|
87
|
+
if (maxRetry) {
|
|
88
|
+
const currentRetryCount = job.retryCount || 0;
|
|
89
|
+
if (currentRetryCount < maxRetry) {
|
|
90
|
+
await this.retryJob(job, Date.now() + Math.max(job.retryDelayMs || 0, 60000));
|
|
91
|
+
update.retryCount = currentRetryCount + 1;
|
|
85
92
|
}
|
|
86
93
|
else {
|
|
87
|
-
if (job.
|
|
88
|
-
update.at = undefined;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
94
|
+
if (job.at) {
|
|
91
95
|
update.disabled = true;
|
|
92
96
|
await this.cancelJob(job.id);
|
|
93
97
|
}
|
|
@@ -95,7 +99,6 @@ export class AbstractJobScheduler {
|
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
await this.jobRepo.updateJobById(job.id, update, tx);
|
|
98
|
-
Object.assign(job, update);
|
|
99
102
|
}
|
|
100
103
|
await tx.commit();
|
|
101
104
|
}
|
|
@@ -13,13 +13,13 @@ export declare class AwsJobScheduler extends AbstractJobScheduler {
|
|
|
13
13
|
protected readonly jobNamespace: string;
|
|
14
14
|
protected readonly scheduler: SchedulerClient;
|
|
15
15
|
constructor(logger: AbstractLogger, db: AbstractDbAdapter, jobRepo: AbstractJobRepository, apiLambdaFunctionArn: string, apiLambdaFunctionRoleArn: string, jobNamespace: string);
|
|
16
|
-
handleCron(jobInfo: AbstractJob): Promise<void>;
|
|
16
|
+
handleCron(jobInfo: Pick<AbstractJob, "id">): Promise<void>;
|
|
17
17
|
private convertCronToSchedulerExpression;
|
|
18
18
|
private getJobUniqueName;
|
|
19
19
|
private getOneTimeExpression;
|
|
20
20
|
private removeJobFromScheduler;
|
|
21
21
|
protected retryJob(job: AbstractJob, at: number): Promise<void>;
|
|
22
|
-
protected cleanupJob(
|
|
22
|
+
protected cleanupJob(jobId: string): Promise<void>;
|
|
23
23
|
protected registerJob(job: AbstractJob): Promise<void>;
|
|
24
24
|
cancelJob(jobId: string): Promise<void>;
|
|
25
25
|
}
|
|
@@ -37,7 +37,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
|
|
|
37
37
|
}
|
|
38
38
|
async handleCron(jobInfo) {
|
|
39
39
|
this.logger.debug(`Handle cron`, jobInfo);
|
|
40
|
-
await this.executeJob(jobInfo);
|
|
40
|
+
await this.executeJob(jobInfo.id);
|
|
41
41
|
}
|
|
42
42
|
convertCronToSchedulerExpression(cron) {
|
|
43
43
|
// AWS Scheduler uses a different cron format than standard cron
|
|
@@ -87,11 +87,11 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
|
|
|
87
87
|
}
|
|
88
88
|
else {
|
|
89
89
|
//-- schedule a new one-time job to re-execute this cron with minus one maxRetry
|
|
90
|
-
await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry ||
|
|
90
|
+
await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry || 0) - 1 });
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
-
async cleanupJob(
|
|
94
|
-
return await this.removeJobFromScheduler(
|
|
93
|
+
async cleanupJob(jobId) {
|
|
94
|
+
return await this.removeJobFromScheduler(jobId);
|
|
95
95
|
}
|
|
96
96
|
async registerJob(job) {
|
|
97
97
|
this.logger.debug("Scheduling job: ", job);
|
|
@@ -131,7 +131,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
|
|
|
131
131
|
Input: JSON.stringify({
|
|
132
132
|
requestContext: {
|
|
133
133
|
cronScheduler: {
|
|
134
|
-
data: job,
|
|
134
|
+
data: { id: job.id },
|
|
135
135
|
},
|
|
136
136
|
},
|
|
137
137
|
}),
|
|
@@ -54,7 +54,7 @@ export declare class LocalJobScheduler extends AbstractJobScheduler implements I
|
|
|
54
54
|
init(): Promise<void>;
|
|
55
55
|
exit(): void;
|
|
56
56
|
protected retryJob(job: AbstractJob, at: number): Promise<void>;
|
|
57
|
-
protected cleanupJob(
|
|
57
|
+
protected cleanupJob(jobId: string): Promise<void>;
|
|
58
58
|
protected registerJob(job: AbstractJob): Promise<void>;
|
|
59
59
|
protected cancelJob(jobId: string): Promise<void>;
|
|
60
60
|
}
|
|
@@ -18,6 +18,7 @@ var CommunicationMessage;
|
|
|
18
18
|
(function (CommunicationMessage) {
|
|
19
19
|
CommunicationMessage["SCHEDULE_JOB"] = "SCHEDULE_JOB";
|
|
20
20
|
CommunicationMessage["REMOVE_JOB"] = "REMOVE_JOB";
|
|
21
|
+
CommunicationMessage["CLEANUP_JOB"] = "CLEANUP_JOB";
|
|
21
22
|
})(CommunicationMessage || (CommunicationMessage = {}));
|
|
22
23
|
let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
23
24
|
logger;
|
|
@@ -65,23 +66,26 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
65
66
|
this.subscribeClient.publish(this.multiClientChannel, JSON.stringify({ type, data }));
|
|
66
67
|
}
|
|
67
68
|
async processMessage(type, data) {
|
|
69
|
+
if (!this.isActive) {
|
|
70
|
+
//-- not active scheduler, ignore
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
68
73
|
switch (type) {
|
|
69
|
-
case CommunicationMessage.SCHEDULE_JOB:
|
|
70
|
-
if (!this.isActive) {
|
|
71
|
-
//-- not active scheduler, ignore
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
+
case CommunicationMessage.SCHEDULE_JOB: {
|
|
74
75
|
const jobInfo = data;
|
|
75
76
|
await this.registerJob(jobInfo);
|
|
76
77
|
break;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//-- not active scheduler, ignore
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
78
|
+
}
|
|
79
|
+
case CommunicationMessage.REMOVE_JOB: {
|
|
82
80
|
const jobId = data;
|
|
83
81
|
await this.cancelJob(jobId);
|
|
84
82
|
break;
|
|
83
|
+
}
|
|
84
|
+
case CommunicationMessage.CLEANUP_JOB: {
|
|
85
|
+
const jobId = data;
|
|
86
|
+
await this.cleanupJob(jobId);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
85
89
|
default:
|
|
86
90
|
this.logger.error(`Not recognize message type ${type}`);
|
|
87
91
|
break;
|
|
@@ -148,12 +152,13 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
148
152
|
await this.registerJob({ ...job, at });
|
|
149
153
|
}
|
|
150
154
|
else {
|
|
151
|
-
//-- schedule a new one-time job to re-execute this cron with minus one
|
|
152
|
-
await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry ||
|
|
155
|
+
//-- schedule a new one-time job to re-execute this cron with minus one maxRetry
|
|
156
|
+
await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry || 0) - 1 });
|
|
153
157
|
}
|
|
154
158
|
}
|
|
155
|
-
async cleanupJob(
|
|
156
|
-
this.jobHolder[
|
|
159
|
+
async cleanupJob(jobId) {
|
|
160
|
+
this.jobHolder[jobId] = undefined;
|
|
161
|
+
this.sendJob(CommunicationMessage.CLEANUP_JOB, jobId);
|
|
157
162
|
}
|
|
158
163
|
async registerJob(job) {
|
|
159
164
|
if (this.isActive) {
|
|
@@ -161,13 +166,13 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
161
166
|
if (job.at) {
|
|
162
167
|
//-- use the lib
|
|
163
168
|
const timeout = setTimeout(() => {
|
|
164
|
-
this.executeJob(job).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
|
|
169
|
+
this.executeJob(job.id).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
|
|
165
170
|
}, new Date(job.at).getTime() - Date.now());
|
|
166
171
|
this.jobHolder[job.id] = { jobCanceler: () => clearTimeout(timeout), job };
|
|
167
172
|
}
|
|
168
173
|
else if (job.cron) {
|
|
169
174
|
const scheduledJob = scheduler.scheduleJob(job.cron, () => {
|
|
170
|
-
this.executeJob(job).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
|
|
175
|
+
this.executeJob(job.id).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
|
|
171
176
|
});
|
|
172
177
|
this.jobHolder[job.id] = { jobCanceler: () => scheduledJob.cancel(), job };
|
|
173
178
|
}
|
package/dist/job/interfaces.d.ts
CHANGED
|
@@ -106,7 +106,6 @@ export class LambdaWrapper {
|
|
|
106
106
|
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
107
107
|
}
|
|
108
108
|
if (requestOptions.method === CRON_REQUEST_METHOD) {
|
|
109
|
-
console.log("handle cron", requestOptions.body);
|
|
110
109
|
await this.jobScheduler?.handleCron(requestOptions.body);
|
|
111
110
|
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
112
111
|
}
|