process-engine.js icon indicating copy to clipboard operation
process-engine.js copied to clipboard

Better examples and clearer flow samples

Open nicolasembleton opened this issue 11 years ago • 5 comments

I think it would be very beneficial to have a clearer flow sample. There are many examples of how individually things work, but I find that the following things are unclear to me, even though I've looked at the sources, examples and tests:

  • How does a workflow changes state reacting to its content? Unclear which API to use, how to call it from outside, how it can manipulate external data. So far my best bet is processInstance(processInstanceId).complete().
  • How does the system integrates with an actual application. I want to do, for example, document approval flow. I'm unclear how to integrate all that.

Sorry for the dummy questions, but even after a couple hours of trial & error, it's still not very clear to me the way it should work. But I really love the way it's built and the features so I try to have it working.

nicolasembleton avatar May 22 '14 15:05 nicolasembleton

It's not dummy at all. Process Engine describes the workflow using tasks and flows. The built-in task types:

  • start: mark the start of process, must be the first task
  • end: mark the end of process.
  • service: automatic task type that execute any code in it
  • decision: Certain things can only be done under certain circumstances. The decision task is used to mark the fork and join of execution path
  • human: manual task type, they are assigned by engine, e.g. place it in the task list, the engine expect confirmation to continue the execution

The flow is something to connect the tasks and can take a condition function if the from task is decision task.

human task service is used to manipulate the task list.

Therefore, for question 1, you can manipulate external data in service task, basically you provide a function that take a process variables and complete function. You can do whatever things in your function, once it's done, call complete to let engine continue execution.

'Auto UW': {type: 'service', action: function (variables, complete) {
      variables.autoUnderwritingResult = variables.age < 60;
      console.log('Auto underwriting service task result:', variables.autoUnderwritingResult);
      complete();
    }}

for question 2, you need to use 'human' task type to model the task that needs to be completed by a user. And once it's done, you can call humanTaskService.complete to let the engine continue execution. For this one, you can see test/Insurance.spec.js for complete code.

Please let me know if you have any other questions.

oliverzy avatar May 23 '14 03:05 oliverzy

Thanks. I think I got it now.

Is it possible to execute code from the action: fn() handler for a human task?

nicolasembleton avatar May 23 '14 04:05 nicolasembleton

Because process instance is a event emitter, you can attach listener on it to listen after event.

processInstance.on('before', function (task) {
// write your code here
console.log(task);      
});

oliverzy avatar May 23 '14 07:05 oliverzy

Gotcha. Pretty nice. Didn't think about that... I think it would just need 1 real-world example with access to outside data, persistence and a couple things like that to make it really practical and helpful to get started.

But thanks. Got it to work nicely.

nicolasembleton avatar May 23 '14 07:05 nicolasembleton

Yes, agree.My daily work is enterprise Java stuff. This project is my side project that inspired by some Java process engine I used but rather lightweight. Glad to see you like it.

oliverzy avatar May 23 '14 10:05 oliverzy