-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (102 loc) · 3.93 KB
/
index.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
function documentLock(schema, options) {
options = options || {lockColumnNames: ["lockExpirationDate"]};
//in ms
var LOCK_TIME_FIRE = options.lockTimeFire || 1000;
if (options.lockColumnNames.length == 0) {
throw new Error("Can't initiate document lock without lock column.")
}
var data = {};
options.lockColumnNames.forEach(function(columnName) {
data[columnName] = Date;
});
schema.add(data);
function exec(query, update, columnName, expirationDate, callback) {
var self = this;
self.constructor.update(query, update, function(err, numAffected) {
if (err) {
return callback(err)
}
if (numAffected != 1) {
return callback(new Error("Unable to take lock."));
}
self.set(columnName, expirationDate);
callback();
});
}
function forceLock(columnName, callback) {
var expirationDate = new Date();
expirationDate.setSeconds(expirationDate.getSeconds() + LOCK_TIME_FIRE / 1000);
var query = {_id: this._id};
var update = {$set: {}};
update["$set"][columnName] = expirationDate;
exec.call(this, query, update, columnName, expirationDate, callback)
};
schema.methods.takeLock = function takeLock(columnName, callback) {
var expirationDate = new Date();
expirationDate.setSeconds(expirationDate.getSeconds() + LOCK_TIME_FIRE / 1000);
var maxDBExpirationDate = new Date();
var query1 = {_id: this._id};
var query2 = {_id: this._id};
query1[columnName] = null;
query2[columnName] = {$lt: maxDBExpirationDate};
var query = {$or: [query1, query2]};
var update = {$set: {}};
update["$set"][columnName] = expirationDate;
exec.call(this, query, update, columnName, expirationDate, callback)
};
schema.methods.getLock = function getLock(columnName, callback) {
if (typeof columnName == "function") {
callback = columnName;
columnName = options.lockColumnNames[0]
}
var self = this;
self.takeLock(columnName, function(err) {
if (err) {
return callback(err)
}
if (!self.lockTimer) {
self.lockTimer = {}
}
self.lockTimer[columnName] = setInterval(function() {
forceLock.call(self, columnName, function() {})
}, LOCK_TIME_FIRE / 2);
callback();
})
};
schema.methods.releaseLock = function releaseLock(columnName, callback) {
if (typeof columnName == "function") {
callback = columnName
columnName = options.lockColumnNames[0]
}
var self = this;
if (!self.lockTimer || !self.lockTimer[columnName]) {
return callback(new Error("Releasing lock on an object that haven't aquired lock or was allready released"))
}
clearTimeout(self.lockTimer[columnName]);
self.lockTimer[columnName] = null;
var query = {_id: self._id};
query[columnName] = self.get(columnName);
var update = {};
update[columnName] = null;
self.model(self.constructor.modelName).update(query, update, function(err, numAffected) {
if (numAffected != 1) {
return callback(new Error("Unable to release lock. Someone change it before us."));
}
self.set(columnName, null)
callback()
});
};
schema.methods.isLocked = function isLocked(columnName, callback) {
if (typeof columnName == "function") {
callback = columnName
columnName = options.lockColumnNames[0]
}
var today = new Date()
if (this.get(columnName) > today) {
return true
}
return false
};
}
module.exports = documentLock;