Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down