node-js-libs / node.io

MIT License
1.8k stars 140 forks source link

How to pipe Jobs correctly? #164

Closed VAS closed 10 years ago

VAS commented 10 years ago

It is often mentioned in issues and documentation that node.io is designed to pipe easily with stdin/stdout.

However, when trying to pipe 2 different jobs, like the following: Job1

var nodeio = require('node.io');

var link = 'http://domain.org';

exports.job = new nodeio.Job({
    input : false,
    run : function() {
        var self = this;

        self.getHtml(link, function(err, $){
            var array = [];
            var results = $('.list-info h3 a').each(function(value){
                array.push(value.attribs.href)
            });

            self.emit(array);
        });
    }
});

And job2

var nodeio = require('node.io');
var link = 'http://domain.org';

exports.job = new nodeio.Job({
    input : false,
    run : function(link) {

        if(!link) this.skip();

        self.getHtml(link, function(err, $){
            if(err) {
                this.exit(err);
            }

            var title = $('.hdr1').text;
            this.emit(title);

        });
    }
});

using the following command line prompt:

node.io job1 | node.io job2

Makes job2 start immediately with a null input value.

chriso commented 10 years ago

It'll work if you leave out the input: false from the second job.

a.js

var nodeio = require('node.io');

exports.job = new nodeio.Job({
    input: false,
    run: function () {
        this.emit('foo');
    }
});

b.js

var nodeio = require('node.io');

exports.job = new nodeio.Job({
    run: function (input) {
        this.emit(input + 'bar');
    }
});

Result:

$ node.io --silent a | node.io b
foobar
OK: Job complete
VAS commented 10 years ago

Thanks @chriso , sorry to see nodeio go, it has helped us a ton.