@@ -34,14 +34,14 @@ You can use `GraphileWorkerModule.forRoot`:
34
34
35
35
``` ts
36
36
// src/app.module.ts
37
- import { GraphileWorkerModule } from ' nest-graphile-worker' ;
38
- import { Module } from ' @nestjs/common' ;
39
- import { AppController } from ' ./app.controller' ;
37
+ import { GraphileWorkerModule } from " nest-graphile-worker" ;
38
+ import { Module } from " @nestjs/common" ;
39
+ import { AppController } from " ./app.controller" ;
40
40
41
41
@Module ({
42
42
imports: [
43
43
GraphileWorkerModule .forRoot ({
44
- connectionString: ' postgres://example:password@postgres/example' ,
44
+ connectionString: " postgres://example:password@postgres/example" ,
45
45
}),
46
46
],
47
47
controllers: [AppController ],
@@ -53,11 +53,11 @@ export class AppModule {}
53
53
Or you can use ` GraphileWorkerModule.forRootAsync ` :
54
54
55
55
``` ts
56
- import { GraphileWorkerModule } from ' @app/ graphile-worker' ;
57
- import { Module } from ' @nestjs/common' ;
58
- import { ConfigModule , ConfigService } from ' @nestjs/config' ;
59
- import { AppController } from ' ./app.controller' ;
60
- import { helloTask } from ' ./hello.task' ;
56
+ import { GraphileWorkerModule } from " nestjs- graphile-worker" ;
57
+ import { Module } from " @nestjs/common" ;
58
+ import { ConfigModule , ConfigService } from " @nestjs/config" ;
59
+ import { AppController } from " ./app.controller" ;
60
+ import { helloTask } from " ./hello.task" ;
61
61
62
62
@Module ({
63
63
imports: [
@@ -66,7 +66,7 @@ import { helloTask } from './hello.task';
66
66
imports: [ConfigModule ],
67
67
inject: [ConfigService ],
68
68
useFactory : (config : ConfigService ) => ({
69
- connectionString: config .get (' PG_CONNECTION' ),
69
+ connectionString: config .get (" PG_CONNECTION" ),
70
70
taskList: {
71
71
hello: helloTask ,
72
72
},
@@ -84,12 +84,12 @@ export class AppModule {}
84
84
To create task you need to define an ` @Injectable ` class with ` @Task(name) ` decorator who contains a decorated method ` @TaskHandler ` :
85
85
86
86
``` ts
87
- import { Injectable , Logger } from ' @nestjs/common' ;
88
- import { Helpers } from ' graphile-worker' ;
89
- import { Task , TaskHandler } from ' ../../src/index' ;
87
+ import { Injectable , Logger } from " @nestjs/common" ;
88
+ import type { Helpers } from " graphile-worker" ;
89
+ import { Task , TaskHandler } from " ../../src/index" ;
90
90
91
91
@Injectable ()
92
- @Task (' hello' )
92
+ @Task (" hello" )
93
93
export class HelloTask {
94
94
private logger = new Logger (HelloTask .name );
95
95
@@ -103,8 +103,8 @@ export class HelloTask {
103
103
Then do not forget to register this class as provider in your module:
104
104
105
105
``` ts
106
- import { Module } from ' @nestjs/common' ;
107
- import { HelloTask } from ' ./hello.task' ;
106
+ import { Module } from " @nestjs/common" ;
107
+ import { HelloTask } from " ./hello.task" ;
108
108
// ...
109
109
110
110
@Module ({
@@ -124,8 +124,8 @@ export class AppModule {}
124
124
You may use ` WorkerService ` :
125
125
126
126
``` ts
127
- import { WorkerService } from ' @app/ graphile-worker' ;
128
- import { Controller , HttpCode , Post } from ' @nestjs/common' ;
127
+ import { WorkerService } from " nestjs- graphile-worker" ;
128
+ import { Controller , HttpCode , Post } from " @nestjs/common" ;
129
129
130
130
@Controller ()
131
131
export class AppController {
@@ -134,15 +134,15 @@ export class AppController {
134
134
@Post ()
135
135
@HttpCode (201 )
136
136
async addJob() {
137
- await this .graphileWorker .addJob (' hello' , { hello: ' world' });
137
+ await this .graphileWorker .addJob (" hello" , { hello: " world" });
138
138
}
139
139
140
- @Post (' bulk' )
140
+ @Post (" bulk" )
141
141
@HttpCode (201 )
142
142
async addJobs() {
143
143
const jobs = new Array (100 )
144
144
.fill (undefined )
145
- .map ((_ , i ) => ({ identifier: ' hello' , payload: { hello: i } }));
145
+ .map ((_ , i ) => ({ identifier: " hello" , payload: { hello: i } }));
146
146
147
147
return this .graphileWorker .addJobs (jobs );
148
148
}
@@ -154,9 +154,9 @@ export class AppController {
154
154
Add ` WorkerService.run ` in ` main.ts ` file:
155
155
156
156
``` ts
157
- import { WorkerService } from ' @app/ graphile-worker' ;
158
- import { NestFactory } from ' @nestjs/core' ;
159
- import { AppModule } from ' ./app.module' ;
157
+ import { WorkerService } from " nestjs- graphile-worker" ;
158
+ import { NestFactory } from " @nestjs/core" ;
159
+ import { AppModule } from " ./app.module" ;
160
160
161
161
async function bootstrap() {
162
162
const app = await NestFactory .create (AppModule );
@@ -168,27 +168,27 @@ bootstrap();
168
168
169
169
## ` OnWorkerEvent ` decorator
170
170
171
- This decorator allow you to listen all [ GRaphile Worker event] ( https://github.com/graphile/worker#workerevents )
171
+ This decorator allow you to listen all [ Graphile Worker event] ( https://github.com/graphile/worker#workerevents )
172
172
173
173
You need to add ` @GraphileWorkerListener ` decorator on your class and then set ` @OnWorkerEvent(eventName) ` on method:
174
174
175
175
``` ts
176
- import { Injectable , Logger } from ' @nestjs/common' ;
177
- import { WorkerEventMap } from ' graphile-worker' ;
178
- import { GraphileWorkerListener , OnWorkerEvent } from ' ../../src/index' ;
176
+ import { Injectable , Logger } from " @nestjs/common" ;
177
+ import { WorkerEventMap } from " graphile-worker" ;
178
+ import { GraphileWorkerListener , OnWorkerEvent } from " ../../src/index" ;
179
179
180
180
@Injectable ()
181
181
@GraphileWorkerListener ()
182
182
export class AppService {
183
183
private readonly logger = new Logger (AppService .name );
184
184
185
- @OnWorkerEvent (' job:success' )
186
- onJobSuccess({ job }: WorkerEventMap [' job:success' ]) {
185
+ @OnWorkerEvent (" job:success" )
186
+ onJobSuccess({ job }: WorkerEventMap [" job:success" ]) {
187
187
this .logger .debug (` job #${job .id } finished ` );
188
188
}
189
189
190
- @OnWorkerEvent (' job:error' )
191
- onJobError({ job , error }: WorkerEventMap [' job:error' ]) {
190
+ @OnWorkerEvent (" job:error" )
191
+ onJobError({ job , error }: WorkerEventMap [" job:error" ]) {
192
192
this .logger .error (` job #${job .id } fail ${JSON .stringify (error )} ` );
193
193
}
194
194
}
0 commit comments