-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (96 loc) · 3.12 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
const mongoose = require('mongoose');
const Admin = mongoose.mongo.Admin;
var program = require('commander');
program
.version('0.1.0')
.option('-l, --lock [type]', 'Name of lock to apply')
.option('-t, --time [type]', 'Lock time')
.option('-pl, --pidlock [type]', 'Pid lock')
.option('-r, --release [type]', 'Name of lock to release')
.option('-c, --clean', 'Clean the MongoDBs (will leave lock table)')
.parse(process.argv);
var lockTime = Math.max(program.time || 1000*60*2,1000*60*10);
console.log = function(){}
if(program.clean){
(async function(){
try{
var con = await mongoose.createConnection('mongodb://127.0.0.1:27017/lock');
var dbs = await new Admin(con.db).listDatabases();
if(dbs){
console.log(dbs);
for(var i=0; i<dbs.databases.length; i++){
if(!['lock', 'admin', 'local'].includes(dbs.databases[i].name)){
console.log('deleting', dbs.databases[i].name);
var conToDrop = await mongoose.createConnection('mongodb://127.0.0.1:27017/'+dbs.databases[i].name);
var doppedCheck = await conToDrop.db.dropDatabase();
console.log(doppedCheck);
}
}
process.exit(0)
}
console.log('no dbs to clean');
}catch(e){
console.log(e);
}
process.exit(1)
})()
return 1;
}
if (!program.lock && !program.release && !program.pidlock){
return process.exit(1);
}
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var LockSchema = new Schema({
lock : { type: String, unique: true },
time : Number,
pid : String
}, {expireAfterSeconds: 60*10});
var LockModel = mongoose.model('Lock', LockSchema);
(async function(){
await mongoose.connect('mongodb://127.0.0.1:27017/lock');
if (program.release && !program.pidlock){
var singleToRelease = await LockModel.findOne({lock: program.release});
console.log(singleToRelease);
if(singleToRelease){
await singleToRelease.remove();
}
return process.exit(0);
}
if (program.release && program.pidlock){
var manyToRelease = await LockModel.find({pid: program.pidlock});
for(var i=0; i < manyToRelease.length; i++){
await manyToRelease[i].remove();
}
return process.exit(0);
}
var locksToRelease = await LockModel.find({time: {$lt: Date.now()}});
console.log('Items to clear', locksToRelease.length);
for( var i=0; i<locksToRelease.length; i++ ) {
console.log('generic timeout', locksToRelease[i]);
await locksToRelease[i].remove();
}
if (program.lock){
console.log("new lock", program.lock);
var query = {lock: program.lock};
if(program.pidlock){
query.pid = program.pidlock;
}
let previousLock = await LockModel.findOne(query);
console.log("previous", previousLock);
if(previousLock && previousLock.time > Date.now()){
console.log("previous is valid");
return process.exit(1);
}else{
if(previousLock){
console.log("releasing previous");
await previousLock.remove();
}
console.log("applying new");
let newLock = new LockModel({lock: program.lock,time: Date.now()*1+lockTime*1 });
await newLock.save();
return process.exit(0);
}
}
process.exit(1);
})();