Skip to content

Commit

Permalink
register route handler context provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jun 12, 2020
1 parent ed011c0 commit 1136503
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
8 changes: 6 additions & 2 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ export class ReportingCore {
this.pluginStart$.next(reportingStartDeps);
}

public async pluginIsSetup(): Promise<boolean> {
public async pluginSetsUp(): Promise<boolean> {
// use deps and config as a cached resolver
if (this.pluginSetupDeps && this.config) {
return true;
}
return await this.pluginSetup$.pipe(take(2)).toPromise(); // once for pluginSetupDeps (sync) and twice for config (async)
}

public async pluginHasStarted(): Promise<boolean> {
public async pluginStartsUp(): Promise<boolean> {
return await this.getPluginStartDeps().then(() => true);
}

public pluginIsStarted() {
return this.pluginStartDeps !== undefined;
}

public setConfig(config: ReportingConfig) {
this.config = config;
this.pluginSetup$.next(true);
Expand Down
16 changes: 15 additions & 1 deletion x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { setFieldFormats } from './services';
import { ReportingSetup, ReportingSetupDeps, ReportingStart, ReportingStartDeps } from './types';
import { registerReportingUsageCollector } from './usage';

declare module 'src/core/server' {
interface RequestHandlerContext {
reporting?: ReportingCore | null;
}
}

export class ReportingPlugin
implements Plugin<ReportingSetup, ReportingStart, ReportingSetupDeps, ReportingStartDeps> {
private readonly initializerContext: PluginInitializerContext<ReportingConfigType>;
Expand All @@ -27,6 +33,14 @@ export class ReportingPlugin
}

public setup(core: CoreSetup, plugins: ReportingSetupDeps) {
core.http.registerRouteHandlerContext('reporting', () => {
if (this.reportingCore.pluginIsStarted()) {
return this.reportingCore;
} else {
return null;
}
});

const { elasticsearch, http } = core;
const { licensing, security } = plugins;
const { initializerContext: initContext, reportingCore } = this;
Expand Down Expand Up @@ -67,7 +81,7 @@ export class ReportingPlugin

// async background start
(async () => {
await this.reportingCore.pluginIsSetup();
await this.reportingCore.pluginSetsUp();
const config = reportingCore.getConfig();

const browserDriverFactory = await initializeBrowserDriverFactory(config, logger);
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/reporting/server/routes/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export function registerJobGenerationRoutes(reporting: ReportingCore, logger: Lo
* Generates enqueued job details to use in responses
*/
const handler: HandlerFunction = async (user, exportTypeId, jobParams, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return res.custom({ statusCode: 503, body: 'Not Available' });
}

const licenseInfo = await reporting.getLicenseInfo();
const licenseResults = licenseInfo[exportTypeId];

Expand Down
34 changes: 34 additions & 0 deletions x-pack/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface ListQuery {
}
const MAIN_ENTRY = `${API_BASE_URL}/jobs`;

const handleUnavailable = (res: any) => {
return res.custom({ statusCode: 503, body: 'Not Available' });
};

export function registerJobInfoRoutes(reporting: ReportingCore) {
const setupDeps = reporting.getPluginSetupDeps();
const userHandler = authorizedUserPreRoutingFactory(reporting);
Expand All @@ -34,6 +38,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
validate: false,
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return handleUnavailable(res);
}

const {
management: { jobTypes = [] },
} = await reporting.getLicenseInfo();
Expand Down Expand Up @@ -64,6 +73,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
validate: false,
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return handleUnavailable(res);
}

const {
management: { jobTypes = [] },
} = await reporting.getLicenseInfo();
Expand Down Expand Up @@ -91,6 +105,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
},
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return handleUnavailable(res);
}

const { docId } = req.params as { docId: string };
const {
management: { jobTypes = [] },
Expand Down Expand Up @@ -131,6 +150,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
},
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return res.custom({ statusCode: 503 });
}

const { docId } = req.params as { docId: string };
const {
management: { jobTypes = [] },
Expand Down Expand Up @@ -178,6 +202,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
},
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return handleUnavailable(res);
}

const { docId } = req.params as { docId: string };
const {
management: { jobTypes = [] },
Expand All @@ -199,6 +228,11 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
},
},
userHandler(async (user, context, req, res) => {
// ensure the async dependencies are loaded
if (!context.reporting) {
return handleUnavailable(res);
}

const { docId } = req.params as { docId: string };
const {
management: { jobTypes = [] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function registerReportingUsageCollector(
)
.toPromise();
};
const collectionIsReady = reporting.pluginHasStarted.bind(reporting);
const collectionIsReady = reporting.pluginStartsUp.bind(reporting);

const collector = getReportingUsageCollector(
reporting,
Expand Down

0 comments on commit 1136503

Please sign in to comment.