Closed 3goats closed 7 years ago
I've had success with capturing the stream this way:
var Writable = require('stream').Writable;
// snip
var myStream = new Writable();
myStream._write = function write(doc, encoding, callback) {
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
var result = decoder.write(doc);
resolve(result); // Note I'm wrapping this in a promise
};
// snip
docker.run('myContainer', ['node', 'app.js'], myStream, {}...
However, I'm finding a limitation on the length of what can be outputted. It seems to cap at 8192 bytes for some reason, so if anyone has a pointer there, I'd appreciate it!
Update: I figured it out...I wasn't implementing the write
function correctly and not calling the callback (next()
in the sample below), therefore it would stop streaming after the first chunk.
var myStream = new Writable();
var output = ''
myStream._write = function write(doc, encoding, next) {
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
var result = decoder.write(doc);
output += result;
next()
// resolve(result); // Moved the resolve to the handler, which fires at the end of the stream
};
function handler(error, data, container) {
if (error) {
console.log({ 'status': 'error', 'message': error });
reject(error)
}
resolve(output);
};
docker.run('myContainer', ['node', 'function.js'], myStream, {}, handler);
The best approach is to use library such as memory-streams to deal with output.
Hi,
Is there a way to use
docker.run
and capture the output to a variable.This is what I have so far: