diff --git a/x-pack/plugins/uptime/server/lib/synthetics_service/hydrate_saved_object.ts b/x-pack/plugins/uptime/server/lib/synthetics_service/hydrate_saved_object.ts index 2e98b62ddee66..08e2934a4ac08 100644 --- a/x-pack/plugins/uptime/server/lib/synthetics_service/hydrate_saved_object.ts +++ b/x-pack/plugins/uptime/server/lib/synthetics_service/hydrate_saved_object.ts @@ -18,39 +18,43 @@ export const hydrateSavedObjects = async ({ monitors: SyntheticsMonitorSavedObject[]; server: UptimeServerSetup; }) => { - const missingUrlInfoIds: string[] = []; + try { + const missingUrlInfoIds: string[] = []; - monitors - .filter((monitor) => monitor.attributes.type === 'browser') - .forEach(({ attributes, id }) => { - const monitor = attributes as MonitorFields; - if (!monitor || !monitor.urls) { - missingUrlInfoIds.push(id); - } - }); + monitors + .filter((monitor) => monitor.attributes.type === 'browser') + .forEach(({ attributes, id }) => { + const monitor = attributes as MonitorFields; + if (!monitor || !monitor.urls) { + missingUrlInfoIds.push(id); + } + }); - if (missingUrlInfoIds.length > 0 && server.uptimeEsClient) { - const esDocs: Ping[] = await fetchSampleMonitorDocuments( - server.uptimeEsClient, - missingUrlInfoIds - ); - const updatedObjects = monitors - .filter((monitor) => missingUrlInfoIds.includes(monitor.id)) - .map((monitor) => { - let url = ''; - esDocs.forEach((doc) => { - // to make sure the document is ingested after the latest update of the monitor - const diff = moment(monitor.updated_at).diff(moment(doc.timestamp), 'minutes'); - if (doc.config_id === monitor.id && doc.url?.full && diff > 1) { - url = doc.url?.full; + if (missingUrlInfoIds.length > 0 && server.uptimeEsClient) { + const esDocs: Ping[] = await fetchSampleMonitorDocuments( + server.uptimeEsClient, + missingUrlInfoIds + ); + const updatedObjects = monitors + .filter((monitor) => missingUrlInfoIds.includes(monitor.id)) + .map((monitor) => { + let url = ''; + esDocs.forEach((doc) => { + // to make sure the document is ingested after the latest update of the monitor + const diff = moment(monitor.updated_at).diff(moment(doc.timestamp), 'minutes'); + if (doc.config_id === monitor.id && doc.url?.full && diff > 1) { + url = doc.url?.full; + } + }); + if (url) { + return { ...monitor, attributes: { ...monitor.attributes, urls: url } }; } + return monitor; }); - if (url) { - return { ...monitor, attributes: { ...monitor.attributes, urls: url } }; - } - return monitor; - }); - await server.authSavedObjectsClient?.bulkUpdate(updatedObjects); + await server.authSavedObjectsClient?.bulkUpdate(updatedObjects); + } + } catch (e) { + server.logger.error(e); } }; diff --git a/x-pack/plugins/uptime/server/lib/synthetics_service/synthetics_service.ts b/x-pack/plugins/uptime/server/lib/synthetics_service/synthetics_service.ts index 450ab324e7e48..257a6881a7388 100644 --- a/x-pack/plugins/uptime/server/lib/synthetics_service/synthetics_service.ts +++ b/x-pack/plugins/uptime/server/lib/synthetics_service/synthetics_service.ts @@ -51,6 +51,9 @@ export class SyntheticsService { public locations: ServiceLocations; + private indexTemplateExists?: boolean; + private indexTemplateInstalling?: boolean; + constructor(logger: Logger, server: UptimeServerSetup, config: ServiceConfig) { this.logger = logger; this.server = server; @@ -70,23 +73,34 @@ export class SyntheticsService { // this.apiKey = apiKey; // } // }); - this.setupIndexTemplates(); } private setupIndexTemplates() { - installSyntheticsIndexTemplates(this.server).then( - (result) => { - if (result.name === 'synthetics' && result.install_status === 'installed') { - this.logger.info('Installed synthetics index templates'); - } else if (result.name === 'synthetics' && result.install_status === 'install_failed') { + if (this.indexTemplateExists) { + // if already installed, don't need to reinstall + return; + } + + if (!this.indexTemplateInstalling) { + installSyntheticsIndexTemplates(this.server).then( + (result) => { + this.indexTemplateInstalling = false; + if (result.name === 'synthetics' && result.install_status === 'installed') { + this.logger.info('Installed synthetics index templates'); + this.indexTemplateExists = true; + } else if (result.name === 'synthetics' && result.install_status === 'install_failed') { + this.logger.warn(new IndexTemplateInstallationError()); + this.indexTemplateExists = false; + } + }, + () => { + this.indexTemplateInstalling = false; this.logger.warn(new IndexTemplateInstallationError()); } - }, - () => { - this.logger.warn(new IndexTemplateInstallationError()); - } - ); + ); + this.indexTemplateInstalling = true; + } } public registerSyncTask(taskManager: TaskManagerSetupContract) { @@ -106,6 +120,8 @@ export class SyntheticsService { async run() { const { state } = taskInstance; + service.setupIndexTemplates(); + getServiceLocations(service.server).then((result) => { service.locations = result.locations; service.apiClient.locations = result.locations; @@ -282,10 +298,13 @@ export class SyntheticsService { namespaces: ['*'], }); - hydrateSavedObjects({ - monitors: findResult.saved_objects as unknown as SyntheticsMonitorSavedObject[], - server: this.server, - }); + if (this.indexTemplateExists) { + // without mapping, querying won't make sense + hydrateSavedObjects({ + monitors: findResult.saved_objects as unknown as SyntheticsMonitorSavedObject[], + server: this.server, + }); + } return (findResult.saved_objects ?? []).map(({ attributes, id }) => ({ ...attributes,