Skip to main content

Automatic tasks

WORK IN PROGRESS

This sections is in active development, so can be incomplete.

You are progressing in your KuFlow learning!

Now, we want to take you to the next level by addressing processes based on the based-on-Temporal Engine, which are much more powerful although more complex.

Don't worry, we have prepared everything you need so that you can tackle this challenge.

First of all, we need to create a new application, named MyWorker. This will allow us to establish a connection to KuFlow from a third-party app (i.e. our worker).

After creating this new application, it will have an Identifier and a Token. Keep this token information in a safe place, since it cannot be retrieved afterwards.

Click on Edit application and Add certificate. We'll name it MyWorkerCert. Also, store the certicate keys in a safe place, for further use.

For the Process reimbursement task, we need to adjust permissions as we want that the worker will take charge of it.

Now it is time to change the Workflow Engine. We are not longer using the Diagram Tool (technically, the Serverless Engine) but the based-on-Temporal KuFlow Engine.

  • Workflow application: MyWorker
  • Task queue: MyQueue
  • Type: MyWorkflow

Once we have made the adjustements in the process definition, it is time to work on the worker (forgive the redundancy).

We can save a lot of coding time asking KuFlow to provide us with a sample implementation for any process definition. In this case we are choosing Java as the implementation language (other languages to be added to this tutorial soon).

Now it is time to publish the process, as it doesn't require any additional changes.

One of the first things to do when preparing a KuFlow worker is to make the arrangements for establishing a connection with the cloud platform.

Modify the application-fillme.yml file, specifying the information of the token and the certificate keys.

After specifying the connection parameters, run the Java project and check that the connection of the worker is well established.

Now it is time to modify the worker, editing SampleWorkflowImpl.java:

Change the runWorkflow() method:

    @Override
public WorkflowResponse runWorkflow(WorkflowRequest request) {
LOGGER.info("Process {} started", request.getProcessId());

this.kuFlowGenerator = new KuFlowGenerator(request.getProcessId());

// --- New code ---
Task taskSubmitClaim = this.createTaskSubmitExpenseClaim(request);
double amount = TaskUtils.getElementValueAsDouble(taskSubmitClaim, "AMOUNT");
if (amount >= 1000)
{
Task taskApproval = this.createTaskApproveClaim(request);
String decision = TaskUtils.getElementValueAsString(taskApproval, "DECISION");
if (decision.equals("Y"))
this.createTaskProcessReimbursement(request);
}
else
{
this.createTaskProcessReimbursement(request);
}
// ---------

CompleteProcessResponse completeProcess = this.completeProcess(request.getProcessId());

LOGGER.info("Process {} finished", request.getProcessId());

return this.completeWorkflow(completeProcess);
}

And modify createTaskProcessReimbursement():

/**
* Create task "Process reimbursement" in KuFlow and wait for its completion
*
* @param workflowRequest workflow request
* @return task created
*/
private Task createTaskProcessReimbursement(WorkflowRequest workflowRequest) {
UUID taskId = this.kuFlowGenerator.randomUUID();

TaskDefinitionSummary tasksDefinition = new TaskDefinitionSummary();
tasksDefinition.setCode(TASK_CODE_PROCESS_REIMBURSEMENT);

Task task = new Task();
task.setId(taskId);
task.setProcessId(workflowRequest.getProcessId());
task.setTaskDefinition(tasksDefinition);

CreateTaskRequest createTaskRequest = new CreateTaskRequest();
createTaskRequest.setTask(task);


// --- Modifications begin here ---

// Create and retrieve Task in KuFlow
// this.kuFlowAsyncActivities.createTaskAndWaitFinished(createTaskRequest);

// Set values
TaskUtils.setElementValueAsString(task, "COMMENTS", "Processed automatically");
this.kuFlowSyncActivities.createTask(createTaskRequest);

// Claim task
ClaimTaskRequest claimTaskRequest = new ClaimTaskRequest();
claimTaskRequest.setTaskId(task.getId());
this.kuFlowSyncActivities.claimTask(claimTaskRequest);

// TO-DO: Process reimbursement in third party systems (or whatever)
// ...

// Complete task
CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
completeTaskRequest.setTaskId(task.getId());
this.kuFlowSyncActivities.completeTask(completeTaskRequest);

// --- End of modifications


RetrieveTaskRequest retrieveTaskRequest = new RetrieveTaskRequest();
retrieveTaskRequest.setTaskId(taskId);
RetrieveTaskResponse retrieveTaskResponse = this.kuFlowSyncActivities.retrieveTask(retrieveTaskRequest);

return retrieveTaskResponse.getTask();
}

Finally, you can test the process definition starting some executions of this workflow. Please check as the workflow depends on the expense amount and on the decision of approval, and observe that the Process reimbursement task is now completed automatically (no user involvement needed).

To help you tackle all the details of this step, we have prepared a small illustrative video that will guide you through the process of importing the template, downloading the Java project, adapting it to your needs, and executing the process.

Watch the video

We recommend that you follow the steps in the video carefully and experiment with the process once you've run it. This will be a big step forward in your understanding of KuFlow and will allow you to get the most out of the platform.

Don't miss the opportunity to tackle more complex processes on KuFlow and experience all that this platform has to offer!

Kuflow Logo