/
index.js
302 lines (263 loc) · 6.74 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var cron = require('node-cron');
var glob = require('glob');
var fs = require('fs-extra');
var Writable = require('readable-stream').Writable;
var debug = require('debug')('cron-file-rotate-stream');
var xtend = require('xtend');
var rm = require('rimraf');
var $util = require('util');
var $path = require('path');
var noop = function(){};
var defaultOptions = {
cron: '0 0 0 * * *',
format: 'Y-M-D',//Y: year, M: month, D: month day, h: hour, m: munite, s:second
fileNum: 14
};
var assert = require('assert');
function CronFileRotateStream(filepath, opts){
if(! this instanceof CronFileRotateStream){
return new CronFileRotateStream(filepath, opts);
}
assert(typeof filepath === 'string' && filepath.trim(), '[cron-file-rotate-stream]invalid filepath! given: ' + filepath);
opts = xtend({}, defaultOptions, opts);
Writable.call(this, Object(opts.streamOptions) === opts.streamOptions ? opts.streamOptions : {} );
this.cronPattern = opts.cron;
this.format = opts.format;
this.fileNum = opts.fileNum;
this.encoding = opts.encoding || 'utf8';
this.fileObj = xtend($path.parse(filepath), {path: filepath});
this.cache = [];
this.stream = this.getStream();
this.task = cron.schedule(opts.cron, this.onCron.bind(this), false);
this.cleanup = this.cleanup.bind(this);
this.on('finish', this.cleanup);
this.removeOldFiles((function(err){
if(err){
this.emit('error', err);
}
}).bind(this));
}
$util.inherits(CronFileRotateStream, Writable);
var proto = CronFileRotateStream.prototype;
proto.startCronTask = function(){
if(this.task && !this.taskRunning){
this.task.start();
this.taskRunning = true;
}
};
proto.stopCronTask = function(){
};
proto.getStream = function(){
var fpath = this.fileObj.path;
var self = this;
fs.ensureFileSync(fpath);
var stream = fs.createWriteStream(fpath, {
flags: 'a',
defaultEncoding: this.encoding,
fd: null,
mode: 0o666
});
stream.on('error', function(err){
self.emit('error', err);
});
return stream;
};
proto.rotate = function(cb){
var backupFilepath = $path.format({
root: this.fileObj.root,
dir: this.fileObj.dir,
base: this.fileObj.name + '_' + getDateStr(this.format) + this.fileObj.ext
}),
oldPath = this.fileObj.path,
self = this;
if(this.stream){
this.stream.once('finish', function(){
self.stream = null;
debug('archiving log to %s...', backupFilepath);
fs.rename(oldPath, backupFilepath, self.removeOldFiles.bind(self, cb));
});
this.stream.end();
}else{
debug('archiving log to %s...', backupFilepath);
fs.rename(oldPath, backupFilepath, this.removeOldFiles.bind(this, cb));
}
};
proto.removeOldFiles = function(cb, err){
var self = this, cb = cb || noop;
if(err){
cb(err);
return;
}
glob(this.fileObj.name + '_*' + this.fileObj.ext, function(er, files){
if(er){
cb(err);
return;
}
files = files.sort(function(file1, file2){
var ts1 = getTimeStamp(file1);
var ts2 = getTimeStamp(file2);
return ts1 - ts2;
});
var oldFileLen = files.length - self.fileNum;
if(oldFileLen > 0){
debug('remove old log files...');
debug('%j', files.slice(0, oldFileLen));
rmFiles(files.slice(0, oldFileLen), function(err){
if(err){
cb(err);
return;
}
cb();
});
}else{
cb();
}
});
};
proto.onCron = function(){
debug('rotate start ...');
this.isCaching = true;
var self = this;
this.rotate(function(err){
if(err){
self.emit('error', err);
}
self.stream = self.getStream();
if(self.cache.length > 0){
debug('flush cache start...');
self.flushCache(function(){
debug('flush cache finished!');
self.isCaching = false;
});
}else{
self.isCaching = false;
}
});
};
proto._write = function(chunk, enc, next){
this.startCronTask();
if(this.isCaching){
debug('[cache]write to cache...........');
this.writeToCache(chunk, enc, next);
}else{
this.writeToFile(chunk, enc, next);
}
};
proto.writeToFile = function(chunk, enc, next){
if(this.stream && !this.isCaching){
this.stream.write(chunk, this.encoding);
next();
}else{
debug('[file]write to cache...........');
this.writeToCache(chunk, enc, next);
}
};
proto.writeToCache = function(chunk, enc, next){
this.cache.push(chunk);
next();
};
proto.flushCache = function(cb){
var cache = this.cache,
i = 0, l = cache.length,
stream = this.stream,
encoding = this.encoding,
self = this;
function write(){
var suc = true;
while(i < l && suc) {
var chunk = cache[i];
if(i === l - 1){
self.cache.length = 0;
stream.write(chunk, encoding, cb);
}else{
suc = stream.write(chunk, encoding);
}
i += 1;
}
if(i < l){
i -= 1;
stream.once('drain', write);
}
}
write();
};
proto.cleanup = function(){
var self = this;
debug('cleanup start ...');
if(this.task){
this.task.stop();
this.task.destroy();
}
if(this.stream){
this.flushCache(function(){
self.stream.end();
});
}
};
//helper
function isFileExist(fp){
try{
var stat = fs.statSync(fp);
return stat.isFile();
}catch(e){
return false;
}
}
function getDateStr(fmt){
var curDate = new Date();
var year = curDate.getFullYear();
var month = curDate.getMonth() + 1;
var day = curDate.getDate();
var minutes = curDate.getMinutes();
var sec = curDate.getSeconds();
var hours = curDate.getHours();
return fmt.replace(/(Y|M|D|m|h|s)/g, function(m, t){
switch(t){
case 'Y': return year;
case 'M': return month;
case 'D': return day;
case 'h': return hours;
case 'm': return minutes;
case 's': return sec;
};
});
}
function getTimeStamp(filepath){
var stat;
try{
stat = fs.statSync(filepath);
return stat.ctime.getTime();
}catch(e){
return -1;
}
}
function rmFiles(files, cb){
var i = 0, l = files.length, errState = null, count = 0;
for(; i < l; i++){
rm(files[i], function(err){
if(err){
errState = errState || err;
}
count += 1;
if(count === l){
cb(errState);
}
});
}
}
module.exports = CronFileRotateStream;
//test
var rotateStream = new CronFileRotateStream($path.join(__dirname, 'test.log'), {
cron: '*/5 * * * * *',
format: 'h-m-s',
fileNum: 20
});
var i = 0;
var timer = setInterval(function(){
rotateStream.write(''+i+'\n', 'utf8');
if(i > 1500){
rotateStream.end();
clearInterval(timer);
}
i += 1;
}, 10);