-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.15 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
const fs = require('fs');
const path = require('path');
const Queue = require('cuejs');
function IngestJS({ dir, interval } = {}) {
this.path = dir;
this.interval = interval;
this.flow = undefined;
this.queue = new Queue();
}
IngestJS.prototype.start = function start() {
this.flow = setInterval(() => {
return fs.readdir(this.path, (err, data) => {
if (err) return console.error(err);
if (data.length === 0) return undefined;
for (let i = 0; i < data.length; i++) {
if (!data[i].match(/\.lock/)) {
fs.rename(path.join(this.path, data[i]), path.join(this.path, `${data[i]}.lock`), (renameErr) => {
if (renameErr) throw new Error(renameErr);
fs.unlink(path.join(this.path, data[i]), unlinkErr => new Error(unlinkErr));
});
this.queue.enq(data[i]);
}
}
return undefined;
});
}, this.interval);
};
IngestJS.prototype.stop = function stop() {
clearInterval(this.flow);
this.flow = undefined;
};
IngestJS.prototype.restart = function restart() {
};
IngestJS.prototype.getQueue = function getQueue() {
return this.queue;
};
module.exports = IngestJS;