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