@dolittle/sdk.resources
Version:
Dolittle is a decentralized, distributed, event-driven microservice platform built to harness the power of events.
62 lines • 5.67 kB
JavaScript
;
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourceCreator = void 0;
const sdk_protobuf_1 = require("@dolittle/sdk.protobuf");
const sdk_resilience_1 = require("@dolittle/sdk.resilience");
const sdk_services_1 = require("@dolittle/sdk.services");
const FailedToGetResourceForTenant_1 = require("../FailedToGetResourceForTenant");
/**
* Represents a system that can create resources by fetching configuration from the Runtime.
* @template TResource - The type of the resource.
* @template TRequest - The type of the resource configuration request.
* @template TResponse - The type of the resource configuration response.
*/
class ResourceCreator {
/**
* Initialises a new instance of the {@link ResourceCreator} class.
* @param {ResourceName} _resource - The name of the resource type.
* @param {UnaryMethod} _method - The gRPC method to call to get the resource configuration from the Runtime.
* @param {ResourcesClient} _client - The resources client to make requests to the Runtime with.
* @param {ExecutionContext} _executionContext - The base execution context for the client.
* @param {Logger} _logger - The logger to use for logging.
*/
constructor(_resource, _method, _client, _executionContext, _logger) {
this._resource = _resource;
this._method = _method;
this._client = _client;
this._executionContext = _executionContext;
this._logger = _logger;
}
/**
* Creates the resource for the provided tenant by fetching configuration from the Runtime.
* @param {TenantId} tenant - The tenant id to create the resource for.
* @param {Cancellation} cancellation - An optional cancellation to cancel the operation.
* @returns {Promise} - A {@link Promise} that, when resolved, returns the created resource.
*/
async createFor(tenant, cancellation = sdk_resilience_1.Cancellation.default) {
try {
this._logger.debug(`Getting ${this._resource} resource for tenant ${tenant}`);
const executionContext = this._executionContext.forTenant(tenant);
const callContext = sdk_protobuf_1.ExecutionContexts.toCallContext(executionContext);
const request = this.createResourceRequest(callContext);
const response = await (0, sdk_services_1.reactiveUnary)(this._client, this._method, request, cancellation).toPromise();
const [requestFailed, requestFailure] = this.requestFailed(response);
if (requestFailed) {
this._logger.warn(`Failed getting ${this._resource} resource for tenant ${tenant} because ${requestFailure.getReason()}`);
throw new FailedToGetResourceForTenant_1.FailedToGetResourceForTenant(this._resource, tenant, requestFailure.getReason());
}
return await this.createResourceFrom(response);
}
catch (error) {
if (error instanceof FailedToGetResourceForTenant_1.FailedToGetResourceForTenant) {
throw error;
}
this._logger.warn(`Failed getting ${this._resource} resource for tenant ${tenant}}`, error);
throw new FailedToGetResourceForTenant_1.FailedToGetResourceForTenant(this._resource, tenant, error);
}
}
}
exports.ResourceCreator = ResourceCreator;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUmVzb3VyY2VDcmVhdG9yLmpzIiwic291cmNlUm9vdCI6Ii4uLyIsInNvdXJjZXMiOlsiSW50ZXJuYWwvUmVzb3VyY2VDcmVhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBK0M7QUFDL0MscUdBQXFHOzs7QUFLckcseURBQTJEO0FBQzNELDZEQUF3RDtBQUN4RCx5REFBb0U7QUFNcEUsa0ZBQStFO0FBRy9FOzs7OztHQUtHO0FBQ0gsTUFBc0IsZUFBZTtJQUVqQzs7Ozs7OztPQU9HO0lBQ0gsWUFDcUIsU0FBdUIsRUFDdkIsT0FBeUMsRUFDekMsT0FBd0IsRUFDeEIsaUJBQW1DLEVBQ25DLE9BQWU7UUFKZixjQUFTLEdBQVQsU0FBUyxDQUFjO1FBQ3ZCLFlBQU8sR0FBUCxPQUFPLENBQWtDO1FBQ3pDLFlBQU8sR0FBUCxPQUFPLENBQWlCO1FBQ3hCLHNCQUFpQixHQUFqQixpQkFBaUIsQ0FBa0I7UUFDbkMsWUFBTyxHQUFQLE9BQU8sQ0FBUTtJQUNqQyxDQUFDO0lBRUo7Ozs7O09BS0c7SUFDSCxLQUFLLENBQUMsU0FBUyxDQUFDLE1BQWdCLEVBQUUsZUFBNkIsNkJBQVksQ0FBQyxPQUFPO1FBQy9FLElBQUk7WUFDQSxJQUFJLENBQUMsT0FBTyxDQUF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