2023-01-13 05:40:33 +01:00
|
|
|
import { redisClient } from "../db/redis.js";
|
|
|
|
import { promisify } from "node:util";
|
|
|
|
import redisLock from "redis-lock";
|
2019-09-09 15:46:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retry delay (ms) for lock acquisition
|
|
|
|
*/
|
|
|
|
const retryDelay = 100;
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const lock: (key: string, timeout?: number) => Promise<() => void> = redisClient
|
2021-08-19 11:33:41 +02:00
|
|
|
? promisify(redisLock(redisClient, retryDelay))
|
2023-01-13 05:40:33 +01:00
|
|
|
: async () => () => {};
|
2019-09-09 15:46:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get AP Object lock
|
|
|
|
* @param uri AP object ID
|
|
|
|
* @param timeout Lock timeout (ms), The timeout releases previous lock.
|
|
|
|
* @returns Unlock function
|
|
|
|
*/
|
|
|
|
export function getApLock(uri: string, timeout = 30 * 1000) {
|
|
|
|
return lock(`ap-object:${uri}`, timeout);
|
|
|
|
}
|
2019-11-05 14:14:42 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function getFetchInstanceMetadataLock(
|
|
|
|
host: string,
|
|
|
|
timeout = 30 * 1000,
|
|
|
|
) {
|
2020-07-26 04:04:07 +02:00
|
|
|
return lock(`instance:${host}`, timeout);
|
2019-11-05 14:14:42 +01:00
|
|
|
}
|
2020-03-06 14:33:54 +01:00
|
|
|
|
|
|
|
export function getChartInsertLock(lockKey: string, timeout = 30 * 1000) {
|
|
|
|
return lock(`chart-insert:${lockKey}`, timeout);
|
|
|
|
}
|