2021-08-19 04:33:41 -05:00
|
|
|
import { redisClient } from '../db/redis.js';
|
2019-09-09 08:46:45 -05:00
|
|
|
import { promisify } from 'util';
|
2021-08-19 04:33:41 -05:00
|
|
|
import * as redisLock from 'redis-lock';
|
2019-09-09 08:46:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retry delay (ms) for lock acquisition
|
|
|
|
*/
|
|
|
|
const retryDelay = 100;
|
|
|
|
|
|
|
|
const lock: (key: string, timeout?: number) => Promise<() => void>
|
2021-03-23 00:54:09 -05:00
|
|
|
= redisClient
|
2021-08-19 04:33:41 -05:00
|
|
|
? promisify(redisLock(redisClient, retryDelay))
|
2019-09-09 08:46:45 -05:00
|
|
|
: async () => () => { };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 07:14:42 -06:00
|
|
|
|
2020-07-25 21:04:07 -05:00
|
|
|
export function getFetchInstanceMetadataLock(host: string, timeout = 30 * 1000) {
|
|
|
|
return lock(`instance:${host}`, timeout);
|
2019-11-05 07:14:42 -06:00
|
|
|
}
|
2020-03-06 07:33:54 -06:00
|
|
|
|
|
|
|
export function getChartInsertLock(lockKey: string, timeout = 30 * 1000) {
|
|
|
|
return lock(`chart-insert:${lockKey}`, timeout);
|
|
|
|
}
|