0

My child.stderr.on() and child.stderr.on() both use a callback which returns a duration value(its just a number representing an execution time in ms).

I'd like to use this in my child.on(). I thought I could declare a variable outside the functions, assign the duration to it in my functions then use it in my child.on(). I've included my fnctions below. The child.stdout.on is the same as child.stdout.on. Can this be done?

child.stdout.on('data', function(data, callback) {
    getEventLog.getId(id, uuid, function(err, id, code, callback){  
      //if(err) return console.log(err)
      console.log("ID: " + id); 
      getEventLog.getDuration(id, uuid, function(err, duration, callback){
        jobRun.duration = duration;
      })
    });
   }); 




child.on('close', function(code, duration) {

  var logMessage = 
    "Completed jobRunId " + jobRun.id + " (" + jobRun.jobsId + ") " + jobRun.jobType + 
      " run " + jobRun.runNumber + " in " + jobRun.duration + " with exit code " + code;
  jobLogger.info(logMessage + "<br />");

  callback(jobRun.id);
});

When I run the above I get this:

Completed jobRunId 818 (601) teragen run 6 in 9813ms with exit code undefined

2
  • So, what happens when you try this? What exactly do you observe? You haven't really described what problem you have when you tried it. Commented Jan 30, 2018 at 1:28
  • Output added to question. Commented Jan 30, 2018 at 6:19

1 Answer 1

2

The biggest possible issue with this is that you're running in an asynchronous environment. That means that while you CAN do it this way, there's no guarantee you're going to have any given assignment on your variable at a given time unless, for example, you're assigning it a value once at the very beginning.

The short answer is yes, it will work as you expect.

The long answer is that unless you are guaranteed to have at least one "child.stdout.on('data'" call before you have a "child.on('close'" call, your variable may not be updated. (And in an asynchronous environment, that sort of code isn't usually a good idea, because you don't know WHEN something is going to happen.)

Sign up to request clarification or add additional context in comments.

4 Comments

With that in mind, how would I hacnge it to work in a more reliable way?
Depends on your use case. I guess my main question would be whether you expect one or multiple "child.stdout.on('data'" events. If you expect one event and child.close happens reliably after that event, I'd probably set it up so that it passes the data to the child.close when it calls it. If the pattern is more like this: child.stdout.on('data') child.stdout.on('data') child.stdout.on('data') child.close() then the duration variable you're recording will just be the last one to occur, so you might want a counter, something like: "totalDuration += duration"
The question you're asking seems to me to be an architecture question that I don't have enough data to answer, I guess. Sorry.
...And upon re-reading your question, I suspect the two events are getting called in the wrong order. Is it possible that getEventLog.getDuration is completing AFTER child.close? ...and if so, then you probably need to set it up so the .close doesn't happen til after the event log (or just wait to log til after.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.