Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Add semaphore to block on puppeteer chromium execution #284

Merged
merged 8 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kibana-reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"plugin_helpers": "node ../../scripts/plugin_helpers"
},
"dependencies": {
"async-mutex": "^0.2.6",
"babel-polyfill": "^6.26.0",
"cheerio": "0.22.0",
"cron-validator": "^1.1.1",
Expand Down
8 changes: 4 additions & 4 deletions kibana-reports/public/components/main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ export function Main(props) {
addReportDefinitionsTableErrorToastHandler(errorType);
};

const addErrorOnDemandDownloadToastHandler = () => {
const addErrorOnDemandDownloadToastHandler = (title = 'Error downloading report.') => {
const errorToast = {
title: 'Error downloading report.',
title,
color: 'danger',
iconType: 'alert',
id: 'onDemandDownloadErrorToast',
};
setToasts(toasts.concat(errorToast));
};

const handleOnDemandDownloadErrorToast = () => {
addErrorOnDemandDownloadToastHandler();
const handleOnDemandDownloadErrorToast = (title?: string) => {
addErrorOnDemandDownloadToastHandler(title);
};

const addSuccessOnDemandDownloadToastHandler = () => {
Expand Down
2 changes: 2 additions & 0 deletions kibana-reports/public/components/main/main_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export const generateReportById = async (
console.log('error on generating report by id:', error);
if (error.body.statusCode === 403) {
handlePermissionsMissingToast();
} else if (error.body.statusCode === 503) {
handleErrorToast('Server busy, please try again later.')
} else {
handleErrorToast();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ export function ReportDetails(props) {
addPermissionsMissingDownloadToastHandler();
}

const addErrorToastHandler = () => {
const addErrorToastHandler = (title = 'Error loading report details.') => {
const errorToast = {
title: 'Error loading report details.',
title,
color: 'danger',
iconType: 'alert',
id: 'reportDetailsErrorToast',
};
setToasts(toasts.concat(errorToast));
};

const handleErrorToast = () => {
addErrorToastHandler();
const handleErrorToast = (title?: string) => {
addErrorToastHandler(title);
};

const addSuccessToastHandler = () => {
Expand Down
7 changes: 7 additions & 0 deletions kibana-reports/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ILegacyClusterClient,
} from '../../../src/core/server';
import { setIntervalAsync } from 'set-interval-async/dynamic';
import { Semaphore, SemaphoreInterface, withTimeout } from 'async-mutex';
import esReportsPlugin from './backend/opendistro-es-reports-plugin';
import notificationPlugin from './backend/opendistro-notification-plugin';
import {
Expand Down Expand Up @@ -50,9 +51,14 @@ export class OpendistroKibanaReportsPlugin
OpendistroKibanaReportsPluginStart
> {
private readonly logger: Logger;
private readonly semaphore: SemaphoreInterface;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();

const timeoutError = new Error('Server busy');
timeoutError.statusCode = 503;
this.semaphore = withTimeout(new Semaphore(1), 180000, timeoutError);
}

public setup(core: CoreSetup) {
Expand Down Expand Up @@ -83,6 +89,7 @@ export class OpendistroKibanaReportsPlugin
(context, request) => {
return {
logger: this.logger,
semaphore: this.semaphore,
notificationClient,
esReportsClient,
};
Expand Down
22 changes: 15 additions & 7 deletions kibana-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SetCookie } from 'puppeteer-core';
import { deliverReport } from './deliverReport';
import { updateReportState } from './updateReportState';
import { saveReport } from './saveReport';
import { SemaphoreInterface } from 'async-mutex';

export const createReport = async (
request: KibanaRequest,
Expand All @@ -45,6 +46,8 @@ export const createReport = async (
const isScheduledTask = false;
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
//@ts-ignore
const semaphore: SemaphoreInterface = context.reporting_plugin.semaphore;
// @ts-ignore
const notificationClient: ILegacyScopedClusterClient = context.reporting_plugin.notificationClient.asScoped(
request
Expand Down Expand Up @@ -102,13 +105,18 @@ export const createReport = async (
}
});
}
createReportResult = await createVisualReport(
reportParams,
completeQueryUrl,
logger,
cookieObject,
timezone
);
const [value, release] = await semaphore.acquire();
penghuo marked this conversation as resolved.
Show resolved Hide resolved
try {
createReportResult = await createVisualReport(
reportParams,
completeQueryUrl,
logger,
cookieObject,
timezone
);
} finally {
release();
}
}
// update report state to "created"
// TODO: temporarily remove the following
Expand Down
12 changes: 12 additions & 0 deletions kibana-reports/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,13 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==

async-mutex@^0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.2.6.tgz#0d7a3deb978bc2b984d5908a2038e1ae2e54ff40"
integrity sha512-Hs4R+4SPgamu6rSGW8C7cV9gaWUKEHykfzCCvIRuaVv636Ju10ZdeUbvb4TBEW0INuq2DHZqXbK4Nd3yG4RaRw==
dependencies:
tslib "^2.0.0"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
Expand Down Expand Up @@ -5453,6 +5460,11 @@ tslib@^1.9.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.3.tgz#8e0741ac45fc0c226e58a17bfc3e64b9bc6ca61c"
integrity sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==

[email protected]:
version "0.0.0"
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"
Expand Down