whatap 0.4.51 → 0.4.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/help.txt +1 -1
- package/lib/core/agent.js +1 -0
- package/lib/util/process-seq.js +125 -123
- package/package.json +2 -2
package/help.txt
CHANGED
package/lib/core/agent.js
CHANGED
package/lib/util/process-seq.js
CHANGED
|
@@ -4,154 +4,156 @@
|
|
|
4
4
|
* can be found in the LICENSE file.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
var fs = require('fs'),
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
var fs = require('fs'),
|
|
8
|
+
path = require('path'),
|
|
9
|
+
hahsUtil = require('../util/hashutil2'),
|
|
10
|
+
IPUtil = require('../util/iputil');
|
|
10
11
|
function isLiveProcess(pid) {
|
|
11
|
-
|
|
12
|
+
if(!pid || pid.length === 0) return true;
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
try {
|
|
15
|
+
return process.kill(pid,0)
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
return e.code === 'EPERM'
|
|
19
|
+
}
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
function LockCompetition(key){
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
this.MAIN_KEY = key;
|
|
24
|
+
var Configuration = require('./../conf/configure');
|
|
25
|
+
var root = Configuration['app.root'];
|
|
26
|
+
var cwd = process.cwd();
|
|
27
|
+
if(root == undefined) {
|
|
28
|
+
root = cwd;
|
|
29
|
+
}
|
|
30
|
+
let gubun = hahsUtil.hash(IPUtil.getIp());
|
|
31
|
+
this.rootFolder = path.join(root,'SSwhatapSS').replace('SSwhatapSS','\.'+key + '/' + gubun);
|
|
32
|
+
this.LOCK_FILE = path.join(this.rootFolder,this.MAIN_KEY+'.lock');
|
|
33
|
+
this.initComplete = false;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
LockCompetition.prototype.init = function(cb){
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
if(this.initComplete) {
|
|
38
|
+
if(cb) cb();
|
|
39
|
+
}
|
|
40
|
+
fs.mkdir( this.rootFolder, function(err){
|
|
41
|
+
if(!err || err.code === 'EEXIST'){
|
|
42
|
+
this.initComplete = true;
|
|
43
|
+
if(cb) cb();
|
|
44
|
+
}else{
|
|
45
|
+
return console.error(err);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
LockCompetition.prototype.accessAuth = function(cb){
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
51
|
+
var self = this;
|
|
52
|
+
self.init(function(){
|
|
53
|
+
var i = setInterval(function(){
|
|
54
|
+
self.createLockFile(path.join(self.LOCK_FILE), function(result){
|
|
55
|
+
if(result){
|
|
56
|
+
self.checkFileIndex(1, function(idx){
|
|
57
|
+
self.releaseLockFile( function(result){
|
|
58
|
+
if(result){
|
|
59
|
+
cb(idx);
|
|
60
|
+
clearInterval(i);
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
},10)
|
|
67
|
+
});
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
LockCompetition.prototype.readLockFile = function(cb){
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
fs.readFile( this.LOCK_FILE, 'utf-8', function(err,data){
|
|
72
|
+
if(cb) cb(err, data);
|
|
73
|
+
});
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
LockCompetition.prototype.createLockFile = function(file, cb){
|
|
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
|
-
|
|
77
|
+
var self = this;
|
|
78
|
+
self.readLockFile(function(err, data){
|
|
79
|
+
if(err){
|
|
80
|
+
fs.writeFile(file, process.pid + '', 'utf-8', function(err,data){
|
|
81
|
+
if(err){
|
|
82
|
+
if(cb) return cb(false);
|
|
83
|
+
}else{
|
|
84
|
+
self.readLockFile( function(err, data){
|
|
85
|
+
if(err && cb) return cb(false);
|
|
86
|
+
if(parseInt(data) === process.pid){
|
|
87
|
+
if(cb) return cb(true);
|
|
88
|
+
}else{
|
|
89
|
+
if(cb) return cb(false);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}else{
|
|
95
|
+
if(isLiveProcess(data)){
|
|
96
|
+
if(cb) return cb(false);
|
|
97
|
+
}else{
|
|
98
|
+
fs.unlink(self.LOCK_FILE, function(err){
|
|
99
|
+
if(err){
|
|
100
|
+
if(cb) return cb(false);
|
|
101
|
+
}else{
|
|
102
|
+
if(cb) return cb(true);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
LockCompetition.prototype.releaseLockFile = function(cb){
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
var self = this;
|
|
112
|
+
fs.readFile(self.LOCK_FILE, 'utf-8', function(err,data){
|
|
113
|
+
if(err && cb) return cb(err, data);
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
115
|
+
if(parseInt(data) === process.pid){
|
|
116
|
+
fs.unlink(self.LOCK_FILE, function(err){
|
|
117
|
+
if(err){
|
|
118
|
+
if(cb) return cb(false);
|
|
119
|
+
}else{
|
|
120
|
+
if(cb) return cb(true);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}else{
|
|
124
|
+
if(cb) return cb(false);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
LockCompetition.prototype.checkFileIndex = function(idx,cb){
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
130
|
+
var self = this;
|
|
131
|
+
idx = idx || 1;
|
|
132
|
+
fs.readFile(path.join(self.rootFolder, self.MAIN_KEY+idx), 'utf-8', function(err,data){
|
|
133
|
+
if(err){
|
|
134
|
+
fs.writeFile(path.join(self.rootFolder, self.MAIN_KEY+idx), process.pid + '', 'utf-8', function(err){
|
|
135
|
+
if(err){
|
|
136
|
+
self.checkFileIndex(++idx,cb);
|
|
137
|
+
}else{
|
|
138
|
+
if(cb) cb(idx);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}else{
|
|
142
|
+
if( isLiveProcess(data) ){
|
|
143
|
+
self.checkFileIndex(++idx,cb);
|
|
144
|
+
}else{
|
|
145
|
+
fs.writeFile(path.join(self.rootFolder, self.MAIN_KEY+idx), process.pid + '', 'utf-8', function(result){
|
|
146
|
+
if(err){
|
|
147
|
+
self.checkFileIndex(++idx,cb);
|
|
148
|
+
}else{
|
|
149
|
+
if(cb) cb(idx);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})
|
|
153
155
|
}
|
|
154
156
|
|
|
155
157
|
module.exports = function(key){
|
|
156
|
-
|
|
158
|
+
return new LockCompetition(key);
|
|
157
159
|
};
|
package/package.json
CHANGED