Skip to main content

Your First Task Worker


In LittleHorse, Task Workers are the building blocks of your workflows. They do the actual "work" in your workflows. 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 a task queue in the LH Server. Every time the LH Server tells it to, the task worker executes a function or method that you write. That method is a task in a workflow; when it is executed the result is reported automatically back to the LH 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 tasks that you can execute. This is done with the TaskDef, or Task Definition. 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 task.

note

You can think of a TaskDef as a method signature. In fact, the useful info from a TaskDef come directly from your Task Method's method signature!

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 LH Server running in your local environment:

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

Next, you can either choose to use the Quickstart or create a java project using your build tool of choice. Make sure to include the littlehorse-client dependency. For example, in Gradle, you should add the following to the dependencies section in build.gradle:

  implementation 'io.littlehorse:littlehorse-client:0.12.1'

Writing the Task Method

Let's create a simple greeting task implementation:

note

In real life, your imagination is the limit as to what you can do inside a LittleHorse Task Method. Make an API call, talk to a database, call an LLM, whatever you want!

package io.littlehorse.tutorial;

import io.littlehorse.sdk.worker.LHTaskMethod;

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

The important part of the above code is the @LHTaskMethod("greet") annotation, which tells the LH SDK that the greet() method should be called when we encounter the TaskDef called greet. Other than that line, everything else is just Plain Old Java.

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 LH SDK ships with a useful LHTaskWorker object that makes it easy to do that. The following executable Main file acccomplishes that:

package io.littlehorse.tutorial;

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

public class Main {
public static void main(String[] args) {
// Create configuration from environment variables, which tells the SDK where to talk to LittleHorse.
LHConfig config = new LHConfig();

// Create and register the worker
LHTaskWorker worker = new LHTaskWorker(new GreetingWorker(), config);

// Create the Task Definition (`TaskDef`) in LittleHorse.
worker.registerTaskDef();
}
}

Run this code using:

./gradlew run

If you navigate to your LittleHorse dashboard, you should see the greet TaskDef.

TaskDef in Dashboard

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:

package io.littlehorse.tutorial;

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 GreetingWorker(), config);
worker.start();
}
}

Wrapping Up

In this tutorial, you learned about the basic building blocks used in a workflow: 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 Workflow Specification.

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