Skip to content

Commit 4c72856

Browse files
authored
Merge pull request #995 from Dudrie/add-puppeteer-options
Add timeout option to pass to puppeteer.
2 parents 5c8ad2a + 4af70a6 commit 4c72856

7 files changed

Lines changed: 57 additions & 25 deletions

File tree

server/config/development.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ sessionTimeout: 120
88
handbookUrl: 'https://dudrie.github.io/Tutor-Management-System/'
99

1010
database:
11-
databaseURL: 'mongodb://localhost:27017/tms-new-grading'
12-
maxRetries: 2
13-
config:
14-
authSource: 'admin'
15-
authMechanism: 'SCRAM-SHA-1'
11+
databaseURL: 'mongodb://localhost:27017/tms-new-grading'
12+
maxRetries: 2
13+
config:
14+
authSource: 'admin'
15+
authMechanism: 'SCRAM-SHA-1'
1616

1717
defaultSettings:
18-
canTutorExcuseStudents: true
19-
defaultTeamSize: 3
18+
canTutorExcuseStudents: true
19+
defaultTeamSize: 3
20+
21+
puppeteer:
22+
timeout: abcd

server/config/production.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ handbookUrl: 'https://dudrie.github.io/Tutor-Management-System/'
99

1010
# Configuration for the database access. More information can be found in the documentation.
1111
database:
12-
databaseURL: 'mongodb://mongo:27017/tms'
13-
maxRetries: 2
14-
config:
15-
authSource: 'admin'
16-
authMechanism: 'SCRAM-SHA-1'
12+
databaseURL: 'mongodb://mongo:27017/tms'
13+
maxRetries: 2
14+
config:
15+
authSource: 'admin'
16+
authMechanism: 'SCRAM-SHA-1'
1717

1818
# Set specific settings to a default value. These settings can also be edited through the client. More information can be found in the documentation.
1919
defaultSettings:
20-
canTutorExcuseStudents: true
21-
defaultTeamSize: 3
20+
canTutorExcuseStudents: true
21+
defaultTeamSize: 3
22+
23+
# Settings used for the puppeteer instance
24+
puppeteer:
25+
# Timeout in ms
26+
timeout: 30000

server/src/module/pdf/subservices/PDFGenerator.core.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Logger } from '@nestjs/common';
22
import fs from 'fs';
33
import puppeteer from 'puppeteer';
4+
import { StaticSettings } from '../../settings/settings.static';
45

56
/**
67
* @param T Type of the options passed to `generatePDF`.
@@ -17,7 +18,7 @@ export abstract class PDFGenerator<T = Record<string, unknown>> {
1718
public abstract generatePDF(options: T): Promise<Buffer>;
1819

1920
/**
20-
* Generates a PDF from the given body. The body gets put in a HTML wrapper first.
21+
* Generates a PDF from the given body. The body gets put in an HTML wrapper first.
2122
*
2223
* @param body Body content to be put in the PDF as HTML body.
2324
*
@@ -34,6 +35,7 @@ export abstract class PDFGenerator<T = Record<string, unknown>> {
3435
browser = await puppeteer.launch({
3536
args: ['--disable-dev-shm-usage'],
3637
executablePath: process.env.TMS_PUPPETEER_EXEC_PATH,
38+
timeout: StaticSettings.getService().getPuppeteerConfiguration()?.timeout,
3739
});
3840

3941
this.logger.debug('Browser started.');
@@ -90,9 +92,9 @@ export abstract class PDFGenerator<T = Record<string, unknown>> {
9092
<head>
9193
<style>${githubCSS}</style>
9294
<style>${highlightCSS}</style>
93-
<style>${this.getCustomCSS()}</style>
95+
<style>${PDFGenerator.getCustomCSS()}</style>
9496
</head>
95-
<body class="markdown-body">${body}</body>
97+
<body class='markdown-body'>${body}</body>
9698
</html>
9799
`;
98100
}
@@ -126,7 +128,7 @@ export abstract class PDFGenerator<T = Record<string, unknown>> {
126128
/**
127129
* @returns Some small customizations to the GitHub markdown CSS.
128130
*/
129-
private getCustomCSS(): string {
131+
private static getCustomCSS(): string {
130132
return '.markdown-body table { display: table; width: 100%; }';
131133
}
132134
}

