@clairejs/server 3.22.8 → 3.22.10

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,5 +1,14 @@
1
1
  ## Change Log
2
2
 
3
+ #### 3.22.10
4
+
5
+ - fix aws job scheduler name space job removal issue
6
+
7
+ #### 3.22.9
8
+
9
+ - fix typing issue with job handler function
10
+ - fix providing transaction to job remove function
11
+
3
12
  #### 3.22.8
4
13
 
5
14
  - remove log, aws job scheduler issue fixed
@@ -4,4 +4,4 @@ import { AbstractHttpController } from "../http/controller/AbstractHttpControlle
4
4
  export declare const Controller: (config?: {
5
5
  mount?: string;
6
6
  permissionGroup?: string;
7
- }) => <T extends AbstractSocketController | AbstractHttpController>(constructor: Constructor<T>) => void;
7
+ }) => <T extends AbstractHttpController | AbstractSocketController>(constructor: Constructor<T>) => void;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export interface CookieOptions {
3
2
  maxAge?: number | undefined;
4
3
  expires?: Date | undefined;
@@ -12,20 +12,20 @@ export interface MappingMetadata<R extends Identifiable, K extends AbstractModel
12
12
  rootMapping: (ks?: DeepPartial<K>[]) => DeepPartial<R>;
13
13
  nestedMapping?: DtoDissolver<R, K>;
14
14
  }
