Skip to content

Commit

Permalink
Swap right and left, wrap initializeService result
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmain authored and banderror committed Aug 15, 2021
1 parent 826276b commit 077ca24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface ConstructorOptions {
waitUntilReadyForWriting: Promise<WaitResult>;
}

export type WaitResult = Either<ElasticsearchClient, Error>;
export type WaitResult = Either<Error, ElasticsearchClient>;

export class RuleDataClient implements IRuleDataClient {
constructor(private readonly options: ConstructorOptions) {}
Expand All @@ -44,9 +44,9 @@ export class RuleDataClient implements IRuleDataClient {
const waitUntilReady = async () => {
const result = await this.options.waitUntilReadyForReading;
if (isLeft(result)) {
return result.left;
throw result.left;
} else {
throw result.right;
return result.right;
}
};

Expand Down Expand Up @@ -100,9 +100,9 @@ export class RuleDataClient implements IRuleDataClient {
const waitUntilReady = async () => {
const result = await this.options.waitUntilReadyForWriting;
if (isLeft(result)) {
return result.left;
throw result.left;
} else {
throw result.right;
return result.right;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { left, right } from 'fp-ts/lib/Either';
import { Either, isLeft, left, right } from 'fp-ts/lib/Either';

import { ElasticsearchClient, Logger } from 'kibana/server';

Expand All @@ -27,7 +27,7 @@ interface ConstructorOptions {
*/
export class RuleDataPluginService {
private readonly resourceInstaller: ResourceInstaller;
private installCommonResources: Promise<'ok' | Error>;
private installCommonResources: Promise<Either<Error, 'ok'>>;
private isInitialized: boolean;

constructor(private readonly options: ConstructorOptions) {
Expand All @@ -38,7 +38,7 @@ export class RuleDataPluginService {
isWriteEnabled: options.isWriteEnabled,
});

this.installCommonResources = Promise.resolve('ok');
this.installCommonResources = Promise.resolve(right('ok'));
this.isInitialized = false;
}

Expand Down Expand Up @@ -77,14 +77,12 @@ export class RuleDataPluginService {
*/
public initializeService(): void {
// Run the installation of common resources and handle exceptions.
// NOTE: this promise cannot reject, otherwise it will lead to an
// unhandled promise rejection shutting down Kibana process.
this.installCommonResources = this.resourceInstaller
.installCommonResources()
.then(() => 'ok')
.then(() => right('ok' as const))
.catch((e) => {
this.options.logger.error(e);
return e; // propagates it to the index initialization phase
return left(e); // propagates it to the index initialization phase
});

this.isInitialized = true;
Expand All @@ -110,27 +108,27 @@ export class RuleDataPluginService {
const waitUntilClusterClientAvailable = async (): Promise<WaitResult> => {
try {
const clusterClient = await this.options.getClusterClient();
return left(clusterClient);
return right(clusterClient);
} catch (e) {
this.options.logger.error(e);
return right(e);
return left(e);
}
};

const waitUntilIndexResourcesInstalled = async (): Promise<WaitResult> => {
try {
const result = await this.installCommonResources;
if (result !== 'ok') {
return right(result);
if (isLeft(result)) {
return result;
}

await this.resourceInstaller.installIndexLevelResources(indexInfo);

const clusterClient = await this.options.getClusterClient();
return left(clusterClient);
return right(clusterClient);
} catch (e) {
this.options.logger.error(e);
return right(e);
return left(e);
}
};

Expand Down

0 comments on commit 077ca24

Please sign in to comment.