Skip to content

Commit d3dd1b7

Browse files
committed
refactor(configuration): Remove ConfigurationService class
and use a plain object `RunnerOptions`
1 parent 66936b0 commit d3dd1b7

File tree

7 files changed

+75
-60
lines changed

7 files changed

+75
-60
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Changelog
2+
3+
## Current
4+
5+
- **fix(hooks)**: Set all worker hooks (was only `job:success`)
6+
- **refactor(configuration)**: Remove `ConfigurationService` class and use a plain object `RunnerOptions`
7+
- **refactor()**: Rename `GraphileWorkerService` to `WorkerService`
8+
- **doc(license)**: add license MIT
9+
10+
## v0.0.3-alpha
11+
12+
- reduce dependencies
13+
14+
## v0.0.2-alpha
15+
16+
- initial version

src/graphile-worker.module.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { DynamicModule, Module, Provider } from '@nestjs/common';
22
import { DiscoveryModule } from '@nestjs/core';
3+
import { EventEmitter } from 'events';
4+
import { RunnerOptions } from 'graphile-worker';
35
import {
46
GraphileWorkerAsyncConfiguration,
7+
GraphileWorkerConfiguration,
58
GraphileWorkerConfigurationFactory,
6-
RunnerOptionWithoutEvents,
9+
RUNNER_OPTIONS_KEY,
710
} from './interfaces/module-config.interfaces';
8-
import {
9-
ConfigurationService,
10-
CONFIGURATION_SERVICE_KEY,
11-
} from './services/configuration.service';
1211
import { ListenerExplorerService } from './services/listener-explorer.service';
1312
import { MetadataAccessorService } from './services/metadata-accessor.service';
1413
import { WorkerService } from './services/worker.service';
@@ -42,12 +41,10 @@ export class GraphileWorkerModule {
4241
* }),
4342
* ```
4443
*/
45-
static forRoot(config: RunnerOptionWithoutEvents): DynamicModule {
46-
const configurationService = new ConfigurationService(config);
47-
44+
static forRoot(config: GraphileWorkerConfiguration): DynamicModule {
4845
const graphileConfigurationServiceProvider: Provider = {
49-
provide: CONFIGURATION_SERVICE_KEY,
50-
useValue: configurationService,
46+
provide: RUNNER_OPTIONS_KEY,
47+
useValue: config,
5148
};
5249

5350
return {
@@ -102,24 +99,35 @@ export class GraphileWorkerModule {
10299
): Provider {
103100
if (options.useFactory) {
104101
return {
105-
provide: CONFIGURATION_SERVICE_KEY,
102+
provide: RUNNER_OPTIONS_KEY,
103+
inject: options.inject || [],
106104
useFactory: async (...args: any[]) => {
107105
const config = await options.useFactory(...args);
108-
return new ConfigurationService(config);
106+
return buildRunnerOptions(config);
109107
},
110-
inject: options.inject || [],
111108
};
112109
}
113110

114111
return {
115-
provide: CONFIGURATION_SERVICE_KEY,
112+
provide: RUNNER_OPTIONS_KEY,
113+
inject: options.inject,
116114
useFactory: async (
117115
optionsFactory: GraphileWorkerConfigurationFactory,
118116
) => {
119117
const config = await optionsFactory.createSharedConfiguration();
120-
return new ConfigurationService(config);
118+
return buildRunnerOptions(config);
121119
},
122-
inject: options.inject,
123120
};
124121
}
125122
}
123+
124+
function buildRunnerOptions(
125+
configuration: GraphileWorkerConfiguration,
126+
): RunnerOptions {
127+
const events = new EventEmitter();
128+
129+
return {
130+
...configuration,
131+
events,
132+
};
133+
}

src/interfaces/module-config.interfaces.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { FactoryProvider, ModuleMetadata } from '@nestjs/common';
22
import { RunnerOptions } from 'graphile-worker';
33

4-
export type RunnerOptionWithoutEvents = Omit<RunnerOptions, 'events'>;
4+
export const RUNNER_OPTIONS_KEY = Symbol.for('RUNNER_OPTIONS_KEY');
5+
6+
/**
7+
* We use `events` internally for decorators.
8+
*/
9+
export type GraphileWorkerConfiguration = Omit<RunnerOptions, 'events'>;
510

611
export interface GraphileWorkerConfigurationFactory {
712
createSharedConfiguration():
8-
| Promise<RunnerOptionWithoutEvents>
9-
| RunnerOptionWithoutEvents;
13+
| Promise<GraphileWorkerConfiguration>
14+
| GraphileWorkerConfiguration;
1015
}
1116

1217
export interface GraphileWorkerAsyncConfiguration
@@ -16,7 +21,7 @@ export interface GraphileWorkerAsyncConfiguration
1621
*/
1722
useFactory?: (
1823
...args: any[]
19-
) => Promise<RunnerOptionWithoutEvents> | RunnerOptionWithoutEvents;
24+
) => Promise<GraphileWorkerConfiguration> | GraphileWorkerConfiguration;
2025

2126
/**
2227
* Optional list of providers to be injected into the context of the Factory function.

src/services/configuration.service.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/services/worker.service.spec.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import {
3-
ConfigurationService,
4-
CONFIGURATION_SERVICE_KEY,
5-
} from './configuration.service';
2+
import { RunnerOptions } from 'graphile-worker';
3+
import { RUNNER_OPTIONS_KEY } from '../interfaces/module-config.interfaces';
64
import { ListenerExplorerService } from './listener-explorer.service';
75
import { WorkerService } from './worker.service';
86

@@ -20,14 +18,11 @@ describe(WorkerService.name, () => {
2018
},
2119
},
2220
{
23-
useFactory: () => {
24-
const configurationService = new ConfigurationService({
25-
taskList: { hello: () => {} },
26-
connectionString: 'postgres://example:password@postgres/example',
27-
});
28-
return configurationService;
29-
},
30-
provide: CONFIGURATION_SERVICE_KEY,
21+
provide: RUNNER_OPTIONS_KEY,
22+
useValue: {
23+
taskList: { hello: () => {} },
24+
connectionString: 'postgres://example:password@postgres/example',
25+
} as RunnerOptions,
3126
},
3227
],
3328
}).compile();

src/services/worker.service.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,20 @@ import {
88
TaskSpec,
99
WorkerUtils,
1010
} from 'graphile-worker';
11-
import {
12-
ConfigurationService,
13-
CONFIGURATION_SERVICE_KEY,
14-
} from './configuration.service';
11+
import { RUNNER_OPTIONS_KEY } from '../interfaces/module-config.interfaces';
12+
import { uniq } from '../utils/array.utils';
1513
import { ListenerExplorerService } from './listener-explorer.service';
1614

1715
@Injectable()
1816
export class WorkerService {
1917
private readonly logger = new Logger(WorkerService.name);
2018
private isMigrationDone: boolean;
21-
private readonly options: RunnerOptions;
2219

2320
constructor(
24-
@Inject(CONFIGURATION_SERVICE_KEY)
25-
configuration: ConfigurationService,
21+
@Inject(RUNNER_OPTIONS_KEY) private readonly options: RunnerOptions,
2622
private readonly explorerService: ListenerExplorerService,
2723
) {
28-
configuration.config.events.on('job:success', (...args: any[]) => {
29-
this.explorerService.listeners
30-
.filter(({ event }) => event === 'job:success')
31-
.forEach(({ callback }) => callback(...args));
32-
});
33-
34-
this.options = configuration.config;
24+
this.hookEvents();
3525
}
3626

3727
/**
@@ -99,4 +89,16 @@ export class WorkerService {
9989
this.logger.debug('Run migrations');
10090
this.isMigrationDone = true;
10191
}
92+
93+
private hookEvents() {
94+
const events = this.explorerService.listeners.map(({ event }) => event);
95+
96+
for (const event of uniq(events)) {
97+
this.options.events.on(event, (...args: any[]) => {
98+
this.explorerService.listeners
99+
.filter(({ event }) => event === event)
100+
.forEach(({ callback }) => callback(...args));
101+
});
102+
}
103+
}
102104
}

src/utils/array.utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function uniq<T>(a: Array<T>): Array<T> {
2+
return Array.from(new Set(a));
3+
}

0 commit comments

Comments
 (0)