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

feature(script): $addCallback $finishCallback #591

Closed

Conversation

andreialecu
Copy link
Contributor

Add manual callback handling, for when you need to require other node functions that do not play well with deployd's automatic async callback handling.

Call $addCallback() before calling an async function to increment the counter, and $finishCallback() when the callback is finished to decrement the counter. When the counter is 0, deployd will return the response.

Add manual callback handling, for when you need to `require` other node functions that do not play well with deployd's automatic async callback handling.

Call $addCallback() before calling an async function to increment the counter, and $finishCallback() when the callback is finished to decrement the counter. When the counter is 0, deployd will return the response.
@andreialecu
Copy link
Contributor Author

Relevant code snippet for some sort of event:

    var async = require("async");

    $addCallback();

    async.each(body.storeData, function (data, done) {
      doStuffOnData(data, function (err, result) {
         done(err, result);
      );
    }, function (err){
        if (err) return cancel(err);
        setResult({done: true});
        $finishCallback();
    });

…ng in deployd's console if callback add/finish happened faster than next process.nextTick.
@ericfong
Copy link
Member

Thanks for your contribution!
Landed as d8dbf3b

Any good way to detect the functions which don't play well with dpd?
Use setTimeout and check callbackCount and show warning to dev ? But for each event will be too heavy?

@ericfong ericfong closed this Jun 30, 2015
@andreialecu
Copy link
Contributor Author

Deployd only attempts to wrap functions that are part of its script domain or script context.

Using require() bypasses that entirely, so everything async that is used through require needs this.

@fotoflo
Copy link

fotoflo commented Jul 21, 2015

in your snippet

    var async = require("async");

    $addCallback();

    async.each(body.storeData, function (data, done) {
      doStuffOnData(data, function (err, result) {
         done(err, result);
      );
    }, function (err){
        if (err) return cancel(err);
        setResult({done: true});
        $finishCallback();
    });

will return cancel be returned on error? how does $finishCallback() get reached?

@andreialecu
Copy link
Contributor Author

@fotoflo: cancel automatically finishes all callbacks, or rather bypasses waiting for them, and returns the response immediately as per #602

@fotoflo
Copy link

fotoflo commented Jul 21, 2015

Thanks

@Rifazi
Copy link

Rifazi commented Dec 5, 2016

@andreialecu you are literally my hero.

@moorthi07
Copy link
Member

This helps in the 'POST' event also.
Use case:
Post event:

var createzipfile = require('..createzipfile');
$addCallback();
try{

                createzipfile(this,session,ctx,  (err,result) =>{
        if (err) {
          console.log('dispatch call failed', err);
              cancel(" Error while generating app. Pl. try again. " + err);
     
      } 
     
          this.filepath= "{'zippath':'" + result +"'}";
      
           console.log('this.codemigration..vvvv.....',this.codemigration);
      
    $finishCallback();
      });
   
           }
           catch(err)
           {
                 console.log('err in dispatch------',err);
               cancel(" Error while generating app. Pl. try again. " + err);
           }

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

Successfully merging this pull request may close these issues.

5 participants