@clairejs/server 3.28.3 → 3.28.5

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.3
3
+ #### 3.28.5
4
4
 
5
5
  - implement new job scheduler
6
6
 
@@ -26,6 +26,7 @@ export declare abstract class AbstractJobScheduler {
26
26
  protected abstract cancelJob(id: string): Promise<void>;
27
27
  protected abstract retryJob(job: AbstractJob, at: number): Promise<void>;
28
28
  protected abstract cleanupJob(id: string): Promise<void>;
29
+ protected afterJobRun(job: AbstractJob): Promise<void>;
29
30
  /**
30
31
  * Remove the scheduled job and prevent if from running in the future
31
32
  * @param id The job id returned from scheduleJobAt function
@@ -27,6 +27,11 @@ export class AbstractJobScheduler {
27
27
  await this.registerJob({ ...payload, id: jobId });
28
28
  return jobId;
29
29
  }
30
+ async afterJobRun(job) {
31
+ if (job.at) {
32
+ await this.cleanupJob(job.id).catch((err) => this.logger.error(`Failed to cleanup one time job ${job.id}`, err));
33
+ }
34
+ }
30
35
  /**
31
36
  * Remove the scheduled job and prevent if from running in the future
32
37
  * @param id The job id returned from scheduleJobAt function
@@ -65,7 +70,7 @@ export class AbstractJobScheduler {
65
70
  this.logger.debug(`Calling job handler for ${job.id}`);
66
71
  await jobHandler.handlerFn(job);
67
72
  this.logger.debug(`Job handler for ${job.id} completed`);
68
- await this.cleanupJob(job.id).catch((err) => this.logger.error(`Failed to cleanup job: ${job.id}`, err));
73
+ await this.afterJobRun(job);
69
74
  //-- job run success, update
70
75
  update.lastSuccessAt = Date.now();
71
76
  //-- reset retry count if this is cron job
@@ -84,18 +89,18 @@ export class AbstractJobScheduler {
84
89
  update.lastError = String(err);
85
90
  //-- job run error, check retry
86
91
  const maxRetry = job.maxRetry || 0;
92
+ let retry = false;
87
93
  if (maxRetry) {
88
94
  const currentRetryCount = job.retryCount || 0;
89
- if (currentRetryCount < maxRetry) {
95
+ retry = currentRetryCount < maxRetry;
96
+ if (retry) {
90
97
  await this.retryJob(job, Date.now() + Math.max(job.retryDelayMs || 0, 60000));
91
98
  update.retryCount = currentRetryCount + 1;
92
99
  }
93
- else {
94
- if (job.at) {
95
- update.disabled = true;
96
- await this.cancelJob(job.id);
97
- }
98
- }
100
+ }
101
+ if (!retry && job.at) {
102
+ update.disabled = true;
103
+ await this.cancelJob(job.id);
99
104
  }
100
105
  }
101
106
  await this.jobRepo.updateJobById(job.id, update, tx);
@@ -88,7 +88,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
88
88
  }
89
89
  else {
90
90
  //-- schedule a new one-time job to re-execute this cron with minus one maxRetry
91
- await this.scheduleJob({ ...payload, at, maxRetry: (payload.maxRetry || 0) - 1 });
91
+ await this.scheduleJob({ ...payload, cron: undefined, at, maxRetry: (payload.maxRetry || 0) - 1 });
92
92
  }
93
93
  }
94
94
  async cleanupJob(jobId) {
@@ -154,7 +154,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
154
154
  }
155
155
  else {
156
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 });
157
+ await this.scheduleJob({ ...payload, cron: undefined, at, maxRetry: (job.maxRetry || 0) - 1 });
158
158
  }
159
159
  }
160
160
  async cleanupJob(jobId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.28.3",
3
+ "version": "3.28.5",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",