server/src/module/settings/model/ApplicationConfiguration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Type } from 'class-transformer';
22
import { IsNumber, IsOptional, IsString, IsUrl, Min, ValidateNested } from 'class-validator';
33
import { ClientSettingsDTO } from '../settings.dto';
44
import { DatabaseConfiguration } from './DatabaseConfiguration';
5+
import { PuppeteerConfiguration } from './PuppeteerConfiguration';
56

67
export class ApplicationConfiguration {
78
@IsOptional()
@@ -25,4 +26,9 @@ export class ApplicationConfiguration {
2526
@Type(() => ClientSettingsDTO)
2627
@ValidateNested()
2728
readonly defaultSettings: ClientSettingsDTO | undefined;
29+
30+
@IsOptional()
31+
@Type(() => PuppeteerConfiguration)
32+
@ValidateNested()
33+
readonly puppeteer: PuppeteerConfiguration | undefined;
2834
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { IsNumber, Min } from 'class-validator';
2+
3+
export class PuppeteerConfiguration {
4+
// @IsOptional()
5+
@IsNumber({}, { always: true })
6+
@Min(0)
7+
readonly timeout!: number;
8+
}

server/src/module/settings/settings.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ReturnModelType } from '@typegoose/typegoose';
33
import { InjectModel } from 'nestjs-typegoose';
44
import { SettingsDocument, SettingsModel } from '../../database/models/settings.model';
55
import { StartUpException } from '../../exceptions/StartUpException';
6-
import { IClientSettings, IMailingSettings } from '../../shared/model/Settings';
6+
import { IClientSettings, IMailingSettings } from 'shared/model/Settings';
77
import { ClientSettingsDTO } from './settings.dto';
88
import { StaticSettings } from './settings.static';
99

server/src/module/settings/settings.static.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
DatabaseConfiguration,
1111
DatabaseConfigurationValidationGroup,
1212
} from './model/DatabaseConfiguration';
13-
import { EnvironmentConfig, ENV_VARIABLE_NAMES } from './model/EnvironmentConfig';
13+
import { ENV_VARIABLE_NAMES, EnvironmentConfig } from './model/EnvironmentConfig';
14+
import { PuppeteerConfiguration } from './model/PuppeteerConfiguration';
1415

1516
export class StaticSettings {
1617
private static service: StaticSettings = new StaticSettings();
@@ -27,7 +28,7 @@ export class StaticSettings {
2728
constructor() {
2829
this.config = this.loadConfigFile();
2930

30-
this.envConfig = this.loadEnvironmentVariables();
31+
this.envConfig = StaticSettings.loadEnvironmentVariables();
3132
this.databaseConfig = this.loadDatabaseConfig();
3233
}
3334

@@ -45,7 +46,7 @@ export class StaticSettings {
4546
/**
4647
* Returns the encryption secret for the database.
4748
*
48-
* @returns Encrytion secret.
49+
* @returns Encryption secret.
4950
*/
5051
getDatabaseSecret(): string {
5152
return this.databaseConfig.secret;
@@ -58,6 +59,13 @@ export class StaticSettings {
5859
return this.databaseConfig;
5960
}
6061

62+
/**
63+
* @returns Configuration for the puppeteer instance. Can be `undefined`.
64+
*/
65+
getPuppeteerConfiguration(): PuppeteerConfiguration | undefined {
66+
return this.config.puppeteer;
67+
}
68+
6169
/**
6270
* Returns the value of the `sessionTimeout` setting.
6371
*
@@ -166,12 +174,12 @@ export class StaticSettings {
166174
* @returns Valid configuration extracted from the environment variables.
167175
* @throws `StartUpException` - If not all required environment variables were provided.
168176
*/
169-
private loadEnvironmentVariables(): EnvironmentConfig {
177+
private static loadEnvironmentVariables(): EnvironmentConfig {
170178
const envConfig = plainToClass(EnvironmentConfig, process.env, {
171179
excludeExtraneousValues: true,
172180
});
173181

174-
this.assertEnvNoErrors(validateSync(envConfig));
182+
StaticSettings.assertEnvNoErrors(validateSync(envConfig));
175183

176184
return envConfig;
177185
}
@@ -182,7 +190,7 @@ export class StaticSettings {
182190
* @param errors Array containing validation errors from class-validator (or empty).
183191
* @throws `StartUpException` - If `errors` is not empty.
184192
*/
185-
private assertEnvNoErrors(errors: ValidationError[]) {
193+
private static assertEnvNoErrors(errors: ValidationError[]) {
186194
if (errors.length === 0) {
187195
return;
188196
}

0 commit comments

Comments
 (0)