@sourceloop/task-service / Exports
@sourceloop/task-service¶
Overview¶
A reusable, customizable and workflow based task service which creates some actionable tasks based upon various events happening via different microservices in a distributed system.
To get started with a basic implementation of this service, see /sandbox/task-ms-example.
Installation¶
Getting Started¶
You can start using @sourceloop/task-service
in just 4 steps:
Bind Component¶
- choose and bind an incoming connector and an outgoing connector. refer Incoming and Outgoing connectors section for more details
- bind the config for task service (optional) -
Configure datasources¶
- to use the task service you need to bind atleast two datasources (though both could connect to the same db)
- the
dataSourceName
property of these two should beWorkflowServiceSourceName
andTaskDbSourceName
variables exported by the task service. For example, one of the datasources would look something like this -
Run the migrations¶
- To run the migrations provided by the task service (available in the migrations folder of both the source code and the packed version in your
node_modules
), use the db-migrate package. - Run the HTTP migrations only if you plan to use the Http Outgoing connector.
- Additionally, there is now an option to choose between SQL migration or PostgreSQL migration. NOTE : For @sourceloop/cli users, this choice can be specified during the scaffolding process by selecting the "type of datasource" option.
Commands¶
The workflows run by the task service could have service tasks for various actions, but the task service provides two common service-tasks-
Create Tasks¶
This command expects an input variable holding a list of tasks. This command creates all the tasks in this variable and triggers an outgoing event with the created tasks. To trigger this command at a node, use the topic - create-tasks
and provide a variable with structure -
End Task¶
This command is expected to be used as a topic for the end event of a task workflow. For example, in case of camunda, it would be topic for Message End Event node. To use this command, use the topic - end-task
Incoming and Outgoing Connectors¶
Task service needs an IIncomingConnector
and an IOutgoingConnectors
implementation to send and receive events from and to an external source. By default, task service comes with 2 different sets of connectors. Note that you can use different types of incoming and outgoing connectors (For e.g. Incoming events are received through Kafka but outgoing events go to a webhook subcriptions using Http)
Kafka¶
This set of connectors implements connectors to receive and send events through Kafka. It can be bound as both incoming and outgoing connector and needs an extra binding of an adapter for adapting kafka events to the type expected by the task service. You need to install kafkajs to use these connectors - npm i kafkajs
HTTP¶
This set of connector implement code to send and receive events through HTTP calls.
Incoming connector¶
- it receives events through the endpoint -
/events/trigger
- the payload for this endpoint looks something like this -
Outgoing connector¶
- The outgoing connector publishes events to all the webhook subcriptions
- A webhook subscription is created by hitting the
/webhooks/subscribe
endpoint with a payload that looks something like this -
and a couple of required request headers - x-api-key
and x-api-secret
.
- values for these headers are generated through another endpoint -
/client-apps
. This endpoint is supposed to be hit once by each new client and returns newly generated key and secret that are used for sending and verifying webhook calls. The call to the/client-apps
expects following body -
- each webhook call also sends two headers -
x-task-signature
andx-task-timestamp
to help validate the authenticity of the webhook calls by the client. This signature can be validated by the client by generating an HMAC using the event payload and the timestamp. A sample node.js code on how to do this is given below -
Using with Sequelize¶
This service supports Sequelize as the underlying ORM using @loopback/sequelize extension. And in order to use it, you'll need to do following changes.
1.To use Sequelize in your application, add following to application.ts:
- Use the
SequelizeDataSource
in your audit datasource as the parent class. Refer this for more details.