Skip to content

Commit d6aaf03

Browse files
committed
doc: improve Readme
1 parent 7ed5a99 commit d6aaf03

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

README.md

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Why you should prefer Graphile Worker instead of [Bull](https://github.com/nestj
1515

1616
## Features
1717

18-
- use a `GraphileWorkerModule.forRoot` to register Graphile Worker (support `asRootAsync` as well)
18+
- provide a module `GraphileWorkerModule` to setup the runner using `asRoot` or `asRootAsync`
1919
- provide a `WorkerService` to add jobs or start runner
2020
- provide a `@OnWorkerEvent` decorator to add custom behavior on `job:success` for example
2121
- provide a `@Task(name)` decorator to define your injectable tasks
@@ -24,13 +24,17 @@ Why you should prefer Graphile Worker instead of [Bull](https://github.com/nestj
2424

2525
```sh
2626
npm install nestjs-graphile-worker
27+
yarn add nestjs-graphile-worker
28+
pnpm add nestjs-graphile-worker
2729
```
2830

2931
## Usage
3032

31-
### Import module
33+
### 1. Setup the module
3234

33-
You can use `GraphileWorkerModule.forRoot`:
35+
In order, to setup the library, you need to import and initialize [`GraphileWorkerModule`](./src/graphile-worker.module.ts).
36+
37+
You can do it using `forRoot` method:
3438

3539
```ts
3640
// src/app.module.ts
@@ -50,9 +54,10 @@ import { AppController } from "./app.controller";
5054
export class AppModule {}
5155
```
5256

53-
Or you can use `GraphileWorkerModule.forRootAsync`:
57+
Or using `forRootAsync`:
5458

5559
```ts
60+
// src/app.module.ts
5661
import { GraphileWorkerModule } from "nestjs-graphile-worker";
5762
import { Module } from "@nestjs/common";
5863
import { ConfigModule, ConfigService } from "@nestjs/config";
@@ -79,11 +84,37 @@ import { helloTask } from "./hello.task";
7984
export class AppModule {}
8085
```
8186

82-
## Create task
87+
The module configuration is [`GraphileWorkerConfiguration`](./src/interfaces/module-config.interfaces.ts), which is a wrapper around Graphile's [`RunnerOptions`](https://github.com/graphile/worker/blob/7feecdde5692569f006d3379f4caee01c4482707/src/interfaces.ts#L716)
88+
89+
```ts
90+
type GraphileWorkerConfiguration = Omit<RunnerOptions, "events" | "taskList">;
91+
```
92+
93+
This means you can pass any configuration to the runner, like [Recurring tasks (crontab)](https://worker.graphile.org/docs/cron):
94+
95+
```ts
96+
// src/app.module.ts
97+
@Module({
98+
imports: [
99+
GraphileWorkerModule.forRoot({
100+
connectionString: "postgres://example:password@postgres/example",
101+
crontab: [' * * * * * taskIdentifier ?priority=1 {"foo": "bar"}'].join(
102+
"\n",
103+
),
104+
}),
105+
],
106+
controllers: [AppController],
107+
providers: [],
108+
})
109+
export class AppModule {}
110+
```
111+
112+
### 2. Create task
83113

84-
To create task you need to define an `@Injectable` class with `@Task(name)` decorator who contains a decorated method `@TaskHandler`:
114+
To create task you need to define an `@Injectable` class with `@Task(name)` decorator containing a decorated method with `@TaskHandler`:
85115

86116
```ts
117+
// src/hello.task.ts
87118
import { Injectable, Logger } from "@nestjs/common";
88119
import type { Helpers } from "graphile-worker";
89120
import { Task, TaskHandler } from "../../src/index";
@@ -103,6 +134,7 @@ export class HelloTask {
103134
Then do not forget to register this class as provider in your module:
104135

105136
```ts
137+
// src/app.module.ts
106138
import { Module } from "@nestjs/common";
107139
import { HelloTask } from "./hello.task";
108140
// ...
@@ -119,9 +151,9 @@ import { HelloTask } from "./hello.task";
119151
export class AppModule {}
120152
```
121153

122-
## Create jobs
154+
### 3. Create jobs
123155

124-
You may use `WorkerService`:
156+
You can use [`WorkerService`](./src/services/worker.service.ts) which is a wrapper of [`graphile-worker`](graphile-worker)'s [`Runner`](https://worker.graphile.org/docs/library/run#runner) instance. [`WorkerService`](./src/services/worker.service.ts) let you add job easily.
125157

126158
```ts
127159
import { WorkerService } from "nestjs-graphile-worker";
@@ -149,9 +181,9 @@ export class AppController {
149181
}
150182
```
151183

152-
## Start runner
184+
### 4. Start runner
153185

154-
Add `WorkerService.run` in `main.ts` file:
186+
Add [`WorkerService.run`](https://github.com/madeindjs/nestjs-graphile-worker/blob/7ed5a99dcd28a11259031e0e738b0cf5a4050904/src/services/worker.service.ts#L31-L42) in `main.ts` file:
155187

156188
```ts
157189
import { WorkerService } from "nestjs-graphile-worker";
@@ -166,11 +198,12 @@ async function bootstrap() {
166198
bootstrap();
167199
```
168200

169-
## `OnWorkerEvent` decorator
201+
### 5. Listen any Graphile's event
170202

171-
This decorator allow you to listen all [Graphile Worker event](https://github.com/graphile/worker#workerevents)
203+
You can use `@OnWorkerEvent` decorator to listen any [Graphile Worker event](https://worker.graphile.org/docs/worker-events). You simply have to:
172204

173-
You need to add `@GraphileWorkerListener` decorator on your class and then set `@OnWorkerEvent(eventName)` on method:
205+
1. `@GraphileWorkerListener` decorator on your class
206+
2. set `@OnWorkerEvent(eventName)` on your method
174207

175208
```ts
176209
import { Injectable, Logger } from "@nestjs/common";
@@ -194,19 +227,9 @@ export class AppService {
194227
}
195228
```
196229

197-
## Test
198-
199-
```bash
200-
# unit tests
201-
$ npm run test
202-
203-
# test coverage
204-
$ npm run test:cov
205-
```
206-
207-
# Sample
230+
## Sample
208231

209-
You can find a [sample](./sample/) who use library. To run it, simply `npm install` and then:
232+
You can find a [sample](./sample/) using this library. To run it, simply `npm install` and then:
210233

211234
```sh
212235
docker-compose up

0 commit comments

Comments
 (0)