@opengis/fastify-table
Version:
core-plugins
20 lines (19 loc) • 686 B
JavaScript
import { createHash } from "node:crypto";
import config from "../../../../config.js";
import getRedis from "../../redis/funcs/getRedis.js";
const md5 = (string) => createHash("md5").update(string).digest("hex");
const rclient = getRedis();
export default async function verifyUnique(name) {
if (!config.redis)
return true; // skip if redis is disabled
const cronId = config.port || 3000 + md5(name);
// one per node check
const key = `cron:unique:${cronId}`;
const unique = await rclient.setnx(key, 1);
const ttl = await rclient.ttl(key);
if (!unique && ttl !== -1) {
return false;
}
await rclient.expire(key, 20);
return true;
}