v15.0.0: Task Middleware and Better Binary

14 views
Skip to first unread message

Evan Tahler

unread,
Aug 3, 2016, 9:56:08 PM8/3/16
to actionHero.js
https://github.com/evantahler/actionhero/releases/tag/v15.0.0

Task Middleware

  • Removes "plugins" from tasks in favor of middleware, matching the rest of the ActionHero system.
  • Task middleware is implemented as a thin wrapper around Node Resque plugins and currently exposes the before_performafter_performbefore_enqueue, and after_enqueuefunctions of Resque plugins through preProcessorpostProcessorpreEnqueue, and postEnqueue methods. Each middleware requires a name and at least one function. In addition, a middleware can be global, in which case it also requires a priority.
  • In the preProcessor, you can access the original task params through this.args[0].
  • In the postProcessor, you can access the task result at this.worker.result.
  • In the preEnqueue and postEnqueue you can access the task params through this.args[0]. - If you wish to prevent a task from being enqueued using the preEnqueue middleware you must explicitly set the toRun value to false in the callback. Because the task middleware is executed by Resque this is an instance of a Resque Worker and contains a number of other elements which may be useful in a middleware.
  • The following example is a simplistic implementation of a task execution timer middleware.
'use strict';

module.exports = {
  loadPriority:  1000,
  initialize: function(api, next){
    api.taskTimer = {
      middleware: {
        name: 'timer',
        global: true,
        priority: 90,
        preProcessor: function(next){
          var worker = this.worker;
          worker.start = process.hrtime();
          next();
        },
        postProcessor: function(next){
          var worker = this.worker;
          var elapsed = process.hrtime(worker.start);
          var seconds = elapsed[0];
          var millis = elapsed[1] / 1000000;
          api.log('Task ' + worker.job.class + ' finished in ' + seconds + ' s and ' + millis + ' ms.', 'info');
          next();
        },
        preEnqueue: function(next){
          var params = this.args[0];
          //Validate params
          next(null, true); //callback is in form cb(error, toRun)
        },
        postEnqueue: function(next){
          api.log("Task successfully enqueued!");
          next();
        }
      }
    };

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

Binary Commands

  • This change greatly simplifies/refactors the ActionHero binaries and changes the following commands:
`actionhero generateAction --name=[name]`      -> `actionhero generate action --name=[name]` 
`actionhero generateInitializer --name=[name]` -> `actionhero generate initializer --name=[name]` 
`actionhero generateServer --name=[name]`      -> `actionhero generate server --name=[name]` 
`actionhero generateTask --name=[name]`        -> `actionhero generate task --name=[name]` 
  • We now also use the optimist generators/CLI tool to provide more helpful input guidance.
  • Binary commands now use normal ActionHero logger options, matching the rest of the project
  • When generating an initializer, you can specify priorities on the CLI via --startPriority--stopPriority and --loadPriority
  • Fixes a few edge cases where the ActionHero server would not boot if there were logging errors
  • Moves a few of the boot messages to debug status to quiet down ActionHero when starting up.
  • Update help file for Binary
  • by @evantahler and @l0oky via #913
Reply all
Reply to author
Forward
0 new messages