@atlas-engine/atlas_engine_client

Contains the client for accessing the AtlasEngine.

Usage no npm install needed!

<script type="module">
  import atlasEngineAtlasEngineClient from 'https://cdn.skypack.dev/@atlas-engine/atlas_engine_client';
</script>

README

AtlasEngine Client.ts

A client for communicating with the AtlasEngine.

It is written in TypeScript and implemented in NodeJS.

Quick example

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  const processInstances = await client.processInstances.query({});

  console.log(processInstances);
}

run();

How to use the client

You only need to provide an url to the AtlasEngine you want to access. After that, the client is ready to use.

You can either create a single AtlasEngineClient, which exposes references to all other clients,

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  const processInstances = await client.processInstances.query({}, null, 0, 100);

  console.log(processInstances);
}

run();

or create multiple specific clients with the ClientFactory:

import {ClientFactory} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const processInstanceClient = ClientFactory.createProcessInstanceClient(atlasEngineUri);

  const processInstances = await processInstanceClient.query({}, null, 0, 100);

  console.log(processInstances);
}

run();

Starting Process Instances

You can start new Process Instances through processDefinitionsClient.startProcessInstance(parameters) - Resolves right after the Process Instance was started

Where parameters is an object that collects all startup parameters.

The following parameters are required:

  • processModelId: The ID of the Process Model to execute
  • startEventId: The ID of the Start Event by which to start the Process Model. Optional, if the Process Model only has one Start Event.

In addition, the following optional parameters are available:

  • correlationId: The ID of the correlation to which this Process Instance belongs. If not provided, it will be auto-generated
  • initialToken: The initial process token with which to start the Process Instance
  • parentProcessInstanceId: When the Process Instance is supposed to be the Subprocess of another Process Instance, this contains the ID of the parent Process Instance

or through

processDefinitionsClient.startProcessInstanceAndAwaitEndEvent(parameters) - Resolves when the Process Instance has finished with any End Event

or through

processDefinitionsClient.startProcessInstanceAndAwaitSpecificEndEvent(parameters, endEventId) - Resolves after the ProcessInstance has reached a specific End Event and has additionally the required endEventId parameter:

  • endEventId: The ID of the End Event to wait for

Examples:

Start Process Instance and wait for it to start

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  await client.processDefinitions.startProcessInstance({processModelId: 'myProcessModelId'});
}

run();

Start Process Instance and wait for it to finish

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  await client.processDefinitions.startProcessInstanceAndAwaitEndEvent({processModelId: 'myProcessModelId'});
}

run();

Start Process Instance and wait for an End Event

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  await client.processDefinitions.startProcessInstanceAndAwaitSpecificEndEvent({processModelId: 'myProcessModelId'}, 'My_End_Event_1');
}

run();

Start Process Instance with custom payload and correlation ID

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  await client.processDefinitions.startProcessInstance({
    processModelId: 'myProcessModelId',
    correlationId: 'my_custom_correlation_id',
    initialToken: {
      hello: 'world',
    },
  });
}

run();

Start Process Instance as a sub process

import {AtlasEngineClient} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  await client.processDefinitions.startProcessInstance({
    processModelId: 'myProcessModelId',
    parentProcessInstanceId: 'Some-Other-Process-Instance-Id',
  });
}

run();

Code Samples

You can find executable code samples here and here.

Query UserTasks

import {AtlasEngineClient, DataModels} from '@atlas-engine/atlas_engine_client';

const atlasEngineUri = 'http://localhost:10560';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  const userTasks = await client.userTasks.query({
    processModelId: 'myProcessModelId',
    state: DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended,
  });

  console.log(userTasks);
}

run();

How to use External Task Workers

An External Task worker is designed to process External Tasks associated with a BPMN ServiceTask.

Basic

You need to provide three arguments to a worker:

  • The url of the AtlasEngine where the worker should connect to
  • The topic by which to poll External Tasks
  • A handler function for processing the External Tasks
import {AtlasEngineClient, DataModels} from '@atlas-engine/atlas_engine_client';

interface AddPayload {
  number1: number;
  number2: number;
}

interface AddResult {
  sum: number;
}

const atlasEngineUri = 'http://localhost:10560';
const topic = 'sum_numbers';

async function run() {
  const client = new AtlasEngineClient(atlasEngineUri);

  const externalTaskWorker = await client.externalTasks.subscribeToExternalTaskTopic(topic, doAdd);

  externalTaskWorker.start();
}

async function doAdd(
  payload: AddPayload,
  externalTask: DataModels.ExternalTasks.ExternalTask<AddPayload>,
): Promise<AddResult> {

  const result: AddResult = {
    sum: payload.number1 + payload.number2,
  };

  console.log('Receive payload from process instance');
  console.log(JSON.stringify(payload));

  return Promise.resolve(result);
}

run();

Note that by subscribing to a single topic, you create a specific worker for this topic.

Starting and stopping

Start the worker with:

externalTaskWorker.start();

And stop it with:

externalTaskWorker.stop();

Code Samples

You can find executable code samples here and here.