Skip to content

Commit 7ab2aa9

Browse files
committed
Add runner
1 parent a9bf1eb commit 7ab2aa9

File tree

7 files changed

+84
-34
lines changed

7 files changed

+84
-34
lines changed

README.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,72 @@
11
# [Graphile Worker](https://github.com/graphile/worker) for [Nest](https://github.com/nestjs/nest)
22

3+
This is wrapper for Nest.js and [Graphile Worker](https://github.com/graphile/worker).
4+
5+
What is Graphile worker ?
6+
37
> Job queue for PostgreSQL running on Node.js - allows you to run jobs (e.g. sending emails, performing calculations, generating PDFs, etc) "in the background" so that your HTTP response/application code is not held up. Can be used with any PostgreSQL-backed application. Pairs beautifully with [PostGraphile](https://www.graphile.org/postgraphile/) or [PostgREST](http://postgrest.org/).
48
5-
## Description
9+
Why you should prefer Graphile Worker instead of [Bull](https://github.com/nestjs/bull) ?
610

7-
framework TypeScript starter repository.
11+
1. You already have a PostgreSQL in your stack (and you don't want to add a Redis server)
812

913
## Installation
1014

1115
```bash
12-
$ npm install
16+
$ npm install nest-graphile-worker
1317
```
1418

15-
## Running the app
19+
Then import module
20+
21+
```ts
22+
// src/app.module.ts
23+
import { GraphileWorkerModule } from 'nest-graphile-worker';
24+
import { Module } from '@nestjs/common';
25+
import { AppController } from './app.controller';
26+
27+
@Module({
28+
imports: [
29+
GraphileWorkerModule.forRoot({
30+
connectionString: 'postgres://example:password@postgres/example',
31+
}),
32+
],
33+
controllers: [AppController],
34+
providers: [],
35+
})
36+
export class AppModule {}
37+
```
1638

17-
```bash
18-
# development
19-
$ npm run start
39+
Now you can add jobs using `GraphileWorkerService`
40+
41+
```ts
42+
import { GraphileWorkerService } from 'graphile-worker';
43+
import { Controller, HttpCode, Post } from '@nestjs/common';
44+
45+
@Controller()
46+
export class AppController {
47+
constructor(private readonly graphileWorker: GraphileWorkerService) {}
48+
49+
@Post()
50+
@HttpCode(201)
51+
async addJob() {
52+
await this.graphileWorker.quickAddJob('test', { hello: 'world' });
53+
}
54+
}
55+
```
56+
57+
Also you can run worker in bacground in `main.ts` file:
2058

21-
# watch mode
22-
$ npm run start:dev
59+
```ts
60+
import { GraphileWorkerService } from '@app/graphile-worker';
61+
import { NestFactory } from '@nestjs/core';
62+
import { AppModule } from './app.module';
2363

24-
# production mode
25-
$ npm run start:prod
64+
async function bootstrap() {
65+
const app = await NestFactory.create(AppModule);
66+
app.get(GraphileWorkerService).run();
67+
await app.listen(3000);
68+
}
69+
bootstrap();
2670
```
2771

2872
## Test
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DynamicModule, Module, Provider } from '@nestjs/common';
2-
import { WorkerUtilsOptions } from 'graphile-worker';
2+
import { RunnerOptions } from 'graphile-worker';
33
import { GraphileWorkerService } from './graphile-worker.service';
44

55
export const GRAPHILE_WORKER_TOKEN = Symbol.for('NestJsGraphileWorker');
@@ -9,21 +9,7 @@ export const GRAPHILE_WORKER_TOKEN = Symbol.for('NestJsGraphileWorker');
99
exports: [GraphileWorkerService],
1010
})
1111
export class GraphileWorkerModule {
12-
static forRoot(options: WorkerUtilsOptions): DynamicModule;
13-
static forRoot(connectionString: string): DynamicModule;
14-
static forRoot(connectionStringOrOptions: unknown): DynamicModule {
15-
let options: WorkerUtilsOptions = {};
16-
17-
if (typeof connectionStringOrOptions === 'string') {
18-
options.connectionString = connectionStringOrOptions;
19-
} else if (isWorkerUtilsOptions(connectionStringOrOptions)) {
20-
options = connectionStringOrOptions;
21-
} else {
22-
throw Error(
23-
'Cannot detect type of option provided for `GraphileWorkerModule.forRoot()`',
24-
);
25-
}
26-
12+
static forRoot(options: RunnerOptions): DynamicModule {
2713
const graphileWorkerService: Provider = {
2814
provide: GraphileWorkerService,
2915
useValue: new GraphileWorkerService(options),
@@ -37,7 +23,3 @@ export class GraphileWorkerModule {
3723
};
3824
}
3925
}
40-
41-
function isWorkerUtilsOptions(options: unknown): options is WorkerUtilsOptions {
42-
return (options as WorkerUtilsOptions).connectionString !== undefined;
43-
}

libs/graphile-worker/src/graphile-worker.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ import { Injectable, Logger } from '@nestjs/common';
22
import {
33
Job,
44
quickAddJob,
5+
run,
56
runMigrations,
7+
RunnerOptions,
68
TaskSpec,
7-
WorkerUtilsOptions,
89
} from 'graphile-worker';
910

1011
@Injectable()
1112
export class GraphileWorkerService {
1213
private readonly logger = new Logger(GraphileWorkerService.name);
1314
private isMigrationDone: boolean;
1415

15-
constructor(private readonly options: WorkerUtilsOptions) {}
16+
constructor(private readonly options: RunnerOptions) {}
17+
18+
/**
19+
* Run a new worker
20+
*/
21+
async run() {
22+
await this.runMigrations();
23+
24+
this.logger.debug('Start runner');
25+
26+
const runner = await run(this.options);
27+
return runner.promise;
28+
}
1629

1730
async quickAddJob(
1831
identifier: string,

src/app.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export class AppController {
88
@Post()
99
@HttpCode(201)
1010
async addJob() {
11-
await this.graphileWorker.quickAddJob('test', { hello: 'world' });
11+
await this.graphileWorker.quickAddJob('hello', { hello: 'world' });
1212
}
1313
}

src/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { GraphileWorkerModule } from '@app/graphile-worker';
22
import { Module } from '@nestjs/common';
33
import { AppController } from './app.controller';
4+
import { helloTask } from './hello.task';
45

56
@Module({
67
imports: [
78
GraphileWorkerModule.forRoot({
89
connectionString: 'postgres://example:password@postgres/example',
10+
taskList: {
11+
hello: helloTask,
12+
},
913
}),
1014
],
1115
controllers: [AppController],

src/hello.task.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Helpers } from 'graphile-worker';
2+
3+
export function helloTask(payload: any, helpers: Helpers) {
4+
console.log(`hello task %o`, payload);
5+
}

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { GraphileWorkerService } from '@app/graphile-worker';
12
import { NestFactory } from '@nestjs/core';
23
import { AppModule } from './app.module';
34

45
async function bootstrap() {
56
const app = await NestFactory.create(AppModule);
7+
app.get(GraphileWorkerService).run();
68
await app.listen(3000);
79
}
810
bootstrap();

0 commit comments

Comments
 (0)