@bazilio-san/af-stream
Version:
Data stream from database table
96 lines • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartTimeRedis = void 0;
const redis_1 = require("redis");
const luxon_1 = require("luxon");
const utils_1 = require("./utils/utils");
class StartTimeRedis {
constructor(options) {
this.options = options;
const url = `redis://${options.host}:${options.port}`;
console.log(`[AF-STREAM]: Redis are expected at ${url}`);
this.client = (0, redis_1.createClient)({ url });
this.client.on('error', (err) => {
console.error('Redis Client Error');
options.exitOnError(err);
});
const streamKey = (0, utils_1.getStreamKey)(options.streamId);
this.streamKey = streamKey;
options.eventEmitter.on('save-last-ts', async ({ lastTs }) => {
const redisClient = await this.getRedisClient();
redisClient === null || redisClient === void 0 ? void 0 : redisClient.set(streamKey, lastTs).catch((err) => {
options.logger.error(err);
});
});
}
async getRedisClient() {
if (this.client.isOpen) {
return this.client;
}
try {
await this.client.connect();
}
catch (err) {
this.options.logger.error('Failed to initialize Redis client');
this.options.exitOnError(err);
}
if (!this.client.isOpen) {
this.options.exitOnError('Failed to initialize Redis client');
}
return this.client;
}
async getStartTimeFromRedis() {
const redisClient = await this.getRedisClient();
let startTime;
try {
startTime = await redisClient.get(this.streamKey);
}
catch (err) {
this.options.logger.error(err);
return 0;
}
startTime = Number(startTime);
if (!startTime) {
return 0;
}
if (!luxon_1.DateTime.fromMillis(startTime).isValid) {
this.options.logger.error(`Cache stored data is not a unix timestamp: ${startTime}`);
return 0;
}
this.options.logger.info(`Get time of last sent entry: ${(0, utils_1.millis2iso)(startTime, { includeOffset: true })} from the Redis cache using key ${this.streamKey}`);
return startTime;
}
// !!!Attention!!! STREAM_START_TIME - time in GMT
getStartTimeFromENV() {
const { logger } = this.options;
const { STREAM_START_TIME = '', STREAM_START_BEFORE = '' } = process.env;
const dt = luxon_1.DateTime.fromISO(STREAM_START_TIME, { zone: 'GMT' });
if (STREAM_START_TIME) {
if (dt.isValid) {
return dt.toMillis();
}
logger.error(`Start time is incorrect. STREAM_START_TIME: ${STREAM_START_TIME}`);
}
if (STREAM_START_BEFORE) {
if (utils_1.timeParamRE.test(STREAM_START_BEFORE)) {
return Date.now() - (0, utils_1.getTimeParamMillis)(STREAM_START_BEFORE);
}
logger.error(`Start time is incorrect. STREAM_START_BEFORE: ${STREAM_START_BEFORE}`);
}
return 0;
}
async getStartTime() {
// initialize connection with Redis to save state later
await this.getRedisClient();
let startTime = 0;
let isUsedSavedStartTime = false;
if (this.options.useStartTimeFromRedisCache) {
startTime = await this.getStartTimeFromRedis();
isUsedSavedStartTime = !!startTime;
}
startTime = startTime || this.getStartTimeFromENV() || Date.now();
return { isUsedSavedStartTime, startTime };
}
}
exports.StartTimeRedis = StartTimeRedis;
//# sourceMappingURL=StartTimeRedis.js.map