15
- export declare const SingleMap: <R extends Identifiable, K extends AbstractModel>(modelClass: Constructor<K>, forwardOps: (ops?: QueryCondition<R>[] | undefined) => QueryCondition<K>[] | undefined, forwardMapping: (t: DeepPartial<K>) => DeepPartial<K> | undefined, rootMapping: (k?: DeepPartial<K> | undefined) => DeepPartial<R>, nestedMapping?: DtoDissolver<R, K> | undefined) => {
15
+ export declare const SingleMap: <R extends Identifiable, K extends AbstractModel>(modelClass: Constructor<K>, forwardOps: (ops?: QueryCondition<R>[]) => QueryCondition<K>[] | undefined, forwardMapping: (t: DeepPartial<K>) => DeepPartial<K> | undefined, rootMapping: (k?: DeepPartial<K>) => DeepPartial<R>, nestedMapping?: DtoDissolver<R, K>) => {
16
16
  multiple: boolean;
17
17
  modelClass: Constructor<K>;
18
- forwardOps: (ops?: QueryCondition<R>[] | undefined) => QueryCondition<K>[] | undefined;
18
+ forwardOps: (ops?: QueryCondition<R>[]) => QueryCondition<K>[] | undefined;
19
19
  forwardMapping: (t: DeepPartial<K>[]) => DeepPartial<K>[] | undefined;
20
- rootMapping: (ks?: DeepPartial<K>[] | undefined) => DeepPartial<R>;
20
+ rootMapping: (ks?: DeepPartial<K>[]) => DeepPartial<R>;
21
21
  nestedMapping: DtoDissolver<R, K> | undefined;
22
22
  };
23
- export declare const MultipleMap: <R extends Identifiable, K extends AbstractModel>(modelClass: Constructor<K>, forwardOps: (ops?: QueryCondition<R>[] | undefined) => QueryCondition<K>[] | undefined, forwardMapping: (t: DeepPartial<K>[]) => DeepPartial<K>[] | undefined, rootMapping: (ks?: DeepPartial<K>[] | undefined) => DeepPartial<R>, nestedMapping?: DtoDissolver<R, K> | undefined) => {
23
+ export declare const MultipleMap: <R extends Identifiable, K extends AbstractModel>(modelClass: Constructor<K>, forwardOps: (ops?: QueryCondition<R>[]) => QueryCondition<K>[] | undefined, forwardMapping: (t: DeepPartial<K>[]) => DeepPartial<K>[] | undefined, rootMapping: (ks?: DeepPartial<K>[]) => DeepPartial<R>, nestedMapping?: DtoDissolver<R, K>) => {
24
24
  multiple: boolean;
25
25
  modelClass: Constructor<K>;
26
- forwardOps: (ops?: QueryCondition<R>[] | undefined) => QueryCondition<K>[] | undefined;
26
+ forwardOps: (ops?: QueryCondition<R>[]) => QueryCondition<K>[] | undefined;
27
27
  forwardMapping: (t: DeepPartial<K>[]) => DeepPartial<K>[] | undefined;
28
- rootMapping: (ks?: DeepPartial<K>[] | undefined) => DeepPartial<R>;
28
+ rootMapping: (ks?: DeepPartial<K>[]) => DeepPartial<R>;
29
29
  nestedMapping: DtoDissolver<R, K> | undefined;
30
30
  };
31
31
  export declare class DtoRepository<T extends Identifiable> extends AbstractRepository<T> implements ICrudRepository<T> {
@@ -27,7 +27,7 @@ export declare class ModelRepository<T extends AbstractModel> extends AbstractRe
27
27
  tx: ITransaction;
28
28
  logger?: AbstractLogger;
29
29
  }): Promise<UpdateManyResponse<T>>;
30
- getMany({ queries, ops, tx, logger, }: {
30
+ getMany({ queries, ops, tx, logger: _logger, }: {
31
31
  queries?: GetManyQueries<T>;
32
32
  ops?: QueryCondition<T>[];
33
33
  tx?: ITransaction;
@@ -1,8 +1,9 @@
1
1
  import { AbstractLogger } from "@clairejs/core";
2
2
  import { ITransaction, ITransactionFactory } from "@clairejs/orm";
3
3
  import { CustomJobInfo, JobInfo, JobInfoMetadata, ScheduledJob } from "./interfaces";
4
+ export type JobHandlerFn = (job: ScheduledJob, tx: ITransaction) => Promise<ScheduledJob | void>;
4
5
  interface JobHandlerMetadata extends JobInfoMetadata {
5
- handlerFn: (job: ScheduledJob, tx: ITransaction) => Promise<ScheduledJob | undefined>;
6
+ handlerFn: JobHandlerFn;
6
7
  }
7
8
  export declare abstract class AbstractJobScheduler {
8
9
  protected readonly logger: AbstractLogger;
@@ -31,7 +32,7 @@ export declare abstract class AbstractJobScheduler {
31
32
  * Remove the scheduled job and prevent if from running in the future
32
33
  * @param id The job id returned from scheduleJobAt function
33
34
  */
34
- abstract removeJob(id: string): Promise<void>;
35
+ abstract removeJob(id: string, tx?: ITransaction): Promise<void>;
35
36
  /**
36
37
  * Execute the scheduled job
37
38
  * @param job The schedled job info to execute
@@ -35,25 +35,26 @@ export class AbstractJobScheduler {
35
35
  //-- run job
36
36
  const allJobs = await this.getAvailableJobInfo();
37
37
  const jobHandler = allJobs.find((j) => j.jobName === job.jobName);
38
- if (!jobHandler) {
39
- //-- remove job
40
- this.logger.info(`Remove job with id: ${job.id} as handler is not found`);
41
- await this.removeJob(job.id);
42
- return;
43
- }
44
38
  const tx = await this.db.createTransaction();
45
39
  try {
46
- const newJob = await jobHandler.handlerFn({ ...job }, tx);
47
- if (job.at) {
48
- if (!newJob) {
49
- await this.removeJob(job.id);
50
- this.logger.info(`Remove one-time job ${job.jobName} at timestamp ${job.at}`);
51
- }
52
- else {
53
- await this.afterJob(newJob, tx);
40
+ if (!jobHandler) {
41
+ //-- remove job
42
+ this.logger.info(`Remove job with id: ${job.id} as handler is not found`);
43
+ await this.removeJob(job.id, tx);
44
+ }
45
+ else {
46
+ const newJob = await jobHandler.handlerFn({ ...job }, tx);
47
+ if (job.at) {
48
+ if (!newJob) {
49
+ await this.removeJob(job.id, tx);
50
+ this.logger.info(`Remove one-time job ${job.jobName} at timestamp ${job.at}`);
51
+ }
52
+ else {
53
+ await this.afterJob(newJob, tx);
54
+ }
54
55
  }
56
+ this.logger.debug("Job tx commiting");
55
57
  }
56
- this.logger.debug("Job tx commiting");
57
58
  await tx.commit();
58
59
  this.logger.debug("Job tx committed");
59
60
  }
@@ -61,6 +62,5 @@ export class AbstractJobScheduler {
61
62
  this.logger.error("Error handling job", err);
62
63
  await tx.rollback();
63
64
  }
64
- return;
65
65
  }
66
66
  }
@@ -12,10 +12,10 @@ export declare class AwsJobScheduler extends AbstractJobScheduler {
12
12
  protected readonly db: AbstractDbAdapter;
13
13
  protected readonly uniqueJobIdFactory: () => string;
14
14
  protected readonly apiLambdaFunctionArn: string;
15
+ protected readonly jobNamespace: string;
15
16
  protected readonly eventBusName: string;
16
- protected readonly jobNamePrefix: string;
17
17
  protected readonly eventbridge: aws.EventBridge;
18
- constructor(logger: AbstractLogger, db: AbstractDbAdapter, uniqueJobIdFactory: () => string, apiLambdaFunctionArn: string, eventBusName?: string, jobNamePrefix?: string);
18
+ constructor(logger: AbstractLogger, db: AbstractDbAdapter, uniqueJobIdFactory: () => string, apiLambdaFunctionArn: string, jobNamespace: string, eventBusName?: string);
19
19
  handleCron(jobInfo: ScheduledJob): Promise<void>;
20
20
  protected afterJob(_job: ScheduledJob, _tx: ITransaction): Promise<void>;
21
21
  private generateCronFromTimestamp;
@@ -21,17 +21,17 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
21
21
  db;
22
22
  uniqueJobIdFactory;
23
23
  apiLambdaFunctionArn;
24
+ jobNamespace;
24
25
  eventBusName;
25
- jobNamePrefix;
26
26
  eventbridge = new aws.EventBridge();
27
- constructor(logger, db, uniqueJobIdFactory, apiLambdaFunctionArn, eventBusName = "default", jobNamePrefix = "claire-aws-job-") {
27
+ constructor(logger, db, uniqueJobIdFactory, apiLambdaFunctionArn, jobNamespace, eventBusName = "default") {
28
28
  super(logger, db);
29
29
  this.logger = logger;
30
30
  this.db = db;
31
31
  this.uniqueJobIdFactory = uniqueJobIdFactory;
32
32
  this.apiLambdaFunctionArn = apiLambdaFunctionArn;
33
+ this.jobNamespace = jobNamespace;
33
34
  this.eventBusName = eventBusName;
34
- this.jobNamePrefix = jobNamePrefix;
35
35
  }
36
36
  async handleCron(jobInfo) {
37
37
  this.logger.debug(`Handle cron`, jobInfo);
@@ -59,7 +59,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
59
59
  const allRules = await this.eventbridge
60
60
  .listRules({
61
61
  EventBusName: this.eventBusName,
62
- NamePrefix: this.jobNamePrefix,
62
+ NamePrefix: this.jobNamespace,
63
63
  })
64
64
  .promise();
65
65
  const allJobs = await Promise.all((allRules.Rules || []).map(async (rule) => {
@@ -83,7 +83,7 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
83
83
  async scheduleJob(jobInfo) {
84
84
  this.logger.debug("Scheduling job: ", jobInfo);
85
85
  if (jobInfo.cron || jobInfo.at) {
86
- const jobId = `${this.jobNamePrefix}${this.uniqueJobIdFactory()}`;
86
+ const jobId = `${this.jobNamespace}${this.uniqueJobIdFactory()}`;
87
87
  let cronExpression;
88
88
  if (jobInfo.at) {
89
89
  cronExpression = this.generateCronFromTimestamp(jobInfo.at);
@@ -139,8 +139,10 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
139
139
  //-- remove job that no more exist
140
140
  const nomoreExistJobs = scheduledJobs.filter((job) => !allJobs.find((j) => j.jobName === job.jobName));
141
141
  for (const job of nomoreExistJobs) {
142
- this.logger.info(`Removing stale job: ${job.jobName} of id: ${job.id}`);
143
- await this.removeJob(job.id);
142
+ if (job.id) {
143
+ this.logger.info(`Removing stale job: ${job.jobName} of id: ${job.id}`);
144
+ await this.removeJob(job.id);
145
+ }
144
146
  }
145
147
  if (nomoreExistJobs.length) {
146
148
  this.logger.info(`Cleaned up: ${nomoreExistJobs.length} stale jobs`);
@@ -184,6 +186,6 @@ let AwsJobScheduler = class AwsJobScheduler extends AbstractJobScheduler {
184
186
  AwsJobScheduler = __decorate([
185
187
  LogContext(),
186
188
  __metadata("design:paramtypes", [AbstractLogger,
187
- AbstractDbAdapter, Function, String, Object, Object])
189
+ AbstractDbAdapter, Function, String, String, Object])
188
190
  ], AwsJobScheduler);
189
191
  export { AwsJobScheduler };
@@ -65,6 +65,6 @@ export declare class LocalJobScheduler extends AbstractJobScheduler implements I
65
65
  protected isActiveScheduler(): boolean;
66
66
  getAllScheduledJobs(): Promise<ScheduledJob[]>;
67
67
  syncJobs(): Promise<void>;
68
- protected scheduleJob(jobInfo: JobInfo): Promise<string>;
69
- removeJob(id: string): Promise<void>;
68
+ protected scheduleJob(jobInfo: JobInfo, tx?: ITransaction): Promise<string>;
69
+ removeJob(id: string, tx?: ITransaction): Promise<void>;
70
70
  }
@@ -195,7 +195,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
195
195
  }
196
196
  return;
197
197
  }
198
- async scheduleJob(jobInfo) {
198
+ async scheduleJob(jobInfo, tx) {
199
199
  if (this.isActive) {
200
200
  //-- case each job type
201
201
  if (jobInfo.at) {
@@ -205,7 +205,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
205
205
  jobName: jobInfo.jobName,
206
206
  params: jobInfo.params,
207
207
  at: jobInfo.at,
208
- }));
208
+ }, tx));
209
209
  //-- use the lib
210
210
  const scheduledJob = { ...jobInfo, id };
211
211
  const timeout = setTimeout(() => {
@@ -237,7 +237,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
237
237
  });
238
238
  }
239
239
  }
240
- async removeJob(id) {
240
+ async removeJob(id, tx) {
241
241
  if (this.isActive) {
242
242
  //-- remove from holder
243
243
  const job = this.jobHolder[id];
@@ -246,7 +246,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
246
246
  this.jobHolder[id] = undefined;
247
247
  }
248
248
  //-- remove from persistence
249
- await this.jobRepo.removeJobById(id);
249
+ await this.jobRepo.removeJobById(id, tx);
250
250
  }
251
251
  else {
252
252
  //-- get unique message id
@@ -1,3 +1,6 @@
1
1
  import { AbstractJobController } from "./AbstractJobController";
2
- export declare const CronJob: (jobName: string, cron: string) => <T extends AbstractJobController>(prototype: T, propertyKey: keyof T) => void;
3
- export declare const CustomJob: (jobName: string) => <T extends AbstractJobController>(prototype: T, propertyKey: keyof T) => void;
2
+ import { JobHandlerFn } from "./AbstractJobScheduler";
3
+ type Descriptor<T> = Omit<TypedPropertyDescriptor<T>, "set">;
4
+ export declare const CronJob: (jobName: string, cron: string) => <T extends AbstractJobController>(prototype: T, propertyKey: keyof T, _descriptor: Descriptor<JobHandlerFn>) => void;
5
+ export declare const CustomJob: (jobName: string) => <T extends AbstractJobController>(prototype: T, propertyKey: keyof T, _descriptor: Descriptor<JobHandlerFn>) => void;
6
+ export {};
@@ -7,7 +7,7 @@ jobName,
7
7
  /**
8
8
  * Cron expression
9
9
  */
10
- cron) => (prototype, propertyKey) => {
10
+ cron) => (prototype, propertyKey, _descriptor) => {
11
11
  const metadata = initObjectMetadata(prototype);
12
12
  if (!metadata.jobs) {
13
13
  metadata.jobs = [];
@@ -26,7 +26,7 @@ export const CustomJob = (
26
26
  /**
27
27
  * Unique name of job
28
28
  */
29
- jobName) => (prototype, propertyKey) => {
29
+ jobName) => (prototype, propertyKey, _descriptor) => {
30
30
  const metadata = initObjectMetadata(prototype);
31
31
  if (!metadata.jobs) {
32
32
  metadata.jobs = [];
@@ -11,7 +11,8 @@ var FileLogMedium_1;
11
11
  import fs from "fs";
12
12
  import path from "path";
13
13
  import { LogLevel, Initable } from "@clairejs/core";
14
- let FileLogMedium = FileLogMedium_1 = class FileLogMedium {
14
+ let FileLogMedium = class FileLogMedium {
15
+ static { FileLogMedium_1 = this; }
15
16
  static levels = Object.values(LogLevel);
16
17
  destination;
17
18
  separated;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { FileOperation } from "../common/FileOperation";
3
2
  import { GetUploadUrlResponseBody } from "../controllers/dto/upload";
4
3
  export declare abstract class AbstractFileService {
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { FileOperation } from "../../common/FileOperation";
3
2
  import { AbstractFileService } from "../AbstractFileService";
4
3
  export declare class LocalFileService extends AbstractFileService {
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { AbstractLogger } from "@clairejs/core";
3
2
  import { FileOperation } from "../../common/FileOperation";
4
3
  import { AbstractFileService } from "../AbstractFileService";
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { AbstractLogger, IInit } from "@clairejs/core";
3
2
  import WebSocket from "ws";
4
3
  import { Server } from "http";
@@ -1,2 +1,2 @@
1
1
  import { AbstractModel } from "@clairejs/core";
2
- export declare const LocaleOf: <T>(referenceColumn: keyof T) => <K extends string>(prototype: T & AbstractModel & { [k in K]?: Record<string, string> | undefined; }, propertyKey: K) => void;
2
+ export declare const LocaleOf: <T>(referenceColumn: keyof T) => <K extends string>(prototype: T & AbstractModel & { [k in K]?: Record<string, string>; }, propertyKey: K) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.22.8",
3
+ "version": "3.22.10",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
@@ -52,7 +52,7 @@
52
52
  "id-sdk": "^1.5.16",
53
53
  "mocha": "^10.2.0",
54
54
  "ts-node": "^10.9.1",
55
- "typescript": "^5.0.4",
55
+ "typescript": "^5.5.4",
56
56
  "uuid": "^9.0.0"
57
57
  }
58
58
  }