e2ebridge/bpmn

Some handlers are not called

chevdor opened this issue · 6 comments

I am using the task.bpmn sample.

task.bpmn | uploaded via ZenHub

A basic manager:

var bpmn = require("bpmn");
var manager = new bpmn.ProcessManager();

bpmn.createUnmanagedProcess("/.../task.bpmn", function(err, task){
    task.triggerEvent("MyStart");
});

and the following handlers:

/*global module exports console */

exports.MyStart = function( data , done ){
    // called after the start event arrived at MyStart
    console.log("Running MyStart");
    done();
};

exports.MyTask = function( data , done ){
    // called at the beginning of MyTask
    console.log("Running MyTask");
    setTimeout(function(){
        done();
    }, 2000);

};

exports.MyTaskDone = function( data , done ){
    // Called after the process has been notified that the task has been finished
    // by invoking myProcess.taskDone("MyTask").
    // Note: <task name> + "Done" handler are only called for
    // user tasks, manual task, and unspecified tasks
    console.log("MyTask is now Done", data);
    done();
};


exports.MyEnd = function( data , done ){
    // Called after MyEnd has been reached
    console.log("Running MyEnd");
    done();
};

exports.MyEndDone = function( data , done ){
    // Called after MyEnd has been reached
    console.log("MyEnd is done");
    done();
};

I get the following output:

$ node manager.js 
Running MyStart
Running MyTask
$ 

so some handlers seem not to be called:

  • MyTaskDone
  • MyEnd
  • MyEndDone (ok not sure whether this one makes sense at all 😄 )

From my understanding of the documentation, there are two main types of tasks:

"Task, User Task, Manual Task, Receive Task: These tasks call an event handler when the task starts and then _wait until taskDone(taskName, data) is invoked_ on the process."

and

"Service Task, Script Task, Business Rule Task, Send Task (Wait Tasks): These tasks call an event handler when the task starts and _proceed immediately after the handler finishes._"

Therefore, I think it's not that the handlers got ignored but rather your process did not yet finish the task "MyTask". It seems either necessary to explicitly call taskDone or explicitly specify the type of MyTask as a Service Task.
.

Hello, I tried calling the task explicitly using taskDone; the handler was still not called. Any solution?

Hello,

Effectively to trigger the end of a waiting task (Task, User Task, Manual Task) we need to call taskDone(taskName, data) on the process.

Be careful that the taskDone should be called after the start handler function (MyTask in the exemple) is finished (the done callback is called). In the example there is a timeout of 2sec. I suspect that this can be the issue for @adi2f4u.

Hi @cyrilschmitt , I tried again with the following changes: The output I receive is:
MyStart
MyTask

/*global module exports console */

exports.MyStart = function( data , done ){
// called after the start event arrived at MyStart
console.log("MyStart");
done("MyStart");
};

exports.MyTask = function( data , done ){
// called at the beginning of MyTask
console.log("MyTask");
done();
taskDone("MyTask", data); //Also tried with taskDone(MyTask, data);

};

exports.MyTaskDone = function( data , done ){
// Called after the process has been notified that the task has been finished
// by invoking myProcess.taskDone("MyTask").
// Note: + "Done" handler are only called for
// user tasks, manual task, and unspecified tasks
console.log("MyTaskDone");
done();
};

exports.MyEnd = function( data , done ){
// Called after MyEnd has been reached
console.log("MyEnd");
done();
};

/**

  • @param {String} eventType Possible types are: "activityFinishedEvent", "callHandler"
  • @param {String?} currentFlowObjectName The current activity or event
  • @param {String} handlerName
  • @param {String} reason Possible reasons:
  •                      - no handler given
    
  •                      - process is not in a state to handle the incoming event
    
  •                      - the event is not defined in the process
    
  •                      - the current state cannot be left because there are no outgoing flows
    
    */
    exports.defaultEventHandler = function(eventType, currentFlowObjectName, handlerName, reason, done) {
    // Called, if no handler could be invoked.
    done(data);
    };

exports.defaultErrorHandler = function(error, done) {
// Called if errors are thrown in the event handlers
done();
};

exports.onBeginHandler = function(currentFlowObjectName, data, done) {
// do something
done(data);
};

exports.onEndHandler = function(currentFlowObjectName, data, done) {
// do something
done(data);
};

Ok I understand your error now. The taskDone function should not be called like that but on the process object.

var bpmn = require("bpmn");
var manager = new bpmn.ProcessManager();

bpmn.createUnmanagedProcess("/.../task.bpmn", function(err, process){
    process.triggerEvent("MyStart");

    // timeout is here to make it asynchronous. In real cases it would be called in reaction to a user interaction, a service request...
    setTimeout(function(){
        process.taskDone("MyTask", {});
    }, 2000);

});

Ah understand now. Fixed my issue. Thank you!