summaryrefslogtreecommitdiff
path: root/src/misc/app-lock.ts
blob: 847299b46da215bed8f75d4f3d8a6588dba0f6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}

export function getFetchInstanceMetadataLock(host: string, timeout = 30 * 1000) {
	return lock(`instance:${host}`, timeout);
}

export function getChartInsertLock(lockKey: string, timeout = 30 * 1000) {
	return lock(`chart-insert:${lockKey}`, timeout);
}