[Uptime monitor management] Make index template installation retry (#125537)

This commit is contained in:
Shahzad 2022-02-15 10:06:19 +01:00 committed by GitHub
parent 75ac8e515b
commit 7c2437f5a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 45 deletions

View file

@ -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);
}
});
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 } };
monitors
.filter((monitor) => monitor.attributes.type === 'browser')
.forEach(({ attributes, id }) => {
const monitor = attributes as MonitorFields;
if (!monitor || !monitor.urls) {
missingUrlInfoIds.push(id);
}
return monitor;
});
await server.authSavedObjectsClient?.bulkUpdate(updatedObjects);
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;
});
await server.authSavedObjectsClient?.bulkUpdate(updatedObjects);
}
} catch (e) {
server.logger.error(e);
}
};

View file

@ -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;
@ -283,10 +299,13 @@ export class SyntheticsService {
perPage: 10000,
});
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,