Skip to content

Commit 268311d

Browse files
committed
Add quickAddJob
1 parent 34f610c commit 268311d

15 files changed

+666
-189
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"Graphile",
4+
"postgres"
5+
]
6+
}

docker-compose.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3.3'
2+
services:
3+
backend:
4+
image: node:16-alpine
5+
working_dir: /usr/src/app
6+
command: sh -c "npm install && npm run start:dev"
7+
volumes:
8+
- ./.:/usr/src/app
9+
ports:
10+
- 3000:3000
11+
depends_on:
12+
- postgres
13+
14+
postgres:
15+
image: postgres:13
16+
environment:
17+
POSTGRES_USER: example
18+
POSTGRES_PASSWORD: password
19+
POSTGRES_DB: example
20+
# command: postgres -c max_locks_per_transaction=1000
21+
ports:
22+
- 5432:5432
23+
expose:
24+
- 5432
25+
volumes:
26+
- ./docker/postgres:/docker-entrypoint-initdb.d
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { DynamicModule, Module, Provider } from '@nestjs/common';
2+
import { GraphileWorkerService } from './graphile-worker.service';
3+
4+
export const GRAPHILE_WORKER_TOKEN = Symbol.for('NestJsGraphileWorker');
5+
6+
@Module({
7+
providers: [GraphileWorkerService],
8+
exports: [GraphileWorkerService],
9+
})
10+
export class GraphileWorkerModule {
11+
/**
12+
*
13+
* @param connectionString `${env.TYPEORM_CONNECTION}://${env.TYPEORM_USERNAME}:${env.TYPEORM_PASSWORD}@${env.TYPEORM_HOST}/${env.TYPEORM_DATABASE}`
14+
}
15+
* @returns
16+
*/
17+
static forRoot(connectionString: string): DynamicModule {
18+
const graphileWorkerService: Provider = {
19+
provide: GraphileWorkerService,
20+
useValue: new GraphileWorkerService(connectionString),
21+
};
22+
23+
return {
24+
global: true,
25+
module: GraphileWorkerModule,
26+
providers: [graphileWorkerService],
27+
exports: [graphileWorkerService],
28+
};
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { GraphileWorkerService } from './graphile-worker.service';
3+
4+
describe('GraphileWorkerService', () => {
5+
let service: GraphileWorkerService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [GraphileWorkerService],
10+
}).compile();
11+
12+
service = module.get<GraphileWorkerService>(GraphileWorkerService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Injectable, Logger } from '@nestjs/common';
2+
import {
3+
Job,
4+
quickAddJob,
5+
runMigrations,
6+
TaskSpec,
7+
WorkerUtilsOptions,
8+
} from 'graphile-worker';
9+
10+
@Injectable()
11+
export class GraphileWorkerService {
12+
private readonly logger = new Logger(GraphileWorkerService.name);
13+
private isMigrationDone: boolean;
14+
15+
constructor(private readonly connectionString: string) {}
16+
17+
async quickAddJob(
18+
options: WorkerUtilsOptions,
19+
identifier: string,
20+
payload?: unknown,
21+
spec?: TaskSpec,
22+
): Promise<Job> {
23+
await this.runMigrations();
24+
25+
const job = await quickAddJob(
26+
{
27+
connectionString: this.connectionString,
28+
...options,
29+
},
30+
identifier,
31+
payload,
32+
spec,
33+
);
34+
35+
this.logger.debug(`quickAddJob add job #${job.id}`);
36+
37+
return job;
38+
}
39+
40+
private async runMigrations() {
41+
if (this.isMigrationDone) {
42+
return;
43+
}
44+
45+
await runMigrations({ connectionString: this.connectionString });
46+
this.logger.debug('Run migrations');
47+
this.isMigrationDone = true;
48+
}
49+
}

libs/graphile-worker/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './graphile-worker.module';
2+
export * from './graphile-worker.service';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"outDir": "../../dist/libs/graphile-worker"
6+
},
7+
"include": ["src/**/*"],
8+
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
9+
}

nest-cli.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
{
22
"collection": "@nestjs/schematics",
3-
"sourceRoot": "src"
4-
}
3+
"sourceRoot": "src",
4+
"projects": {
5+
"graphile-worker": {
6+
"type": "library",
7+
"root": "libs/graphile-worker",
8+
"entryFile": "index",
9+
"sourceRoot": "libs/graphile-worker/src",
10+
"compilerOptions": {
11+
"tsConfigPath": "libs/graphile-worker/tsconfig.lib.json"
12+
}
13+
}
14+
},
15+
"compilerOptions": {
16+
"webpack": true
17+
}
18+
}

0 commit comments

Comments
 (0)