Problem With Task Middleware

16 views
Skip to first unread message

Paul Tiseo

unread,
Aug 18, 2016, 10:25:54 AM8/18/16
to actionHero.js
This task middleware is giving me an error at startup. Do I not need the next() at the end of the initialize() block?

Error:
C:\Repositories\Node\agent\node_modules\async\dist\async.js:844
            if (fn === null) throw new Error("Callback was already called.");
                             ^

Error: Callback was already called.
    at C:\Repositories\Node\agent\node_modules\async\dist\async.js:844:36
    at C:\Repositories\Node\agent\node_modules\async\dist\async.js:3676:17
    at C:\Repositories\Node\agent\node_modules\async\dist\async.js:339:31
    at C:\Repositories\Node\agent\node_modules\actionhero\actionhero.js:150:15
    at Object.module.exports.initialize (C:\Repositories\Node\agent\initializers\1000_taskTimer.js:34:5)
    at loadFunction (C:\Repositories\Node\agent\node_modules\actionhero\actionhero.js:148:44)
    at C:\Repositories\Node\agent\node_modules\async\dist\async.js:3671:13
    at replenish (C:\Repositories\Node\agent\node_modules\async\dist\async.js:884:21)
    at iterateeCallback (C:\Repositories\Node\agent\node_modules\async\dist\async.js:869:21)
    at C:\Repositories\Node\agent\node_modules\async\dist\async.js:847:20

Code:
module.exports = {
  loadPriority:  1000,

  initialize: function(api, next) {
    
    var taskTimer = {
        name: 'taskTimer',
        global: false,
        priority: 500,
        preProcessor: function(next) {
          console.log('taskTimer preProcessor:');
          next();
        },
        postProcessor: function(next) {
          console.log('taskTimer postProcessor:');
          next();
        },
        preEnqueue: function(next) {
          console.log('taskTimer preEnqueue:');
          next(null, true);
        },
        postEnqueue: function(next) {
          console.log('taskTimer postEnqueue:');
          next();
        }
    };

    api.tasks.addMiddleware(taskTimer, function(error) {
      next(error);
    });

    next();
  }

};

Evan Tahler

unread,
Aug 18, 2016, 11:48:56 AM8/18/16
to actionHero.js
You are re-using `next` here.  As written, you will call 'next' normally as the execution of the end of your method AND after the middleware is added.  You only need to call it once.

Also, there really is no reason to have a callback at all for api.tasks.addMiddleware (and none of the other middleware add methods have this).  I'll remove that here: https://github.com/evantahler/actionhero/pull/926
Reply all
Reply to author
Forward
0 new messages