Skip to content

Commit

Permalink
expriment with redis
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Mar 13, 2023
1 parent 7e828e5 commit 8139d39
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 44 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,36 @@ monitoring RESTful API server that allows authenticated users to monitor URLs, a
## Run Local Development environment via docker

```bash
$ docker compose --env-file .env.dev up
docker compose --env-file .env.dev up
```

## Running e2e tests
requires `pnpm` and `docker`

1) it spawns a test db container
2) create a an instance of the app
3) run e2e tests
4) close/destory the app
5) teardown test db

```bash
$ pnpm test:e2e
```

## Test

### Running unit tests

```bash
# unit tests
$ pnpm run test
pnpm run test

# test coverage
$ pnpm run test:cov
pnpm run test:cov
```

### Running e2e tests
requires `pnpm` and `docker`

basically what running e2e tests do:
1) it spawns a test db container
2) create a an instance of the app
3) run e2e tests
4) close/destory the app
5) teardown test db

```bash
pnpm test:e2e
```
## Swagger (OpenAPI) documentation

accessed via the endpoint ` /api`
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { UsersModule } from './components/users/users.module';
import { BullModule } from '@nestjs/bullmq';

// import { UrlCheckSubscriber } from './url-checks/entities/subscribers/url-check.subscriber';
import { JobQueueModule } from './components/job-queue/job-queue.module';

@Module({
imports: [
Expand Down Expand Up @@ -58,6 +59,7 @@ import { BullModule } from '@nestjs/bullmq';
ReportsModule,
EmailModule,
PollingModule,
JobQueueModule,
],
controllers: [AppController],
providers: [
Expand Down
10 changes: 10 additions & 0 deletions src/components/job-queue/job-queue.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BullModule } from '@nestjs/bullmq';
import { Module } from '@nestjs/common';
import { JobQueueService } from './job-queue.service';
import { TestConsumer } from './test.consumer';

@Module({
imports: [BullModule.registerQueue({ name: 'test' })],
providers: [JobQueueService, TestConsumer],
})
export class JobQueueModule {}
18 changes: 18 additions & 0 deletions src/components/job-queue/job-queue.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JobQueueService } from './job-queue.service';

describe('JobQueueService', () => {
let service: JobQueueService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JobQueueService],
}).compile();

service = module.get<JobQueueService>(JobQueueService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
22 changes: 22 additions & 0 deletions src/components/job-queue/job-queue.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { InjectQueue } from '@nestjs/bullmq';
import { Injectable } from '@nestjs/common';
import { Queue } from 'bullmq';

@Injectable()
export class JobQueueService {
constructor(@InjectQueue('test') private testQueue: Queue) {}

async testTheQueue() {
console.log('I am gonna test the queue');
const job = await this.testQueue.add(
'test',
{
ping: 'pong',
},
{ repeat: { every: 5000 } },
);
console.log(await this.testQueue.getRepeatableJobs());

this.testQueue.removeRepeatableByKey(job.repeatJobKey);
}
}
9 changes: 9 additions & 0 deletions src/components/job-queue/test.consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Job } from 'bullmq';

@Processor('test')
export class TestConsumer extends WorkerHost {
async process(job: Job<any, any, string>, token?: string): Promise<any> {
console.log(job.data);
}
}
20 changes: 0 additions & 20 deletions src/components/polling/polling.controller.spec.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/components/polling/polling.controller.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/components/polling/polling.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Module } from '@nestjs/common';
import { PollingService } from './polling.service';
import { PollingController } from './polling.controller';
import { HttpModule } from '@nestjs/axios';
import { ReportsModule } from '../reports/reports.module';
import { BullModule } from '@nestjs/bullmq';

@Module({
imports: [HttpModule, ReportsModule],
controllers: [PollingController],
imports: [
HttpModule,
ReportsModule,
BullModule.registerQueue({ name: 'polling' }),
],
providers: [PollingService],
exports: [PollingService],
})
Expand Down
3 changes: 3 additions & 0 deletions src/components/polling/polling.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { HttpService } from '@nestjs/axios';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { Injectable, Logger } from '@nestjs/common';
import { SchedulerRegistry } from '@nestjs/schedule';
import * as https from 'https';
Expand Down Expand Up @@ -26,6 +28,7 @@ export class PollingService {
private readonly logger = new Logger(PollingService.name);

constructor(
@InjectQueue('polling') private pollingQueue: Queue,
private schedulerRegistry: SchedulerRegistry,
private httpService: HttpService,
private reportService: ReportsService,
Expand Down

0 comments on commit 8139d39

Please sign in to comment.