@clairejs/server 3.28.0 → 3.28.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Change Log
2
2
 
3
- #### 3.28.0
3
+ #### 3.28.2
4
4
 
5
5
  - implement new job scheduler
6
6
 
@@ -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(job: AbstractJob): Promise<void>;
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(job: AbstractJob): Promise<void>;
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(job) {
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
- if (job.retryOnFail) {
82
- if ((job.retryCount || 0) < (job.maxRetry || 1)) {
83
- update.retryCount = (job.retryCount || 0) + 1;
84
- await this.retryJob({ ...job, ...update }, Date.now() + Math.max(job.retryDelayMs || 0, 60000));
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.cron) {
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(job: AbstractJob): Promise<void>;
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
@@ -71,8 +71,9 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
71
71
  await this.scheduler.send(deleteCommand);
72
72
  }
73
73
  async retryJob(job, at) {
74
- const jobName = this.getJobUniqueName(job.id);
75
- if (job.at) {
74
+ const { id, disabled, execCount, failCount, retryCount, lastError, lastRunAt, lastSuccessAt, ...payload } = job;
75
+ const jobName = this.getJobUniqueName(id);
76
+ if (at) {
76
77
  //-- update the job
77
78
  const getCommand = new GetScheduleCommand({
78
79
  Name: jobName,
@@ -87,11 +88,11 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
87
88
  }
88
89
  else {
89
90
  //-- schedule a new one-time job to re-execute this cron with minus one maxRetry
90
- await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry || 1) - 1 });
91
+ await this.scheduleJob({ ...payload, at, maxRetry: (payload.maxRetry || 0) - 1 });
91
92
  }
92
93
  }
93
- async cleanupJob(job) {
94
- return await this.removeJobFromScheduler(job.id);
94
+ async cleanupJob(jobId) {
95
+ return await this.removeJobFromScheduler(jobId);
95
96
  }
96
97
  async registerJob(job) {
97
98
  this.logger.debug("Scheduling job: ", job);
@@ -131,7 +132,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
131
132
  Input: JSON.stringify({
132
133
  requestContext: {
133
134
  cronScheduler: {
134
- data: job,
135
+ data: { id: job.id },
135
136
  },
136
137
  },
137
138
  }),
@@ -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(job: AbstractJob): Promise<void>;
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
- case CommunicationMessage.REMOVE_JOB:
78
- if (!this.isActive) {
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;
@@ -143,17 +147,19 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
143
147
  this.logger.debug("LocalJobScheduler exit");
144
148
  }
145
149
  async retryJob(job, at) {
150
+ const { id, disabled, execCount, failCount, retryCount, lastError, lastRunAt, lastSuccessAt, ...payload } = job;
146
151
  if (job.at) {
147
152
  //-- just register the job at new timestamp
148
153
  await this.registerJob({ ...job, at });
149
154
  }
150
155
  else {
151
- //-- schedule a new one-time job to re-execute this cron with minus one retry
152
- await this.scheduleJob({ ...job, at, maxRetry: (job.maxRetry || 1) - 1 });
156
+ //-- schedule a new one-time job to re-execute this cron with minus one maxRetry
157
+ await this.scheduleJob({ ...payload, at, maxRetry: (job.maxRetry || 0) - 1 });
153
158
  }
154
159
  }
155
- async cleanupJob(job) {
156
- this.jobHolder[job.id] = undefined;
160
+ async cleanupJob(jobId) {
161
+ this.jobHolder[jobId] = undefined;
162
+ this.sendJob(CommunicationMessage.CLEANUP_JOB, jobId);
157
163
  }
158
164
  async registerJob(job) {
159
165
  if (this.isActive) {
@@ -161,13 +167,13 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
161
167
  if (job.at) {
162
168
  //-- use the lib
163
169
  const timeout = setTimeout(() => {
164
- this.executeJob(job).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
170
+ this.executeJob(job.id).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
165
171
  }, new Date(job.at).getTime() - Date.now());
166
172
  this.jobHolder[job.id] = { jobCanceler: () => clearTimeout(timeout), job };
167
173
  }
168
174
  else if (job.cron) {
169
175
  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));
176
+ this.executeJob(job.id).catch((err) => this.logger.error(`Error execute job ${job.jobName} with id: ${job.id}`, err));
171
177
  });
172
178
  this.jobHolder[job.id] = { jobCanceler: () => scheduledJob.cancel(), job };
173
179
  }
@@ -1,7 +1,6 @@
1
1
  import { ObjectMetadata } from "@clairejs/core";
2
2
  export declare const CRON_REQUEST_METHOD = "cron";
3
3
  export interface JobRetryOptions {
4
- retryOnFail?: boolean;
5
4
  maxRetry?: number;
6
5
  retryDelayMs?: number;
7
6
  }
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.28.0",
3
+ "version": "3.28.2",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",