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

allows Alerts to recover gracefully from Executor errors #53688

Merged
merged 16 commits into from
Jan 6, 2020
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 @@ -5,7 +5,7 @@
*/

import { State, Context } from '../types';
import { parseDuration } from './parse_duration';
import { parseDuration } from '../lib';

interface Meta {
lastScheduledActions?: {
Expand Down
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/alerting/server/alert_instance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { AlertInstance } from './alert_instance';
export { createAlertInstanceFactory } from './create_alert_instance_factory';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TaskRunnerFactory } from './lib';
import { TaskRunnerFactory } from './task_runner';
import { AlertTypeRegistry } from './alert_type_registry';
import { taskManagerMock } from '../../task_manager/server/task_manager.mock';

Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import { TaskRunnerFactory } from './lib';
import { RunContext } from '../../task_manager/server';
import { TaskRunnerFactory } from './task_runner';
import { RunContext } from '../../task_manager';
import { TaskManagerSetupContract } from './shim';
import { AlertType } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { Request } from 'hapi';
import { AlertsClientFactory, ConstructorOpts } from './alerts_client_factory';
import { alertTypeRegistryMock } from '../alert_type_registry.mock';
import { taskManagerMock } from '../../../task_manager/server/task_manager.mock';
import { KibanaRequest } from '../../../../../../src/core/server';
import { loggingServiceMock } from '../../../../../../src/core/server/mocks';
import { encryptedSavedObjectsMock } from '../../../../../plugins/encrypted_saved_objects/server/mocks';
import { alertTypeRegistryMock } from './alert_type_registry.mock';
import { taskManagerMock } from '../../task_manager/server/task_manager.mock';
import { KibanaRequest } from '../../../../../src/core/server';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { encryptedSavedObjectsMock } from '../../../../plugins/encrypted_saved_objects/server/mocks';

jest.mock('../alerts_client');
jest.mock('./alerts_client');

const savedObjectsClient = jest.fn();
const securityPluginSetup = {
Expand Down Expand Up @@ -55,7 +55,7 @@ test('creates an alerts client with proper constructor arguments', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);

expect(jest.requireMock('../alerts_client').AlertsClient).toHaveBeenCalledWith({
expect(jest.requireMock('./alerts_client').AlertsClient).toHaveBeenCalledWith({
savedObjectsClient,
logger: alertsClientFactoryParams.logger,
taskManager: alertsClientFactoryParams.taskManager,
Expand All @@ -72,7 +72,7 @@ test('creates an alerts client with proper constructor arguments', async () => {
test('getUserName() returns null when security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

const userNameResult = await constructorCall.getUserName();
expect(userNameResult).toEqual(null);
Expand All @@ -84,7 +84,7 @@ test('getUserName() returns a name when security is enabled', async () => {
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.getCurrentUser.mockResolvedValueOnce({ username: 'bob' });
const userNameResult = await constructorCall.getUserName();
Expand All @@ -94,7 +94,7 @@ test('getUserName() returns a name when security is enabled', async () => {
test('createAPIKey() returns { apiKeysEnabled: false } when security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

const createAPIKeyResult = await constructorCall.createAPIKey();
expect(createAPIKeyResult).toEqual({ apiKeysEnabled: false });
Expand All @@ -103,7 +103,7 @@ test('createAPIKey() returns { apiKeysEnabled: false } when security is disabled
test('createAPIKey() returns { apiKeysEnabled: false } when security is enabled but ES security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce(null);
const createAPIKeyResult = await constructorCall.createAPIKey();
Expand All @@ -116,7 +116,7 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce({ api_key: '123', id: 'abc' });
const createAPIKeyResult = await constructorCall.createAPIKey();
Expand All @@ -132,7 +132,7 @@ test('createAPIKey() throws when security plugin createAPIKey throws an error',
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockRejectedValueOnce(new Error('TLS disabled'));
await expect(constructorCall.createAPIKey()).rejects.toThrowErrorMatchingInlineSnapshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import Hapi from 'hapi';
import uuid from 'uuid';
import { AlertsClient } from '../alerts_client';
import { AlertTypeRegistry, SpaceIdToNamespaceFunction } from '../types';
import { SecurityPluginStartContract, TaskManagerStartContract } from '../shim';
import { KibanaRequest, Logger } from '../../../../../../src/core/server';
import { InvalidateAPIKeyParams } from '../../../../../plugins/security/server';
import { PluginStartContract as EncryptedSavedObjectsStartContract } from '../../../../../plugins/encrypted_saved_objects/server';
import { AlertsClient } from './alerts_client';
import { AlertTypeRegistry, SpaceIdToNamespaceFunction } from './types';
import { SecurityPluginStartContract, TaskManagerStartContract } from './shim';
import { KibanaRequest, Logger } from '../../../../../src/core/server';
import { InvalidateAPIKeyParams } from '../../../../plugins/security/server';
import { PluginStartContract as EncryptedSavedObjectsStartContract } from '../../../../plugins/encrypted_saved_objects/server';

export interface ConstructorOpts {
logger: Logger;
Expand Down
6 changes: 2 additions & 4 deletions x-pack/legacy/plugins/alerting/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { AlertInstance } from './alert_instance';
export { validateAlertTypeParams } from './validate_alert_type_params';
export { parseDuration, getDurationSchema } from './parse_duration';
export { AlertsClientFactory } from './alerts_client_factory';
export { TaskRunnerFactory } from './task_runner_factory';
export { LicenseState } from './license_state';
export { validateAlertTypeParams } from './validate_alert_type_params';
54 changes: 54 additions & 0 deletions x-pack/legacy/plugins/alerting/server/lib/result_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export interface Ok<T> {
tag: 'ok';
value: T;
}

export interface Err<E> {
tag: 'err';
error: E;
}
export type Result<T, E> = Ok<T> | Err<E>;

export function asOk<T>(value: T): Ok<T> {
return {
tag: 'ok',
value,
};
}

export function asErr<T>(error: T): Err<T> {
return {
tag: 'err',
error,
};
}

export function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
return result.tag === 'ok';
}

export function isErr<T, E>(result: Result<T, E>): result is Err<E> {
return !isOk(result);
}

export async function promiseResult<T, E>(future: Promise<T>): Promise<Result<T, E>> {
try {
return asOk(await future);
} catch (e) {
return asErr(e);
}
}

export function map<T, E, Resolution>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map doesn't seem like a great name for this function; processResult(), handleResult(), something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a standard name for this operation in FP... it's mapping the Result type, be it an Ok or an Err into a specific type.
And TBH I don't feel process and handle tell me anything about what is happening here.

result: Result<T, E>,
onOk: (value: T) => Resolution,
onErr: (error: E) => Resolution
): Resolution {
return isOk(result) ? onOk(result.value) : onErr(result.error);
}
Loading