Listen for Tasks
Why listen for tasks?
Services need to receive a command from the Engine in order to execute any desired task. Every time a command is received, it will ensure that the sender is the Engine, then it will check if it can handle the command, and if so, it will execute it. Once executed, it will reply to the Engine with the result of the command.
Steps to follow
To implement tasks in your Service, you'll need to :
- Add the task definition in the Service's
- Listen for task's execution from the Engine
- Submit the outputs of the task
The first step is to declare the tasks that the service will be able to execute in the service's
mesg.yml file. The events should be indexed by their ID and should describe the following attributes :
|name||String|| ||If the name of the task is not set, the name will be the ID of the task.|
|description||String|| ||Description of the task: what the task is doing and why it is useful.|
|inputs||map<id,Parameter>|| ||Map of inputs that the task needs in order to be executed.|
|outputs||map<id,Outputs>|| ||Map of outputs that the task will emit. The task can declare multiple outputs but can only submit one output per execution.|
|name||String|| ||Name of the output. The default is the ID.|
|description||String|| ||A description of the output: what kind of output, and how is it useful.|
|data||map<id,Parameter>|| ||Map of the data the output will return.|
|name||String|| ||Name or the parameter. The default is the ID.|
|description||String|| ||Description of the parameter.|
|type||Type|| ||Type of the parameter.|
|object||Parameter|| ||Nested parameters. Parameters can contain child parameters. It can only be defined when |
|optional||Boolean|| ||If true, this parameter is considered as optional and might remain empty.|
|repeated||Boolean|| ||Define this parameter as an array of the type selected|
Type of parameter
The parameter can be one of the following:
Example of a task definition in a
mesg.yml file :
... tasks: taskX: name: "Task X" description: "This is the task X" inputs: inputX: name: "Input x" description: "Foo is a string" type: String optional: false inputY: name: "Input y" description: "Bar is an optional array of boolean" type: Boolean optional: true repeated: true outputs: outputX: name: "OutputX" description: "Output X" data: foo: name: "Output data x" description: "Description about output data x" type: String bar: name: "Output data y" description: "Description about output data y" type: Boolean outputY: ... ...
Listen for task executions
To listen for task to execute, the Service needs to open a stream with the Engine using the Protobuffer definition and gRPC. Every task received on the stream needs to be executed by the Service and the output submitted back to the Engine.
Consider listening for tasks when your service is ready. If your service needs to synchronize some data first, you should wait for this synchronization before listening for tasks.
Submit outputs of task executions
Once the task execution is finished, the Service has to send the outputs of the execution back to the Engine using the Protobuffer definition and gRPC. Only one output can be submitted per execution even if the task has declared multiple outputs.
You need help ? Check out the MESG Forum.