-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·59 lines (51 loc) · 1.57 KB
/
index.js
File metadata and controls
executable file
·59 lines (51 loc) · 1.57 KB
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
const spawn = require('child_process').spawn
var RateLimiter = require('limiter').RateLimiter
var path = require('path')
var chokidar = require('chokidar')
var fs = require('fs');
const async = require('async')
const dir = process.env.SPOOL_DIR || '/var/www/html/spool/default'
var pattern = `${dir}/*.message`
const rate = 80
const concurrency = 20
var limiter = new RateLimiter(rate, 'second')
var q = async.queue(function ls(task, callback) {
console.log(task)
var script = path.resolve(__dirname, 'send.php')
limiter.removeTokens(1, function() {
var child = spawn('php', [script, task], {detached: true})
child.on('close', function(code) {
callback()
})
})
}, concurrency)
q.drain = function() {
console.log('all items have been processed')
}
// Need to rename all .sending to normal
fs.readdir(dir, (err, files) => {
if ( err ) console.log('ERROR: ' + err);
files.forEach(file => {
fs.rename(`${dir}/${file}`, `${dir}/${file.replace('.sending', '')}`, function(err) {
if ( err ) console.log('ERROR: ' + err);
});
});
});
// One-liner for current directory, ignores .dotfiles
const watcher = chokidar
.watch(pattern, {
usePolling: true,
interval: 5000,
awaitWriteFinish: true,
ignored: /(^|[\/\\])\../
})
.on('add', path => q.push(path))
var stop = function () {
console.log('draining. wait for 10s')
watcher.close()
setTimeout(() => {
console.log('done')
}, 10000);
}
process.on('SIGTERM', stop);
process.on('SIGINT', stop);