-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
42 lines (34 loc) · 1.07 KB
/
utils.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
var Promise = require('bluebird');
var _ = require('lodash');
var net = require('net');
exports.retry = function(operation, delay, options) {
options || (options = {});
!_.isFunction(options.if) && (options.if = function() { return true; });
return operation()
.catch(function(reason) {
if (options.if(reason)) {
return Promise
.delay(delay)
.then(exports.retry.bind(null, operation, delay * 2, options));
} else {
return Promise.reject(reason);
}
})
};
exports.dispatch = function(babl) {
var response = new Buffer('');
return new Promise(function(resolve, reject) {
var socket = net.connect({ path: process.env.QUARTZ_SOCKET }, function() {
socket.write(babl.toJSON());
});
socket.on('data', function(data) {
response = Buffer.concat([response, data]);
data.slice(-1).toString() === '\n' && socket.end();
});
socket.on('end', function() {
resolve(response);
});
socket.on('error', reject);
socket.on('close', babl.process.kill.bind(babl.process));
});
};