forked from IntersectMBO/govtool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocks_api.js
More file actions
109 lines (101 loc) · 2.92 KB
/
locks_api.js
File metadata and controls
109 lines (101 loc) · 2.92 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { v4: uuidv4 } = require('uuid');
const lock = {};
function acquireLock(key, expiry_secs = 180) {
const now = Date.now();
if (!lock[key] || lock[key].expiry < now) {
const uuid = uuidv4();
lock[key] = {
locked: true,
expiry: now + expiry_secs * 1000,
uuid: uuid,
};
return uuid
}
}
function releaseLock(key,uuid) {
if(uuid){
_lock=lock[key]
if(_lock && (_lock.uuid != uuid)){
// if the uuid doesn't match, the lock should
// have expired and obtained by process.
return;
}
}
delete lock[key];
}
function setup(app) {
/**
* @swagger
* /lock/{key}:
* post:
* summary: Acquire lock
* tags: [Locks]
* parameters:
* - in: path
* name: key
* schema:
* type: string
* required: true
* description: The key of the lock to acquire
* - in: query
* name: expiry_secs
* schema:
* type: integer
* minimum: 1
* default: 180
* description: The expiration time of the lock in seconds (default is 180s)
* responses:
* '200':
* description: Lock acquired successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* uuid:
* type: string
* description: The UUID of the acquired lock
* '423':
* description: Lock not available
*/
app.post('/lock/:key', (req, res) => {
const key = req.params.key;
const expiry_secs = req.query.expiry_secs ? parseInt(req.query.expiry_secs) : 180;
const lock_uuid=acquireLock(key, expiry_secs)
if(lock_uuid){
res.json({ uuid: lock_uuid })
}else{
res.status(423).json({ status: 423, message: 'Lock not available' });
}
});
/**
* @swagger
* /unlock/{key}:
* post:
* summary: Release lock
* tags: [Locks]
* parameters:
* - in: path
* name: key
* schema:
* type: string
* required: true
* description: The key of the lock to release
* - in: query
* name: uuid
* schema:
* type: string
* required: false
* description: The UUID of the lock to release
* responses:
* '200':
* description: Lock released successfully
*/
app.post('/unlock/:key', (req, res) => {
const key = req.params.key;
const uuid = req.query.uuid;
releaseLock(key, uuid);
res.send('Lock released.');
});
}
module.exports.setup = setup;