connected queue to screenshotter; first functional version

main
Inga 🏳‍🌈 2 years ago
parent 540a2de74b
commit a586a0fbc7
  1. 5
      src/app.module.ts
  2. 22
      src/hello-world/controller.spec.ts
  3. 12
      src/hello-world/controller.ts
  4. 8
      src/hello-world/service.ts
  5. 0
      src/screenshots/__image_snapshots__/screenshotter-service-spec-ts-screenshotter-service-should-return-hello-world-screenshot-1-snap.png
  6. 18
      src/screenshots/screenshots.controller.ts
  7. 3
      src/screenshots/screenshots.module.ts
  8. 14
      src/screenshots/screenshots.processor.ts
  9. 0
      src/screenshots/screenshotter.service.spec.ts
  10. 0
      src/screenshots/screenshotter.service.ts
  11. 1
      tsconfig.json

@ -1,8 +1,5 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { HelloWorldController } from './hello-world/controller';
import { HelloWorldService } from './hello-world/service';
import { ScreenshotterService } from './screenshotter/screenshotter.service';
import { BullModule } from '@nestjs/bull'; import { BullModule } from '@nestjs/bull';
import { ScreenshotsModule } from './screenshots/screenshots.module'; import { ScreenshotsModule } from './screenshots/screenshots.module';
import { BullConfigService } from './bull/bullConfigService'; import { BullConfigService } from './bull/bullConfigService';
@ -22,7 +19,5 @@ import { join } from 'path';
rootPath: join(__dirname, '..', 'client'), rootPath: join(__dirname, '..', 'client'),
}), }),
], ],
controllers: [HelloWorldController],
providers: [HelloWorldService, ScreenshotterService],
}) })
export class AppModule {} export class AppModule {}

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HelloWorldController } from './controller';
import { HelloWorldService } from './service';
describe('HelloWorldController', () => {
let helloWorldController: HelloWorldController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [HelloWorldController],
providers: [HelloWorldService],
}).compile();
helloWorldController = app.get<HelloWorldController>(HelloWorldController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(helloWorldController.getHello()).toBe('Hello World!');
});
});
});

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { HelloWorldService } from './service';
@Controller('hello-world')
export class HelloWorldController {
constructor(private readonly helloWorldService: HelloWorldService) {}
@Get()
getHello(): string {
return this.helloWorldService.getHello();
}
}

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloWorldService {
getHello(): string {
return 'Hello World!';
}
}

@ -1,6 +1,8 @@
import { InjectQueue } from '@nestjs/bull'; import { InjectQueue } from '@nestjs/bull';
import { Body, Controller, Get, NotFoundException, Param, Post } from '@nestjs/common'; import { Body, Controller, Get, NotFoundException, Param, Post, Res, StreamableFile } from '@nestjs/common';
import { ApiResponse } from '@nestjs/swagger'; import { ApiResponse } from '@nestjs/swagger';
import { Response } from 'express'
import { Readable } from 'stream';
import { CreateJobRequestDto, GetJobResponseDto, JobStatusDto } from './screenshots.dto' import { CreateJobRequestDto, GetJobResponseDto, JobStatusDto } from './screenshots.dto'
import { QUEUE_NAME, ScreenshotsQueue } from './shared'; import { QUEUE_NAME, ScreenshotsQueue } from './shared';
@ -40,7 +42,7 @@ export class ScreenshotsController {
@Get(':id/result') @Get(':id/result')
@ApiResponse({ status: 404 }) @ApiResponse({ status: 404 })
async getScreenshot(@Param('id') id: string) { async getScreenshot(@Param('id') id: string, @Res({ passthrough: true }) res: Response): Promise<StreamableFile> {
const jobInfo = await this.screenshotsQueue.getJob(id) const jobInfo = await this.screenshotsQueue.getJob(id)
if (!jobInfo) { if (!jobInfo) {
@ -51,6 +53,16 @@ export class ScreenshotsController {
throw new NotFoundException() throw new NotFoundException()
} }
return await jobInfo.returnvalue switch (jobInfo.data.imageType) {
case 'jpeg':
res.setHeader('Content-Type', 'image/jpeg')
break
case 'png':
res.setHeader('Content-Type', 'image/png')
break
}
const buffer = Buffer.from(await jobInfo.returnvalue as string, 'base64')
return new StreamableFile(buffer)
} }
} }

@ -3,6 +3,7 @@ import { Module } from '@nestjs/common';
import { QUEUE_NAME } from './shared'; import { QUEUE_NAME } from './shared';
import { ScreenshotsController } from './screenshots.controller'; import { ScreenshotsController } from './screenshots.controller';
import { ScreenshotsProcessor } from './screenshots.processor'; import { ScreenshotsProcessor } from './screenshots.processor';
import { ScreenshotterService } from './screenshotter.service';
@Module({ @Module({
imports: [ imports: [
@ -11,6 +12,6 @@ import { ScreenshotsProcessor } from './screenshots.processor';
}), }),
], ],
controllers: [ScreenshotsController], controllers: [ScreenshotsController],
providers: [ScreenshotsProcessor] providers: [ScreenshotsProcessor, ScreenshotterService]
}) })
export class ScreenshotsModule {} export class ScreenshotsModule {}

@ -1,12 +1,24 @@
import { Process, Processor } from '@nestjs/bull'; import { Process, Processor } from '@nestjs/bull';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ScreenshotterService } from './screenshotter.service';
import { QUEUE_NAME, ScreenshotJob } from './shared'; import { QUEUE_NAME, ScreenshotJob } from './shared';
@Injectable() @Injectable()
@Processor(QUEUE_NAME) @Processor(QUEUE_NAME)
export class ScreenshotsProcessor { export class ScreenshotsProcessor {
constructor(private readonly screenshotterService: ScreenshotterService) {}
@Process() @Process()
async handleScreenshotJob(job: ScreenshotJob) { async handleScreenshotJob(job: ScreenshotJob) {
job.moveToCompleted('abc') try {
const imageData = await this.screenshotterService.takeScreenshot({
url: job.data.pageUrl,
imageType: job.data.imageType,
})
job.moveToCompleted(imageData.toString('base64'))
} catch(err) {
job.moveToFailed({ message: JSON.stringify(err) })
}
} }
} }

@ -1,6 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"lib": ["ES2019"],
"declaration": true, "declaration": true,
"removeComments": true, "removeComments": true,
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,

Loading…
Cancel
Save