Skip to content

Commit

Permalink
make index template retry
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Feb 14, 2022
1 parent 54de36f commit ebb2f85
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
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

0 comments on commit ebb2f85

Please sign in to comment.