Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
perf(root): convert queues to map when initializing the base class
Browse files Browse the repository at this point in the history
  • Loading branch information
s-r-x committed Oct 6, 2021
1 parent 0b445b5 commit fd7f94f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
21 changes: 5 additions & 16 deletions packages/root/src/gql/data-sources/bull/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ export enum ErrorEnum {
BAD_LIMIT = 'Limit should be >= 1',
}
export class BullDataSource extends DataSource {
private _queuesMap: Map<string, Queue> = new Map();
constructor(private _queues: Queue[], private _config: Config) {
constructor(
private _queues: Queue[],
private _queuesMap: Map<string, Queue>,
private _config: Config
) {
super();
this._convertQueuesToMap(_queues);
}
private _convertQueuesToMap(queues: Queue[]) {
queues.forEach((queue) => {
this._queuesMap.set(queue.id as string, queue);
});
}
private _throwInternalError(e: ErrorEnum) {
throw new BullMonitorError(e);
Expand All @@ -58,14 +55,6 @@ export class BullDataSource extends DataSource {
this._throwInternalError(ErrorEnum.JOB_NOT_FOUND);
}

// queries
getQueueByName(name: string, throwIfNotFound?: boolean) {
const queue = this._queuesMap.get(name);
if (!queue && throwIfNotFound) {
this._throwQueueNotFound();
}
return queue;
}
// queries
getQueueById(id: string, throwIfNotFound?: boolean) {
const queue = this._queuesMap.get(id);
Expand Down
27 changes: 17 additions & 10 deletions packages/root/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
: false,
};
this.ui = new UI();
this._queues = this.config.queues.map(patchBullQueue);
this.initQueues(this.config.queues);
if (this.config.metrics) {
this.initMetricsCollector();
}
Expand All @@ -31,7 +31,7 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
return this._queues;
}
public set queues(queues: Config['queues']) {
this._queues = queues.map(patchBullQueue);
this.initQueues(queues);
if (this.metricsCollector && this.config.metrics) {
this.metricsCollector.stopCollecting();
this.initMetricsCollector();
Expand All @@ -49,7 +49,7 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
playground: this.config.gqlPlayground,
introspection: this.config.gqlIntrospection,
dataSources: () => ({
bull: new BullDataSource(this._queues, {
bull: new BullDataSource(this._queues, this._queuesMap, {
textSearchScanCount: this.config.textSearchScanCount,
}),
metrics: new MetricsDataSource(this.metricsCollector),
Expand Down Expand Up @@ -79,6 +79,13 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
}

private _queues: BullMonitorQueue[];
private _queuesMap: Map<string, BullMonitorQueue> = new Map();
private initQueues(queues: Config['queues']) {
this._queues = queues.map(patchBullQueue);
this._queues.forEach((queue) => {
this._queuesMap.set(queue.id as string, queue);
});
}
private ui: UI;
private metricsCollector?: MetricsCollector;
private defaultMetricsConfig: MetricsConfig = {
Expand All @@ -87,6 +94,13 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
maxMetrics: 100,
blacklist: [],
};
private initMetricsCollector() {
this.metricsCollector = new MetricsCollector(
this._queues,
this.config.metrics as Required<MetricsConfig>
);
this.metricsCollector.startCollecting();
}
private defaultConfig: Required<Config> = {
queues: [],
baseUrl: '',
Expand All @@ -95,13 +109,6 @@ export abstract class BullMonitor<TServer extends ApolloServerBase> {
textSearchScanCount: DEFAULT_TEXT_SEARCH_SCAN_COUNT,
metrics: false,
};
private initMetricsCollector() {
this.metricsCollector = new MetricsCollector(
this._queues,
this.config.metrics as Required<MetricsConfig>
);
this.metricsCollector.startCollecting();
}
}

export { Config, MetricsConfig };

0 comments on commit fd7f94f

Please sign in to comment.