paed01 / bpmn-engine

BPMN 2.0 execution engine. Open source javascript workflow engine.
MIT License
893 stars 167 forks source link

Correct way for ServiceTask #110

Closed juniorschroder closed 4 years ago

juniorschroder commented 4 years ago

Hi!

I'm can't understand how to work with this kind of task. I have this simplest process with a SendTask: image

Flow_1gh6tac Flow_1gh6tac Flow_0qycftg Flow_0qycftg Flow_0o9w0la Flow_0o9w0la So, when I execute this, a error has thown. I'hive tried many ways, acoording docs and issues here, and I can get errors like: callback is not defined and last: ActivityError: service not defined my engine: const engine = BpmnEngine({ name: workflow.nome, source: workflow.xml, moddleOptions: { camunda: require('camunda-bpmn-moddle/resources/camunda') }, services: { serviceFn(scope, callback) { console.log('LOG', {scope, callback}); next(); } } }); What I'm dooing wrong?
paed01 commented 4 years ago

First of all: expressions will find services under ${environment.services.<yourService>}. I don't find any documentation regarding service expressions in this project, but it's documentated in bpmn-elements.

The default BPMN2.0 attribute for a service reference is named implementation, so you can try replacing camunda.expression with that for starters.

<bpmn:sendTask id="Activity_0c0fmal" name="Avisar Gerente" implementation="${environment.services.serviceFn}" />

camunda:expression is not handled by default since it is an extension to BPMN 2.0. I use Camunda modeler for testing, like this example on how to implement Camunda as an extension.

juniorschroder commented 4 years ago

@paed01 Thanks for answer, works like a charm. I see that listener.on activity.wait or even wait does not fired when Service Task are started. Any form to do this? Or in my serviceFn, how I can pass a parameter to this function?

paed01 commented 4 years ago

One way is to pass function arguments in the expression and return a function:

<bpmn:sendTask id="Activity_0c0fmal" name="Avisar Gerente" implementation="${environment.services.serviceFn('msg')}" />

Then your service function would look something like this:

function serviceFn(name) {
  return function send(context, callback) {
     // console.log('inspect and look for environment variables', context)

     if (name !== 'msg') return callback(new Error('Not a message ' + msg));
     return callback(null, name);
  };
}

The built in expressions are documented here.

Otherwise you can make an extension to fill up the context with whatever information you like, either by a service task behaviour or by formatting

juniorschroder commented 4 years ago

Great!

Thanks a lot