Skip to content

Commit 93f3174

Browse files
author
Gerald Baulig
committed
fix(types): resolve all implicit any in service
1 parent 5647bed commit 93f3174

File tree

2 files changed

+58
-61
lines changed

2 files changed

+58
-61
lines changed

src/schedulingService.ts

+52-55
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import * as _ from 'lodash-es';
22
import { errors } from '@restorecommerce/chassis-srv';
33
import * as kafkaClient from '@restorecommerce/kafka-client';
4-
import { AuthZAction, ACSAuthZ, updateConfig, DecisionResponse, Operation, PolicySetRQResponse, ACSResource, CtxResource } from '@restorecommerce/acs-client';
4+
import {
5+
AuthZAction,
6+
ACSAuthZ,
7+
updateConfig,
8+
DecisionResponse,
9+
Operation,
10+
PolicySetRQResponse,
11+
CtxResource,
12+
CustomQueryArgs
13+
} from '@restorecommerce/acs-client';
514
import {
615
JobServiceImplementation as SchedulingServiceServiceImplementation,
7-
JobFailed, JobDone, DeepPartial, JobList, JobListResponse, Data,
16+
JobFailed, JobDone, DeepPartial, JobList, JobListResponse,
817
Backoff_Type, JobOptions_Priority, JobReadRequest, JobReadRequest_SortOrder,
9-
JobResponse
18+
JobResponse, Job,
1019
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/job.js';
1120
import { createClient, RedisClientType } from 'redis';
1221
import { NewJob, Priority } from './types.js';
@@ -17,9 +26,15 @@ import * as uuid from 'uuid';
1726
import { Logger } from 'winston';
1827
import { Response_Decision } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/access_control.js';
1928
import { Attribute } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/attribute.js';
20-
import { DeleteRequest, DeleteResponse, ResourceListResponse } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
21-
import { Queue, QueueOptions, Job } from 'bullmq';
29+
import {
30+
DeleteRequest,
31+
DeleteResponse,
32+
ResourceListResponse
33+
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base.js';
34+
import { Queue, QueueOptions, Job as BullJob } from 'bullmq';
2235
import { Status } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/status.js';
36+
import { Subject } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/auth.js';
37+
import { Meta } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/meta.js';
2338

2439
const { parseExpression } = pkg;
2540
const JOB_DONE_EVENT = 'jobDone';
@@ -34,34 +49,26 @@ const QUEUE_CLEANUP = 'queueCleanup';
3449
*/
3550
export class SchedulingService implements SchedulingServiceServiceImplementation {
3651

37-
jobEvents: kafkaClient.Topic;
38-
logger: Logger;
39-
4052
queuesConfigList: any;
4153
queuesList: Queue[];
4254
defaultQueueName: string;
43-
44-
redisClient: RedisClientType<any, any>;
4555
resourceEventsEnabled: boolean;
4656
canceledJobs: Set<string>;
47-
bullOptions: any;
48-
cfg: any;
49-
authZ: ACSAuthZ;
5057
authZCheck: boolean;
5158
repeatJobIdRedisClient: RedisClientType<any, any>;
5259

53-
5460
constructor(
55-
jobEvents: kafkaClient.Topic,
56-
private redisConfig: any, logger: any, redisClient: RedisClientType<any, any>,
57-
bullOptions: any, cfg: any, authZ: ACSAuthZ) {
58-
this.jobEvents = jobEvents;
61+
private readonly jobEvents: kafkaClient.Topic,
62+
private readonly redisConfig: any,
63+
private readonly logger: Logger,
64+
private readonly redisClient: RedisClientType<any, any>,
65+
private readonly bullOptions: any,
66+
private readonly cfg: any,
67+
private readonly authZ: ACSAuthZ
68+
) {
5969
this.resourceEventsEnabled = true;
60-
this.bullOptions = bullOptions;
61-
this.logger = logger;
6270
this.queuesList = [];
6371
this.queuesConfigList = [];
64-
this.redisClient = redisClient;
6572

6673
const repeatJobIdCfg = cfg.get('redis');
6774
repeatJobIdCfg.database = cfg.get('redis:db-indexes:db-repeatJobId');
@@ -74,7 +81,6 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
7481
}).catch(err => logger.error('Redis client error for repeatable job store', { err }));
7582

7683
this.canceledJobs = new Set<string>();
77-
this.cfg = cfg;
7884
this.authZ = authZ;
7985
this.authZCheck = this.cfg.get('authorization:enabled');
8086

@@ -236,7 +242,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
236242
// for jobs created via Kafka currently there are no acs checks
237243
this.disableAC();
238244
const createDispatch = [];
239-
let result: Job[] = [];
245+
let result: BullJob[] = [];
240246
const logger = this.logger;
241247
const create = this.create;
242248
// Get the jobs
@@ -379,7 +385,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
379385
* @param millis
380386
* @param opts
381387
*/
382-
getNextMillis(millis, opts) {
388+
getNextMillis(millis: number, opts: any) {
383389
if (opts?.every) {
384390
return Math.floor(millis / opts.every) * opts.every + opts.every;
385391
}
@@ -405,7 +411,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
405411
}
406412
}
407413

408-
private md5(str) {
414+
private md5(str: string) {
409415
return crypto
410416
.createHash('md5')
411417
.update(str)
@@ -419,7 +425,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
419425
* @param repeat - job repeate options
420426
* @param jobId - job id
421427
*/
422-
async storeRepeatKey(repeatId, scsJobId, options) {
428+
async storeRepeatKey(repeatId: string, scsJobId: string, options: any) {
423429
try {
424430
if (repeatId && scsJobId) {
425431
this.logger.info('Repeat key mapped to external SCS JobId', { repeatId, scsJobId });
@@ -492,7 +498,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
492498
}
493499
}
494500

495-
const result: Job[] = [];
501+
const result: BullJob[] = [];
496502
// Scheduling jobs
497503
for (let i = 0; i < jobs.length; i += 1) {
498504
const job = jobs[i];
@@ -536,7 +542,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
536542

537543
if (!job.data.meta) {
538544
const now = new Date();
539-
const metaObj = {
545+
const metaObj: Meta = {
540546
created: now,
541547
modified: now,
542548
modified_by: '',
@@ -573,7 +579,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
573579
};
574580

575581
if ((bullOptions as any).timeout === 1) {
576-
delete bullOptions['timeout'];
582+
delete (bullOptions as any).timeout;
577583
}
578584

579585
// Match the Job Type with the Queue Name and add the Job to this Queue.
@@ -638,10 +644,10 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
638644
return jobListResponse;
639645
}
640646

641-
private filterByOwnerShip(customArgsObj, result) {
647+
private filterByOwnerShip(customArgsObj: CustomQueryArgs, result: BullJob[]): BullJob[] {
642648
// applying filter based on custom arguments (filterByOwnerShip)
643-
let filteredResult: Job[] = [];
644-
const customArgs = (customArgsObj)?.custom_arguments;
649+
const filteredResult = new Array<BullJob>();
650+
const customArgs = customArgsObj?.custom_arguments;
645651
if (customArgs?.value) {
646652
let customArgsFilter;
647653
try {
@@ -675,8 +681,8 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
675681
}
676682
}
677683
}
678-
});
679-
filteredResult = filteredResult.concat(filteredResp);
684+
}) as BullJob[];
685+
filteredResult.push(...filteredResp);
680686
}
681687
return filteredResult;
682688
} else {
@@ -738,24 +744,18 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
738744
return { operation_status: acsResponse.operation_status };
739745
}
740746

741-
let result = new Array<Job>();
747+
let result: Array<BullJob>;
742748
if (_.isEmpty(request) || _.isEmpty(request.filter)
743749
&& (!request.filter || !request.filter.job_ids
744750
|| _.isEmpty(request.filter.job_ids))
745751
&& (!request.filter || !request.filter.type ||
746752
_.isEmpty(request.filter.type))) {
747753
result = await this._getJobList();
748-
let custom_arguments;
749-
if (acsResponse?.custom_query_args?.length > 0) {
750-
custom_arguments = acsResponse.custom_query_args[0].custom_arguments;
751-
}
752-
result = this.filterByOwnerShip({ custom_arguments }, result);
754+
const custom_arguments = acsResponse.custom_query_args?.[0];
755+
result = this.filterByOwnerShip(custom_arguments, result);
753756
} else {
754-
const logger = this.logger;
755-
let jobIDs = request.filter.job_ids || [];
756-
if (!_.isArray(jobIDs)) {
757-
jobIDs = [jobIDs];
758-
}
757+
result = new Array<BullJob>();
758+
const jobIDs = Array.isArray(request.filter.job_ids) ? request.filter.job_ids : [request.filter.job_ids];
759759
const typeFilterName = request.filter.type;
760760

761761
// Search in all the queues and retrieve jobs after JobID
@@ -846,11 +846,8 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
846846
if (typeFilterName) {
847847
result = result.filter(job => job.name === typeFilterName);
848848
}
849-
let custom_arguments;
850-
if (acsResponse?.custom_query_args?.length > 0) {
851-
custom_arguments = acsResponse.custom_query_args[0].custom_arguments;
852-
}
853-
result = this.filterByOwnerShip({ custom_arguments }, result);
849+
const custom_arguments = acsResponse.custom_query_args?.[0]?.custom_arguments;
850+
result = this.filterByOwnerShip(custom_arguments, result);
854851
}
855852

856853
result = result.filter(valid => !!valid);
@@ -910,7 +907,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
910907
return jobListResponse;
911908
}
912909

913-
async _getJobList(): Promise<Job[]> {
910+
async _getJobList(): Promise<BullJob[]> {
914911
let jobsList: any[] = [];
915912
for (const queue of this.queuesList) {
916913
const getJobsResult = await queue.getJobs(this.bullOptions['activeAndFutureJobTypes']);
@@ -1165,7 +1162,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
11651162
const mappedJobs = request?.items?.reduce((obj, job) => {
11661163
obj[job.id] = job;
11671164
return obj;
1168-
}, {});
1165+
}, {} as Record<string, Job>);
11691166

11701167
const jobData = await this.read(JobReadRequest.fromPartial(
11711168
{
@@ -1181,7 +1178,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
11811178
subject
11821179
}), {});
11831180

1184-
const result = [];
1181+
const result = new Array<Job>();
11851182

11861183
jobData?.items?.forEach(async (job) => {
11871184
const mappedJob = mappedJobs[job?.payload?.id];
@@ -1198,7 +1195,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
11981195
};
11991196

12001197
if (endJob.when && endJob.options) {
1201-
delete endJob.options['delay'];
1198+
delete (endJob.options as any).delay;
12021199
}
12031200

12041201
result.push(endJob);
@@ -1363,7 +1360,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
13631360
* @param action resource action
13641361
* @param subject subject name
13651362
*/
1366-
async createMetadata(resources: any, action: string, subject): Promise<any> {
1363+
async createMetadata(resources: any, action: string, subject: Subject): Promise<any> {
13671364
const orgOwnerAttributes: Attribute[] = [];
13681365
if (resources && !_.isArray(resources)) {
13691366
resources = [resources];

src/worker.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -347,27 +347,27 @@ export class Worker {
347347
}
348348
}
349349
if (externalJobFiles?.length > 0) {
350-
externalJobFiles.forEach(async (externalFile) => {
350+
await Promise.allSettled(externalJobFiles.map(async (externalFile) => {
351351
if (externalFile.endsWith('.js') || externalFile.endsWith('.cjs')) {
352-
const import_path = [
352+
const importPath = [
353353
process.env.EXTERNAL_JOBS_REQUIRE_DIR ?? cfg.get('externalJobs:importPath') ?? './external-jobs/',
354354
externalFile
355355
].join('').replace(/\.cjs$/, '.js');
356356
try {
357-
const fileImport = await import(import_path);
358-
this.logger?.info('imported:', fileImport);
357+
const fileImport = await import(importPath);
359358
// check for double default
360359
if (fileImport?.default?.default) {
361360
await fileImport.default.default(cfg, logger, events, runWorker);
362361
} else {
363362
await fileImport.default(cfg, logger, events, runWorker);
364363
}
364+
this.logger?.info('imported:', importPath);
365365
}
366366
catch (err: any) {
367-
this.logger?.error(`Error scheduling external job ${import_path}`, { err });
367+
this.logger?.error(`Error scheduling external job ${importPath}`, { err });
368368
}
369369
}
370-
});
370+
}));
371371
}
372372

373373
// Start server

0 commit comments

Comments
 (0)