@bazilio-san/af-stream
Version:
Data stream from database table
92 lines • 3.7 kB
JavaScript
import { createClient } from 'redis';
import { DateTime } from 'luxon';
import { getStreamKey, getTimeParamMillis, millis2iso, timeParamRE } from './utils/utils';
export 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 = createClient({ url });
this.client.on('error', (err) => {
console.error('Redis Client Error');
options.exitOnError(err);
});
const streamKey = 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 (!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: ${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 = 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 (timeParamRE.test(STREAM_START_BEFORE)) {
return Date.now() - 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 };
}
}
//# sourceMappingURL=StartTimeRedis.js.map