@playtini/beta7
Version:
Microframework for Microservices
79 lines • 3.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ServiceBroker_1 = require("../../ServiceBroker");
class RecoveryService {
constructor(data) {
this.serviceBroker = new ServiceBroker_1.ServiceBroker({
nodeID: `recovery-${data.nodeID}`,
kafkaBrokers: data.kafkaBrokers,
schemaRegistry: data.schemaRegistry,
});
this.groupID = `recovery-${data.nodeID}`;
this.kafkaAdmin = this.serviceBroker.kafka.admin();
this.kafkaConsumer = this.serviceBroker.kafka.consumer({
groupId: this.groupID,
allowAutoTopicCreation: true,
});
this.restoreTopic = data.restoreTopic;
this.serviceBroker.debug.info('class created');
}
async start(handler) {
this.serviceBroker.debug.info('starting script');
await this.kafkaAdmin.connect();
this.serviceBroker.debug.info('kafka admin connected');
await this.kafkaAdmin.createTopics({
topics: [{ topic: this.restoreTopic }],
});
this.serviceBroker.debug.info('topics created');
await this.kafkaAdmin.resetOffsets({
groupId: this.groupID,
topic: this.restoreTopic,
earliest: true,
});
this.serviceBroker.debug.info('offsets reset');
await new Promise(async (res) => {
await this.kafkaConsumer.connect();
this.serviceBroker.debug.info('kafka consumer connected');
await this.kafkaConsumer.subscribe({
topic: this.restoreTopic,
fromBeginning: true,
});
this.serviceBroker.debug.info('kafka consumer subscribed');
const topicOffsets = await this.kafkaAdmin.fetchTopicOffsets(this.restoreTopic);
this.serviceBroker.debug.info(`topicOffsets: ${JSON.stringify(topicOffsets)}`);
const totalCount = topicOffsets === null || topicOffsets === void 0 ? void 0 : topicOffsets.reduce((prev, curr) => prev + (Number(curr.high) - Number(curr.low)), 0);
this.serviceBroker.debug.info(`total number of events to read: ${totalCount}`);
let currentStep = 0;
console.time('RECOVERY_SCRIPT');
if (totalCount === currentStep) {
res();
}
else {
await this.kafkaConsumer.run({
eachBatchAutoResolve: true,
eachBatch: async ({ batch }) => {
this.serviceBroker.debug.info(`received batch with ${batch.messages.length} messages`);
for (let message of batch.messages) {
if (message.value) {
const payload = await this.serviceBroker.schemaRegistry.decode(message.value);
await handler(payload);
}
currentStep += 1;
if (totalCount === currentStep) {
res();
}
}
},
});
}
});
this.serviceBroker.debug.info('script successfully completed');
console.timeEnd('RECOVERY_SCRIPT');
await this.kafkaAdmin.disconnect();
await this.kafkaConsumer.disconnect();
this.serviceBroker.debug.info('exiting script');
process.exit();
}
}
exports.default = RecoveryService;
//# sourceMappingURL=RecoveryService.js.map