Skip to content

Instantly share code, notes, and snippets.

@srijs
Created May 9, 2014 19:53
Show Gist options
  • Save srijs/b5b21638280875415216 to your computer and use it in GitHub Desktop.
Save srijs/b5b21638280875415216 to your computer and use it in GitHub Desktop.
Dockerode Timeout + Buffer.limit
var Stream = require('stream'),
Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
function StreamBuffer () {
Stream.call(this);
this.arr = [];
this.writable = true;
this.length = 0;
this.limit = Infinity;
this.write = function (data) {
if (this.length + data.length > this.limit) {
this.emit('limit');
} else {
this.arr.push(data);
this.length += data.length;
}
};
this.end = this.write;
}
StreamBuffer.prototype = Object.create(Stream.prototype);
StreamBuffer.prototype.constructor = StreamBuffer;
var outStream = new StreamBuffer(),
errStream = new StreamBuffer();
outStream.limit = 1024 * 1024;
var killedFlag = false, killingTimeout;
docker.run('ubuntu', ['yes'], [outStream, errStream], {Tty:false}, function (err, data, container) {
clearTimeout(killingTimeout);
console.log('END: (err=' + err + ')');
console.log('Killed: ' + killedFlag);
console.log('Buffer: ' + outStream.length + ' < ' + outStream.limit);
}).on('container', function (container) {
outStream.once('limit', function () {
if (killedFlag) return;
killedFlag = true;
console.warn('Container reached buffer limit. Killing...');
container.kill(function (err, data) {
if (err) console.error('Killing failed:' + err);
});
});
killingTimeout = setTimeout(function () {
if (killedFlag) return;
killedFlag = true;
console.warn('Container reached timeout. Killing...');
container.kill(function (err, data) {
if (err) console.error('Killing failed: ' + err);
});
}, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment