Skip to content

Commit

Permalink
Added cleanupJobAfterCollection flag
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinschabschneider committed Oct 21, 2023
1 parent 3b8c7e4 commit 8e2fe26
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 160 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MAX_TIMEOUT=10000
PERSIST_PERIOD=3600000
DEBUG=false
QUEUE_CONCURRENCY=10
CLEANUP_JOB_AFTER_COLLECTED=false
# ADDITIONAL_SCRIPTS=
# HTTP_HEADERS=
# SECRET_KEY=
Expand Down
9 changes: 9 additions & 0 deletions src/print/collect.configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { get } from 'env-var';

export default () => {
return {
cleanupJobAfterCollected: get('CLEANUP_JOB_AFTER_COLLECTED')
.default('false')
.asBool(),
};
};
34 changes: 28 additions & 6 deletions src/print/collect.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Controller,
Get,
Logger,
NotFoundException,
Param,
Query,
Res,
Expand All @@ -11,17 +13,21 @@ import {
import { ApiTags } from '@nestjs/swagger';
import { Response } from 'express';

import { ConfigService } from '@nestjs/config';
import { ConditionalHtmlExceptionsFilter } from '../common/conditional-html.filter';
import { PrintQueueService } from '../queue/print-queue.service';
import { PrinterQueueService } from '../queue/printer-queue.service';
import { PrintServiceFactory } from '../whatever/print.service.factory';
import { PrintUrlOptionalDto } from './dto/print-url-optional.dto';
import { PrintUrlCollectOptionalDto } from './dto/print-url-collect-optional.dto';

@Controller('collect/:jobId')
@ApiTags('collect')
export class CollectController {
private readonly logger = new Logger(CollectController.name);

constructor(
private readonly queueService: PrintQueueService,
private readonly printerQueueService: PrinterQueueService,
private readonly printServiceFactory: PrintServiceFactory,
private readonly configService: ConfigService,
) {}

@Get()
Expand All @@ -31,19 +37,35 @@ export class CollectController {
@Res({ passthrough: true }) response: Response,
@Param('jobId') jobId: string,
@Query(new ValidationPipe({ transform: true }))
{ download, fileName }: PrintUrlOptionalDto,
{ download, fileName, cleanupJob }: PrintUrlCollectOptionalDto,
): Promise<StreamableFile | string> {
const jobReturnValue = await this.queueService.getPrintJobResult(jobId);
this.logger.log(`Collecting job ${jobId}`);

const job = await this.printerQueueService.getPrintJob(jobId);

if (!job) throw new NotFoundException('Job not found');

const jobReturnValue = await this.printerQueueService.getPrintJobResult(
job,
);

const printService = this.printServiceFactory.create(
jobReturnValue.outputType,
);

return printService.createResponse(
const collectResponse = printService.createResponse(
jobReturnValue.data,
download,
fileName,
response,
);

if (
this.configService.get<boolean>('cleanupJobAfterCollected') ||
cleanupJob
)
await this.printerQueueService.removePrintJob(job);

return collectResponse;
}
}
14 changes: 14 additions & 0 deletions src/print/dto/print-url-collect-optional.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsOptional } from 'class-validator';
import { PrintUrlOptionalDto } from './print-url-optional.dto';

export class PrintUrlCollectOptionalDto extends PrintUrlOptionalDto {
@Transform(({ value }) => value === 'true')
@IsOptional()
@ApiPropertyOptional({
description: 'Cleanup job data.',
default: false,
})
cleanupJob: boolean = false;
}
6 changes: 3 additions & 3 deletions src/print/print-now.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Response } from 'express';
import { ApiKeyAuthGuard } from '../auth/api-key-auth.guard';
import { JwtParamAuthGuard } from '../auth/jwt-param-auth.guard';
import { ConditionalHtmlExceptionsFilter } from '../common/conditional-html.filter';
import { PrintQueueService } from '../queue/print-queue.service';
import { PrinterQueueService } from '../queue/printer-queue.service';
import { PrintOptions } from '../whatever/print-options.interface';
import { PrintOutputType } from '../whatever/print-output-type.enum'; // TODO: dont konw if it should be here, mabye common
import { PrintUrlOptionalDto } from './dto/print-url-optional.dto';
Expand All @@ -31,7 +31,7 @@ const PRIORITY = 2;
@ApiTags('print/now')
export class PrintNowController {
constructor(
private readonly printQueueService: PrintQueueService,
private readonly printerQueueService: PrinterQueueService,
private readonly jwtService: JwtService,
) {}

Expand Down Expand Up @@ -160,7 +160,7 @@ export class PrintNowController {
download: boolean,
fileName: string,
) {
const job = await this.printQueueService.addPrintJob(options, priority);
const job = await this.printerQueueService.addPrintJob(options, priority);

try {
await job.finished();
Expand Down
10 changes: 5 additions & 5 deletions src/print/print-soon.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';

import { ApiKeyAuthGuard } from '../auth/api-key-auth.guard';
import { ConditionalHtmlExceptionsFilter } from '../common/conditional-html.filter';
import { PrintQueueService } from '../queue/print-queue.service';
import { PrintSoonCreateDto } from '../queue/print-soon-create.dto';
import { PrintSoonStatusDto } from '../queue/print-soon-status.dto';
import { PrinterQueueService } from '../queue/printer-queue.service';
import { PrintOutputType } from '../whatever/print-output-type.enum';
import { PrintUrlCallbackOptionalDto } from './dto/print-url-callback-optional.dto';

const PRIORITY = 1;
@Controller('print/:outputType/soon')
@ApiTags('print/soon')
export class PrintSoonController {
constructor(private readonly queueService: PrintQueueService) {}
constructor(private readonly printerQueueService: PrinterQueueService) {}

@Post()
@UseGuards(ApiKeyAuthGuard)
Expand Down Expand Up @@ -58,7 +58,7 @@ export class PrintSoonController {
}
}

const job = await this.queueService.addPrintJob(
const job = await this.printerQueueService.addPrintJob(
{
input: { url, html },
outputType,
Expand All @@ -78,8 +78,8 @@ export class PrintSoonController {
@UseGuards(ApiKeyAuthGuard)
@UseFilters(ConditionalHtmlExceptionsFilter)
async getJobInfo(@Param('jobId') jobId: string): Promise<PrintSoonStatusDto> {
const job = await this.queueService.getPrintJob(jobId);
const job = await this.printerQueueService.getPrintJob(jobId);

return this.queueService.getPrintJobStatus(job);
return this.printerQueueService.getPrintJobStatus(job);
}
}
11 changes: 10 additions & 1 deletion src/print/print.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';

import { ConfigModule } from '@nestjs/config';
import { WhateverModule } from 'src/whatever/whatever.module';
import { QueueModule } from '../queue/queue.module';
import configuration from './collect.configuration';
import { CollectController } from './collect.controller';
import { PrintNowController } from './print-now.controller';
import { PrintSoonController } from './print-soon.controller';

@Module({
imports: [JwtModule, QueueModule, WhateverModule],
imports: [
JwtModule,
QueueModule,
WhateverModule,
ConfigModule.forRoot({
load: [configuration],
}),
],
controllers: [PrintNowController, PrintSoonController, CollectController],
})
export class PrintModule {}
8 changes: 4 additions & 4 deletions src/queue/callback-queue.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import { Job } from 'bull';
import { HttpService } from '@nestjs/axios';
import { Logger } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';
import { PrintQueueService } from './print-queue.service';
import { PrinterQueueService } from './printer-queue.service';

@Processor({ name: 'callbacker' })
export class CallbackQueueConsumer {
private readonly logger = new Logger(CallbackQueueConsumer.name);

constructor(
private readonly printQueueService: PrintQueueService,
private readonly printerQueueService: PrinterQueueService,
private readonly httpService: HttpService,
) {}

@Process({ name: 'callback' })
async transcode(job: Job<string>) {
this.logger.log(`Process callback job ${job.data}`);

const printJob = await this.printQueueService.getPrintJob(job.data);
const printJob = await this.printerQueueService.getPrintJob(job.data);

this.logger.log(`Sending callback ${printJob.data.callbackUrl}`);

const printJobStatus = await this.printQueueService.getPrintJobStatus(
const printJobStatus = await this.printerQueueService.getPrintJobStatus(
printJob,
);

Expand Down
136 changes: 68 additions & 68 deletions src/queue/queue.consumer.ts → src/queue/printer-queue.consumer.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
import {
InjectQueue,
OnQueueCompleted,
OnQueueFailed,
Process,
Processor,
} from '@nestjs/bull';
import { Job, Queue } from 'bull';
import { get } from 'env-var';

import { Logger } from '@nestjs/common';
import { PrintStep } from '../pagedjs/print-step.enum';
import { PrintOptions } from '../whatever/print-options.interface';
import { PrintServiceFactory } from '../whatever/print.service.factory';
import { JobReturnValue } from './job-return-value.interface';

@Processor({ name: 'printer' })
export class QueueConsumer {
private readonly logger = new Logger(QueueConsumer.name);

constructor(
private readonly printServiceFactory: PrintServiceFactory,
@InjectQueue('callbacker') private callbackQueue: Queue,
) {}

@Process({
name: 'print',
concurrency: get('QUEUE_CONCURRENCY').default(1).asIntPositive(), // TODO: hmmmmmmmmm
})
async transcode(job: Job<PrintOptions>): Promise<JobReturnValue> {
this.logger.log(`Process job ${job.id}`);

const printService = this.printServiceFactory.create(job.data.outputType);

const data = await printService.print(
job.data.input,
job.data.additionalScripts,
job.data.timeout,
job.data.injectPolyfill,
job.data.httpHeaders,
async (step: PrintStep) => await job.progress(step.toString()),
);

return { data, outputType: job.data.outputType };
}

@OnQueueCompleted({ name: 'print' })
completedHandler(job: Job) {
this.logger.log(`Finished print ${job.id}`);

this.processHandler(job);
}

@OnQueueFailed({ name: 'print' })
failedHandler(job: Job, e: Error) {
this.logger.error(`Failed print ${job.id}`, e);

this.processHandler(job);
}

private processHandler(job: Job) {
if (job.data.callbackUrl) {
this.logger.log(`Addin callback job for print job ${job.id}`);

this.callbackQueue.add('callback', job.id);
}
}
}
import {
InjectQueue,
OnQueueCompleted,
OnQueueFailed,
Process,
Processor,
} from '@nestjs/bull';
import { Job, Queue } from 'bull';
import { get } from 'env-var';

import { Logger } from '@nestjs/common';
import { PrintStep } from '../pagedjs/print-step.enum';
import { PrintOptions } from '../whatever/print-options.interface';
import { PrintServiceFactory } from '../whatever/print.service.factory';
import { JobReturnValue } from './job-return-value.interface';

@Processor({ name: 'printer' })
export class PrinterQueueConsumer {
private readonly logger = new Logger(PrinterQueueConsumer.name);

constructor(
private readonly printServiceFactory: PrintServiceFactory,
@InjectQueue('callbacker') private callbackerQueue: Queue,
) {}

@Process({
name: 'print',
concurrency: get('QUEUE_CONCURRENCY').default(1).asIntPositive(), // TODO: hmmmmmmmmm
})
async transcode(job: Job<PrintOptions>): Promise<JobReturnValue> {
this.logger.log(`Process job ${job.id}`);

const printService = this.printServiceFactory.create(job.data.outputType);

const data = await printService.print(
job.data.input,
job.data.additionalScripts,
job.data.timeout,
job.data.injectPolyfill,
job.data.httpHeaders,
async (step: PrintStep) => await job.progress(step.toString()),
);

return { data, outputType: job.data.outputType };
}

@OnQueueCompleted({ name: 'print' })
completedHandler(job: Job) {
this.logger.log(`Finished print ${job.id}`);

this.processHandler(job);
}

@OnQueueFailed({ name: 'print' })
failedHandler(job: Job, e: Error) {
this.logger.error(`Failed print ${job.id}`, e);

this.processHandler(job);
}

private processHandler(job: Job) {
if (job.data.callbackUrl) {
this.logger.log(`Addin callback job for print job ${job.id}`);

this.callbackerQueue.add('callback', job.id);
}
}
}
Loading

0 comments on commit 8e2fe26

Please sign in to comment.