@cdp-forge/plugin-pipeline-sdk
Version:
SDK for pipeline plugins for CDP Forge platform
82 lines • 2.94 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pulsar_client_1 = __importDefault(require("pulsar-client"));
class PipelineSTage {
constructor(plugin, config) {
this.consumer = null;
this.producer = null;
this.plugin = plugin;
this.pulsar = new pulsar_client_1.default.Client({
serviceUrl: config.pulsar.proxy
});
this.config = config;
this.input = null;
this.output = null;
this.currentOperation = Promise.resolve();
}
async start(inputTopic, outputTopic = null) {
this.input = inputTopic;
this.output = outputTopic;
this.currentOperation = this.currentOperation.then(() => this.plugin.init());
await this.currentOperation;
this.currentOperation = this._start();
return this.currentOperation;
}
async _start() {
if (this.output) {
this.producer = await this.pulsar.createProducer({
topic: this.output,
producerName: this.config.plugin.name + "-" + this.config.pod.name
});
}
this.consumer = await this.pulsar.subscribe({
topic: this.input,
subscription: this.config.plugin.name,
subscriptionType: 'Shared',
listener: async (msg, msgConsumer) => {
console.log(msg.getData().toString());
const log = JSON.parse(msg.getData().toString());
if (!log)
return;
const elaboratedLog = await this.plugin.elaborate(log);
if (!elaboratedLog) {
console.warn('Messaggio non elaborato');
await msgConsumer.acknowledge(msg);
return;
}
if (this.output && this.producer) {
await this.producer.send({
data: Buffer.from(JSON.stringify(elaboratedLog)),
});
}
await msgConsumer.acknowledge(msg);
},
});
}
async stop() {
await this.currentOperation;
this.currentOperation = this._stop();
return this.currentOperation;
}
async _stop() {
await this.consumer?.close();
await this.producer?.close();
this.consumer = null;
this.producer = null;
}
async restart(inputTopic, outputTopic) {
if (this.input === inputTopic && this.output === outputTopic)
return;
await this.stop();
await this.start(inputTopic, outputTopic);
}
async close() {
await this.stop();
await this.pulsar.close();
}
}
exports.default = PipelineSTage;
//# sourceMappingURL=PipelineStage.js.map