@clairejs/server 3.23.0 → 3.23.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,7 +1,9 @@
1
1
  ## Change Log
2
2
 
3
- #### 3.23.0
3
+ #### 3.23.2
4
4
 
5
+ - fix fallback jobId by id in AbstractJob
6
+ - fix retryDelayMs
5
7
  - improve JobScheduler
6
8
 
7
9
  #### 3.22.15
@@ -19,5 +19,5 @@ export declare abstract class AbstractJobRepository<T extends AbstractJob = Abst
19
19
  * Remove job info by id
20
20
  * @param jobId Unique id of job
21
21
  */
22
- abstract updateJobById(jobId: string, update: Partial<Omit<T, "jobId">>, tx?: ITransaction): Promise<void>;
22
+ abstract updateJobById(jobId: string, update: Partial<Omit<T, "id" | "jobId">>, tx?: ITransaction): Promise<void>;
23
23
  }
@@ -66,7 +66,7 @@ export class AbstractJobScheduler {
66
66
  const tx = await this.db.createTransaction();
67
67
  if (!jobHandler) {
68
68
  this.logger.info(`Disable job with id: ${job.jobId} as handler is not found`);
69
- await this.disableJob(job.jobId, tx);
69
+ await this.disableJob(job.jobId || job.id, tx);
70
70
  }
71
71
  else {
72
72
  const update = {
@@ -104,12 +104,12 @@ export class AbstractJobScheduler {
104
104
  }
105
105
  else {
106
106
  update.disabled = true;
107
- await this.cancelJob(job.jobId);
107
+ await this.cancelJob(job.jobId || job.id);
108
108
  }
109
109
  }
110
110
  }
111
111
  }
112
- await this.jobRepo.updateJobById(job.jobId, update, tx);
112
+ await this.jobRepo.updateJobById(job.jobId || job.id, update, tx);
113
113
  }
114
114
  await tx.commit();
115
115
  }
@@ -133,7 +133,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
133
133
  const nomoreExistJobs = scheduledJobs.filter((job) => !allHandlers.find((handler) => handler.jobName === job.jobName));
134
134
  for (const job of nomoreExistJobs) {
135
135
  this.logger.info(`Removing stale job: ${job.jobName} of id: ${job.jobId}`);
136
- await this.disableJob(job.jobId);
136
+ await this.disableJob(job.jobId || job.id);
137
137
  }
