browsertrix-crawler/src/util/redis.ts
Ilya Kreymer af1e0860e4
TypeScript Conversion (#425)
Follows #424. Converts the upcoming 1.0.0 branch based on native browser-based traffic capture and recording to TypeScript. Fixes #426

---------
Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
Co-authored-by: emma <hi@emma.cafe>
2023-11-09 11:27:11 -08:00

40 lines
947 B
TypeScript

import { Redis } from "ioredis";
import { logger } from "./logger.js";
const error = console.error;
let lastLogTime = 0;
let exitOnError = false;
// log only once every 10 seconds
const REDIS_ERROR_LOG_INTERVAL_SECS = 10000;
console.error = function (...args) {
if (
typeof args[0] === "string" &&
args[0].indexOf("[ioredis] Unhandled error event") === 0
) {
const now = Date.now();
if ((now - lastLogTime) > REDIS_ERROR_LOG_INTERVAL_SECS) {
if (lastLogTime && exitOnError) {
logger.fatal("Crawl interrupted, redis gone, exiting", {}, "redis");
}
logger.warn("ioredis error", {error: args[0]}, "redis");
lastLogTime = now;
}
return;
}
error.call(console, ...args);
};
export async function initRedis(url: string) {
const redis = new Redis(url, {lazyConnect: true});
await redis.connect();
return redis;
}
export function setExitOnRedisError() {
exitOnError = true;
}