Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support put job with loop #3

Closed
jspaper opened this issue Jul 4, 2012 · 2 comments
Closed

Support put job with loop #3

jspaper opened this issue Jul 4, 2012 · 2 comments

Comments

@jspaper
Copy link

jspaper commented Jul 4, 2012

hi mate,
I try to put jobs with loop as below, but it seem need to exit process.
Is there any better idea?

thanks you.

var fivebeans = require('fivebeans');
var host = '127.0.0.1';
var port = 11300;
var tube = 'my_tube';

for (var i=0;i<3;i++){
  var stalker=new fivebeans.client(host, port);
  stalker.connect(function(err){
    stalker.use(tube, function(err, tubename){
      stalker.put(0,0,60,"foo", function(err, jobid){
        console.log(jobid);
        // process.exit(0);
      });
    });
  });
}
@ceejbot
Copy link
Owner

ceejbot commented Jul 4, 2012

You're creating three clients there, once per trip through the loop, which is probably not what you want. If you're looping through data that you want to do something asynchronous with, use a continuer function. I updated the emitjobs.js example to show this off, but here's a stripped down example:

var fivebeans = require('fivebeans');
var joblist = [ 'one', 'two', 'three', 'four', 'five' ];

var doneEmittingJobs = function()
{
    console.log('We reached our completion callback. Now closing down.');
    emitter.end();
    process.exit(0);
};

var continuer = function(err, jobid)
{
    console.log('emitted job id: ' + jobid);
    if (joblist.length === 0)
        return doneEmittingJobs();

    emitter.put(0, 0, 60, JSON.stringify(['testtube', joblist.shift()]), continuer);
};

var emitter = new fivebeans.client('localhost', 11300);
emitter.connect(function(err)
{
    emitter.use('testtube', function(err, tname)
    {
        console.log("using " + tname);
        emitter.put(0, 0, 60, JSON.stringify(['testtube', joblist.shift()]), continuer);
    });
});

First you define a continuer function that knows how to tell when you've looped through all your data and knows how to kick off processing on the next chunk of data. Then you invoke your processing on the first data item & pass the continuer as a callback. This is a general pattern you'll use often when looping through data in node. There's a pretty good explanation of this pattern in this article, with a more for-loop-y variation on it.

Or you could use a flow control module to help with loops. I often use async for this.

@ceejbot ceejbot closed this as completed Jul 4, 2012
@jspaper
Copy link
Author

jspaper commented Jul 6, 2012

@ceejbot Thank you provide those information:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants