@dolittle/sdk.services
Version:
Dolittle is a decentralized, distributed, event-driven microservice platform built to harness the power of events.
101 lines • 9.46 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.ClientProcessor = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const sdk_protobuf_1 = require("@dolittle/sdk.protobuf");
const sdk_resilience_1 = require("@dolittle/sdk.resilience");
const RegistrationFailed_1 = require("./RegistrationFailed");
const ProcessorShouldNeverComplete_1 = require("./ProcessorShouldNeverComplete");
/**
* Defines a system for registering a processor that handles request from the Runtime.
* @template TIdentifier - The type of the identifier.
* @template TClient - The type of the gRPC client to use to connect.
* @template TRegisterArguments - The type of the registration arguments.
* @template TRegisterResponse - The type of the registration response.
* @template TRequest - The type of the requests.
* @template TResponse - The type of the responses.
*/
class ClientProcessor {
/**
* Initializes a new {@link ClientProcessor}.
* @param {string} _kind - What kind of a processor it is.
* @param {TIdentifier} _identifier - The unique identifier for the processor.
*/
constructor(_kind, _identifier) {
this._kind = _kind;
this._identifier = _identifier;
}
/**
* Registers a processor with the Runtime, and if successful starts handling requests.
* @param {TClient} client - The client to use to initiate the reverse call client.
* @param {ExecutionContext} executionContext - The base execution context for the processor.
* @param {ITenantServiceProviders} services - Used to resolve services while handling requests.
* @param {Logger} logger - Used for logging.
* @param {number} pingInterval - The ping interval to configure the processor with.
* @param {Cancellation} cancellation - Used to cancel the registration and processing.
* @returns {Observable} Representing the connection to the Runtime.
*/
register(client, executionContext, services, logger, pingInterval, cancellation) {
const reverseCallClient = this.createClient(client, this.registerArguments, (request, requestExecutionContext) => {
const tenantServiceProvider = services.forTenant(requestExecutionContext.tenantId);
return this.catchingHandle(request, requestExecutionContext, tenantServiceProvider, logger);
}, executionContext, pingInterval, logger, cancellation);
return reverseCallClient.pipe((0, operators_1.map)((registerResponse) => {
const pbFailure = this.getFailureFromRegisterResponse(registerResponse);
if (pbFailure !== undefined) {
const failure = sdk_protobuf_1.Failures.toSDK(pbFailure);
logger.error(`${this._kind} ${this._identifier} failed during registration. ${failure.reason}`);
throw new RegistrationFailed_1.RegistrationFailed(this._kind, this._identifier.value, failure);
}
else {
logger.info(`${this._kind} ${this._identifier} registered with the Runtime, start handling requests.`);
}
}), (0, operators_1.tap)({
error: (error) => {
if (error instanceof RegistrationFailed_1.RegistrationFailed)
return;
logger.error(`${this._kind} ${this._identifier} failed during processing of requests. ${error.message}`);
},
complete: () => {
logger.debug(`${this._kind} ${this._identifier} handling of requests completed.`);
}
}));
}
/**
* Registers a processor with a policy that specifies which and how to retry (reconnect) on errors.
* @param {RetryPolicy} policy - The policy to register with.
* @param {TClient} client - The client to use to initiate the reverse call client.
* @param {ExecutionContext} executionContext - The base execution context for the processor.
* @param {ITenantServiceProviders} services - Used to resolve services while handling requests.
* @param {Logger} logger - Used for logging.
* @param {number} pingInterval - The ping interval to configure the processor with.
* @param {Cancellation} cancellation - The cancellation.
* @returns {Observable} Repressenting the connection to the Runtime.
*/
registerWithPolicy(policy, client, executionContext, services, logger, pingInterval, cancellation) {
const registration = this.register(client, executionContext, services, logger, pingInterval, cancellation);
return (0, sdk_resilience_1.retryWithPolicy)(registration, policy, cancellation);
}
/**
* Registers a processor forever with a policy that specifies which and how to retry (reconnect) on errors.
* If the processor completes an error is thrown that will be handed over to the policy to deal with as any other errors.
* @param {RetryPolicy} policy - The policy to register with.
* @param {TClient} client - The client to use to initiate the reverse call client.
* @param {ExecutionContext} executionContext - The base execution context for the processor.
* @param {ITenantServiceProviders} services - Used to resolve services while handling requests.
* @param {Logger} logger - Used for logging.
* @param {number} pingInterval - The ping interval to configure the processor with.
* @param {Cancellation} cancellation - The cancellation.
* @returns {Observable} Repressenting the connection to the Runtime.
*/
registerForeverWithPolicy(policy, client, executionContext, services, logger, pingInterval, cancellation) {
const registration = this.register(client, executionContext, services, logger, pingInterval, cancellation);
const registrationThatFailsOnComplete = (0, rxjs_1.concat)(registration, (0, rxjs_1.throwError)(new ProcessorShouldNeverComplete_1.ProcessorShouldNeverComplete()));
return (0, sdk_resilience_1.retryWithPolicy)(registrationThatFailsOnComplete, policy, cancellation);
}
}
exports.ClientProcessor = ClientProcessor;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2xpZW50UHJvY2Vzc29yLmpzIiwic291cmNlUm9vdCI6Ii4uLyIsInNvdXJjZXMiOlsiQ2xpZW50UHJvY2Vzc29yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBK0M7QUFDL0MscUdBQXFHOzs7QUFFckcsK0JBQXNEO0FBQ3RELDhDQUEwQztBQVExQyx5REFBa0Q7QUFDbEQsNkRBQXNGO0FBS3RGLDZEQUEwRDtBQUMxRCxpRkFBOEU7QUFFOUU7Ozs7Ozs7O0dBUUc7QUFDSCxNQUFzQixlQUFlO0lBQ2pDOzs7O09BSUc7SUFDSCxZQUN1QixLQUFhLEVBQ2IsV0FBd0I7UUFEeEIsVUFBSyxHQUFMLEtBQUssQ0FBUTtRQUNiLGdCQUFXLEdBQVgsV0FBVyxDQUFhO0lBQzNDLENBQUM7SUFFTDs7Ozs7Ozs7O09BU0c7SUFDSCxRQUFRLENBQUMsTUFBZSxFQUFFLGdCQUFrQyxFQUFFLFFBQWlDLEVBQUUsTUFBYyxFQUFFLFlBQW9CLEVBQUUsWUFBMEI7UUFDN0osTUFBTSxpQkFBaUIsR0FBRyxJQUFJLENBQUMsWUFBWSxDQUN2QyxNQUFNLEVBQ04sSUFBSSxDQUFDLGlCQUFpQixFQUN0QixDQUFDLE9BQWlCLEVBQUUsdUJBQXlDLEVBQUUsRUFBRTtZQUM3RCxNQUFNLHFCQUFxQixHQUFHLFFBQVEsQ0FBQyxTQUFTLENBQUMsdUJBQXVCLENBQUMsUUFBUSxDQUFDLENBQUM7WUFDbkYsT0FBTyxJQUFJLENBQUMsY0FBYyxDQUFDLE9BQU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