|
| 1 | +// Copyright (c) Dolittle. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +import { Logger } from 'winston'; |
| 5 | + |
| 6 | +import { ExecutionContext, TenantId } from '@dolittle/sdk.execution'; |
| 7 | +import { ExecutionContexts } from '@dolittle/sdk.protobuf'; |
| 8 | +import { Cancellation } from '@dolittle/sdk.resilience'; |
| 9 | +import { reactiveUnary, UnaryMethod } from '@dolittle/sdk.services'; |
| 10 | + |
| 11 | +import { Failure } from '@dolittle/contracts/Protobuf/Failure_pb'; |
| 12 | +import { CallRequestContext } from '@dolittle/contracts/Services/CallContext_pb'; |
| 13 | +import { ResourcesClient } from '@dolittle/runtime.contracts/Resources/Resources_grpc_pb'; |
| 14 | + |
| 15 | +import { FailedToGetResourceForTenant } from '../FailedToGetResourceForTenant'; |
| 16 | +import { ResourceName } from '../ResourceName'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Represents a system that can create resources by fetching configuration from the Runtime. |
| 20 | + * @template TResource - The type of the resource. |
| 21 | + * @template TRequest - The type of the resource configuration request. |
| 22 | + * @template TResponse - The type of the resource configuration response. |
| 23 | + */ |
| 24 | +export abstract class ResourceCreator<TResource, TRequest, TResponse> { |
| 25 | + |
| 26 | + /** |
| 27 | + * Initialises a new instance of the {@link ResourceCreator} class. |
| 28 | + * @param {ResourceName} _resource - The name of the resource type. |
| 29 | + * @param {UnaryMethod} _method - The gRPC method to call to get the resource configuration from the Runtime. |
| 30 | + * @param {ResourcesClient} _client - The resources client to make requests to the Runtime with. |
| 31 | + * @param {ExecutionContext} _executionContext - The base execution context for the client. |
| 32 | + * @param {Logger} _logger - The logger to use for logging. |
| 33 | + */ |
| 34 | + protected constructor( |
| 35 | + private readonly _resource: ResourceName, |
| 36 | + private readonly _method: UnaryMethod<TRequest, TResponse>, |
| 37 | + private readonly _client: ResourcesClient, |
| 38 | + private readonly _executionContext: ExecutionContext, |
| 39 | + private readonly _logger: Logger, |
| 40 | + ) {} |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates the resource for the provided tenant by fetching configuration from the Runtime. |
| 44 | + * @param {TenantId} tenant - The tenant id to create the resource for. |
| 45 | + * @param {Cancellation} cancellation - An optional cancellation to cancel the operation. |
| 46 | + * @returns {Promise} - A {@link Promise} that, when resolved, returns the created resource. |
| 47 | + */ |
| 48 | + async createFor(tenant: TenantId, cancellation: Cancellation = Cancellation.default): Promise<TResource> { |
| 49 | + try { |
| 50 | + this._logger.debug(`Getting ${this._resource} resource for tenant ${tenant}`); |
| 51 | + |
| 52 | + const executionContext = this._executionContext.forTenant(tenant); |
| 53 | + const callContext = ExecutionContexts.toCallContext(executionContext); |
| 54 | + |
| 55 | + const request = this.createResourceRequest(callContext); |
| 56 | + const response = await reactiveUnary(this._client, this._method, request, cancellation).toPromise(); |
| 57 | + |
| 58 | + const [requestFailed, requestFailure] = this.requestFailed(response); |
| 59 | + if (requestFailed) { |
| 60 | + this._logger.warn(`Failed getting ${this._resource} resource for tenant ${tenant} because ${requestFailure!.getReason()}`); |
| 61 | + throw new FailedToGetResourceForTenant(this._resource, tenant, requestFailure!.getReason()); |
| 62 | + } |
| 63 | + |
| 64 | + return await this.createResourceFrom(response); |
| 65 | + } catch (error) { |
| 66 | + if (error instanceof FailedToGetResourceForTenant) { |
| 67 | + throw error; |
| 68 | + } |
| 69 | + |
| 70 | + this._logger.warn(`Failed getting ${this._resource} resource for tenant ${tenant}}`, error); |
| 71 | + throw new FailedToGetResourceForTenant(this._resource, tenant, error as any); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a request to get the resource configuration using the provided call context. |
| 77 | + * @param {CallRequestContext} callContext - The call context to use for the request containing the tenant id. |
| 78 | + * @returns {TRequest} A new request. |
| 79 | + */ |
| 80 | + protected abstract createResourceRequest(callContext: CallRequestContext): TRequest; |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks whether the request failed based on the response. |
| 84 | + * @param {TResponse} response - The response received from the Runtime. |
| 85 | + * @returns {[false] | [true, Failure]} False if the request succeeded, true and the failure if it failed. |
| 86 | + */ |
| 87 | + protected abstract requestFailed(response: TResponse): [false] | [true, Failure]; |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates the resource from the configuration received from the Runtime. |
| 91 | + * @param {TResponse} response - The response received from the Runtime. |
| 92 | + * @returns {Promise<TResource>} - A {@link Promise} that, when resolved, returns the created resource. |
| 93 | + */ |
| 94 | + protected abstract createResourceFrom(response: TResponse): Promise<TResource>; |
| 95 | +} |
0 commit comments