@clairejs/server 3.22.9 → 3.22.11
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
|
@@ -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,
|
|
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"
|
|
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.
|
|
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.
|
|
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
|
-
|
|
143
|
-
|
|
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,
|
|
189
|
+
AbstractDbAdapter, Function, String, String, Object])
|
|
188
190
|
], AwsJobScheduler);
|
|
189
191
|
export { AwsJobScheduler };
|
|
@@ -12,6 +12,7 @@ export declare class LambdaWrapper {
|
|
|
12
12
|
protected readonly serverFactory: () => Promise<ClaireServer>;
|
|
13
13
|
protected readonly requestMapper: (event: any) => RequestOptions | undefined;
|
|
14
14
|
protected readonly config?: LambdaWrapperConfig | undefined;
|
|
15
|
+
private logger?;
|
|
15
16
|
private httpRequestHandler?;
|
|
16
17
|
private socketManager?;
|
|
17
18
|
private jobScheduler?;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getServiceProvider, HttpMethod, SocketMethod, Errors } from "@clairejs/core";
|
|
1
|
+
import { getServiceProvider, HttpMethod, SocketMethod, Errors, AbstractLogger } from "@clairejs/core";
|
|
2
2
|
import { AbstractHttpRequestHandler } from "../http/controller/AbstractHttpRequestHandler";
|
|
3
3
|
import { AbstractServerSocketManager } from "../socket/AbstractServerSocketManager";
|
|
4
4
|
import { CRON_REQUEST_METHOD } from "../job/interfaces";
|
|
@@ -66,6 +66,7 @@ export class LambdaWrapper {
|
|
|
66
66
|
serverFactory;
|
|
67
67
|
requestMapper;
|
|
68
68
|
config;
|
|
69
|
+
logger;
|
|
69
70
|
httpRequestHandler;
|
|
70
71
|
socketManager;
|
|
71
72
|
jobScheduler;
|
|
@@ -80,30 +81,34 @@ export class LambdaWrapper {
|
|
|
80
81
|
if (!this._server) {
|
|
81
82
|
this._server = await this.serverFactory();
|
|
82
83
|
const injector = getServiceProvider().getInjector();
|
|
84
|
+
this.logger = injector.resolveOptional(AbstractLogger);
|
|
83
85
|
this.socketManager = injector.resolveOptional(AbstractServerSocketManager);
|
|
84
86
|
this.httpRequestHandler = injector.resolveOptional(AbstractHttpRequestHandler);
|
|
85
87
|
this.jobScheduler = injector.resolveOptional(AbstractJobScheduler);
|
|
86
88
|
await injector.initInstances();
|
|
87
89
|
this.socketManager?.configure();
|
|
88
90
|
await this._server.init();
|
|
91
|
+
this.logger?.info("Server init finish");
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.logger?.info("Server already booted, reusing");
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
async handler(event) {
|
|
92
|
-
await this.bootServer();
|
|
93
|
-
//-- handle http otherwise
|
|
94
98
|
const requestOptions = this.requestMapper(event);
|
|
95
|
-
|
|
96
|
-
throw Errors.SYSTEM_ERROR("Cannot resolve event");
|
|
97
|
-
}
|
|
98
|
-
const corsHeaders = convertCorsToHeaders(requestOptions.headers?.origin || "", this.httpRequestHandler?.corsConfig);
|
|
99
|
-
if (requestOptions.method === HttpMethod.OPTIONS) {
|
|
100
|
-
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
101
|
-
}
|
|
102
|
-
if (requestOptions.method === CRON_REQUEST_METHOD) {
|
|
103
|
-
await this.jobScheduler?.handleCron(requestOptions.body);
|
|
104
|
-
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
105
|
-
}
|
|
99
|
+
const corsHeaders = convertCorsToHeaders(requestOptions?.headers?.origin || "", this.httpRequestHandler?.corsConfig);
|
|
106
100
|
try {
|
|
101
|
+
await this.bootServer();
|
|
102
|
+
if (!requestOptions) {
|
|
103
|
+
throw Errors.SYSTEM_ERROR("Cannot resolve event");
|
|
104
|
+
}
|
|
105
|
+
if (requestOptions.method === HttpMethod.OPTIONS) {
|
|
106
|
+
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
107
|
+
}
|
|
108
|
+
if (requestOptions.method === CRON_REQUEST_METHOD) {
|
|
109
|
+
await this.jobScheduler?.handleCron(requestOptions.body);
|
|
110
|
+
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
111
|
+
}
|
|
107
112
|
if (Object.values(HttpMethod).includes(requestOptions.method)) {
|
|
108
113
|
if (!this.httpRequestHandler) {
|
|
109
114
|
throw Errors.SYSTEM_ERROR("Http request handler not found");
|
|
@@ -136,6 +141,7 @@ export class LambdaWrapper {
|
|
|
136
141
|
return toApiGatewayFormat({ code: 200, headers: corsHeaders, cookies: {} });
|
|
137
142
|
}
|
|
138
143
|
catch (err) {
|
|
144
|
+
this.logger?.error(err);
|
|
139
145
|
return toApiGatewayFormat({
|
|
140
146
|
code: typeof err.code === "number" ? err.code : 500,
|
|
141
147
|
value: { name: err.name, message: err.message, code: err.code || 500 },
|