138
138
  if (nomoreExistJobs.length) {
139
139
  this.logger.info(`Cleaned up: ${nomoreExistJobs.length} stale jobs`);
@@ -143,7 +143,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
143
143
  const scheduledCronJobs = scheduledJobs.filter((j) => j.cron);
144
144
  const unmatchedCronJobs = scheduledCronJobs.filter((j) => j.cron && !allHandlers.find((job) => job.jobName === j.jobName && job.cron === j.cron));
145
145
  if (unmatchedCronJobs.length) {
146
- await Promise.all(unmatchedCronJobs.map((j) => this.disableJob(j.jobId)));
146
+ await Promise.all(unmatchedCronJobs.map((j) => this.disableJob(j.jobId || j.id)));
147
147
  }
148
148
  //-- reschedule new cron jobs and those which are not synced
149
149
  const resyncCronJobs = allHandlers.filter((job) => job.cron &&
@@ -148,7 +148,12 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
148
148
  const allJobs = await this.getCurrentJobHandlers();
149
149
  for (const job of allJobs) {
150
150
  if (job.cron) {
151
- await this.scheduleJob(job);
151
+ await this.scheduleJob({
152
+ ...job.retryOptions,
153
+ jobName: job.jobName,
154
+ cron: job.cron,
155
+ jobId: job.jobName,
156
+ });
152
157
  }
153
158
  }
154
159
  //-- re-schedule "at" jobs that are stored in repo
@@ -171,13 +176,13 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
171
176
  const timeout = setTimeout(() => {
172
177
  this.executeJob(jobInfo).catch((err) => this.logger.error(`Error execute job ${jobInfo.jobName} with id: ${jobInfo.jobId}`, err));
173
178
  }, new Date(jobInfo.at).getTime() - Date.now());
174
- this.jobHolder[jobInfo.jobId] = { jobCanceler: () => clearTimeout(timeout), jobInfo };
179
+ this.jobHolder[jobInfo.jobId || jobInfo.id] = { jobCanceler: () => clearTimeout(timeout), jobInfo };
175
180
  }
176
181
  else if (jobInfo.cron) {
177
182
  const job = scheduler.scheduleJob(jobInfo.cron, () => {
178
183
  this.executeJob(jobInfo).catch((err) => this.logger.error(`Error execute job ${jobInfo.jobName} with id: ${jobInfo.jobId}`, err));
179
184
  });
180
- this.jobHolder[jobInfo.jobId] = { jobCanceler: () => job.cancel(), jobInfo };
185
+ this.jobHolder[jobInfo.jobId || jobInfo.id] = { jobCanceler: () => job.cancel(), jobInfo };
181
186
  }
182
187
  else {
183
188
  throw Errors.SYSTEM_ERROR(`Job does not have time config: ${jobInfo.jobName}`);
@@ -7,7 +7,7 @@ export declare class DefaultJobRepository<T extends AbstractJob & AbstractModel>
7
7
  protected db: AbstractDbAdapter;
8
8
  constructor(model: Constructor<T>, db: AbstractDbAdapter);
9
9
  getJobs(query?: QueryCondition<T>, options?: GetManyOptions<T, keyof T>): Promise<T[]>;
10
- insertJob({ jobId, ...jobInfo }: Partial<T>, tx?: ITransaction): Promise<string>;
10
+ insertJob(jobInfo: Partial<T>, tx?: ITransaction): Promise<string>;
11
11
  removeJobById(jobId: string, tx?: ITransaction): Promise<void>;
12
- updateJobById(jobId: string, update: Partial<Omit<T, "jobId">>, tx?: ITransaction): Promise<void>;
12
+ updateJobById(jobId: string, update: Partial<Omit<T, "id" | "jobId">>, tx?: ITransaction): Promise<void>;
13
13
  }
@@ -10,22 +10,18 @@ export class DefaultJobRepository extends AbstractJobRepository {
10
10
  async getJobs(query, options) {
11
11
  return await this.db.use(this.model).getRecords(query, options);
12
12
  }
13
- async insertJob({ jobId, ...jobInfo }, tx) {
14
- if (jobId) {
15
- //-- update
16
- await this.db.use(this.model, tx).updateOne({ _eq: { jobId } }, jobInfo);
17
- return jobId;
18
- }
19
- else {
20
- //-- create new
21
- const job = await this.db.use(this.model, tx).createOne(jobInfo);
22
- return job.jobId;
23
- }
13
+ async insertJob(jobInfo, tx) {
14
+ const model = new this.model();
15
+ Object.assign(model, jobInfo);
16
+ const job = await this.db.use(this.model, tx).createOne(model);
17
+ return job.jobId || job.id;
24
18
  }
25
19
  async removeJobById(jobId, tx) {
26
- await this.db.use(this.model, tx).deleteMany({ _eq: { jobId } });
20
+ await this.db.use(this.model, tx).deleteMany({ _or: [{ _eq: { jobId } }, { _eq: { id: jobId } }] });
27
21
  }
28
22
  async updateJobById(jobId, update, tx) {
29
- await this.db.use(this.model, tx).updateOne({ _eq: { jobId } }, update);
23
+ await this.db
24
+ .use(this.model, tx)
25
+ .updateOne({ _or: [{ _eq: { jobId } }, { _eq: { id: jobId } }] }, update);
30
26
  }
31
27
  }
@@ -38,7 +38,7 @@ export interface JobSchedulePayload extends JobRetryOptions {
38
38
  cron?: string;
39
39
  }
40
40
  export interface AbstractJob extends JobSchedulePayload {
41
- jobId: string;
41
+ id: string;
42
42
  disabled?: boolean;
43
43
  execCount?: number;
44
44
  failCount?: number;
package/dist/job/job.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AbstractModel } from "@clairejs/core";
2
2
  import { AbstractJob } from "./interfaces";
3
3
  export declare abstract class AbstractJobModel extends AbstractModel implements AbstractJob {
4
- jobId: string;
4
+ jobId?: string;
5
5
  jobName: string;
6
6
  at?: number;
7
7
  cron?: string;
@@ -9,7 +9,7 @@ export declare abstract class AbstractJobModel extends AbstractModel implements
9
9
  retryOnFail?: boolean;
10
10
  maxRetry?: number;
11
11
  retryCount?: number;
12
- retryDelay?: number;
12
+ retryDelayMs?: number;
13
13
  disabled?: boolean;
14
14
  execCount?: number;
15
15
  failCount?: number;
package/dist/job/job.js CHANGED
@@ -17,7 +17,7 @@ export class AbstractJobModel extends AbstractModel {
17
17
  retryOnFail;
18
18
  maxRetry;
19
19
  retryCount;
20
- retryDelay;
20
+ retryDelayMs;
21
21
  disabled;
22
22
  execCount;
23
23
  failCount;
@@ -28,7 +28,6 @@ export class AbstractJobModel extends AbstractModel {
28
28
  __decorate([
29
29
  Column({
30
30
  description: "Unique id of the job",
31
- isRequired: true,
32
31
  }),
33
32
  __metadata("design:type", String)
34
33
  ], AbstractJobModel.prototype, "jobId", void 0);
@@ -73,7 +72,7 @@ __decorate([
73
72
  __decorate([
74
73
  Column({}),
75
74
  __metadata("design:type", Number)
76
- ], AbstractJobModel.prototype, "retryDelay", void 0);
75
+ ], AbstractJobModel.prototype, "retryDelayMs", void 0);
77
76
  __decorate([
78
77
  Column({}),
79
78
  __metadata("design:type", Boolean)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.23.0",
3
+ "version": "3.23.2",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",