Skip to main content

Your First Task Worker

In LittleHorse, Task Workers are the building blocks of your WfSpecs. They do the actual "work" when a WfRun schedules a TaskRun to be executed. In this guide, we'll learn how to create your first Task Worker.

What is a Task Worker?

A Task Worker is a program that uses the LittleHorse SDK to poll TaskRuns from the queue in the LittleHorse Server. Every time the LittleHorse Server tells it to, the Task Worker executes a function or method that you write. That method invocation becomes a TaskRun in a WfRun; when it is executed the result is reported automatically back to the LittleHorse Server so the engine can decide what happens next in that particular WfRun.

Task Worker Architecture

The TaskDef

LittleHorse needs to keep track of all of the available types of TaskDefs that you can execute. This is done with the TaskDef. Before you can create a WfSpec that refers to a TaskDef, you must first register the TaskDef that you want to refer to. The TaskDef stores useful information such as the name and input / output types of the TaskDef.

info

A TaskDef is like a method signature. It tells LittleHorse the method's name, what inputs it needs, and what output it gives back.

Creating a Task Worker

In this section, we will write the code for the Task Worker from the quickstart which executes the simple greet() method.

Project Setup

First, make sure you have the LittleHorse Server running in your local environment:

docker run --pull always --name littlehorse --rm -d -p 2023:2023 -p 8080:8080 ghcr.io/littlehorse-enterprises/littlehorse/lh-standalone:latest

For the purposes of this tutorial, we will be using your respective language's Quickstart:

git clone https://github.com/littlehorse-enterprises/lh-quickstart-java.git
cd lh-quickstart-java

Let's delete all the files in the src/main/java/io/littlehorse/quickstart directory before we start.

Writing the Task Method

Let's create a simple greeting TaskDef implementation:

src/main/java/io/littlehorse/quickstart/Greeter.java
package io.littlehorse.quickstart;

import io.littlehorse.sdk.worker.LHTaskMethod;

public class Greeter {
@LHTaskMethod("greet")
public String normalJavaMethod(String name) {
return "Hello, " + name + "!";
}
}

Registering the Task Worker

Before we can use our Task Worker in workflows, we need to register it with the LittleHorse Server by creating a TaskDef. The LittleHorse SDK ships with a useful LHTaskWorker object that makes it easy to do that. The following executable Main file accomplishes that:

src/main/java/io/littlehorse/quickstart/Main.java
package io.littlehorse.quickstart;

import io.littlehorse.sdk.common.config.LHConfig;
import io.littlehorse.sdk.worker.LHTaskWorker;

public class Main {
public static void main(String[] args) {
LHConfig config = new LHConfig();
LHTaskWorker worker = new LHTaskWorker(new Greeter(), "greet", config);
worker.registerTaskDef();
}
}

Running the Worker

The last thing that remains is to run the Task Worker so that it can start listening for TaskRuns to execute. The following executable Main file can do that:

src/main/java/io/littlehorse/quickstart/Main.java
package io.littlehorse.quickstart;

import io.littlehorse.sdk.common.config.LHConfig;
import io.littlehorse.sdk.worker.LHTaskWorker;

public class Main {
public static void main(String[] args) {
LHConfig config = new LHConfig();
LHTaskWorker worker = new LHTaskWorker(new Greeter(), "greet", config);
worker.start();
}
}

Wrapping Up

In this tutorial, you learned about the basic building blocks used in a WfSpec: TaskDefs and Task Workers. Now that you have created your first Task Worker, continue on to the next lesson to learn how to use it inside a WfSpec.

In the meantime, if you haven't done so already: