DEV Community

Cover image for Part 1 - LittleHorse Advanced Concept Series: Conditionals
Sohini Pattanayak for LittleHorse

Posted on

Part 1 - LittleHorse Advanced Concept Series: Conditionals

Welcome to our blog series on IT Incident Management using LittleHorse! If you're into coding, workflow automation, or just looking to up your IT game, you're in the right place. This series will walk you through creating responsive and efficient workflows to manage IT incidents like a pro. As a bonus, this series will introduce many key LittleHorse concepts that will allow you to apply LittleHorse to any domain, not just IT incident management.

While dealing with IT incident management, being able to make decisions based on different scenarios is key. That's where conditionals come into play. Think of conditionals as the decision-makers in your workflow, helping your system figure out what to do next based on what's happening.

Image description

Conditionals allow your workflows to behave differently depending on the situation, rather than repeatedly doing the same thing over-and-over (like BB-8 above).

Let's set the stage for our incident management system.

Our mission?

To develop a workflow that can identify IT incidents, figure out how serious they are, and take action. We'll break it down into two main types of incidents: the "Oh no!" kind (critical) and the "We've got this" kind (non-critical). Along the way, we’ll become LittleHorse pro’s so that you can apply LittleHorse to any business domain, whether related to IT incident management or otherwise.

To get started, make sure you have completed the below steps:

Have LittleHorse installed on your computer.
Have docker up and running

Once we’re all set with the above prerequisites, we’ll build a system that intelligently assesses IT incidents and decides the best course of action.

Creating the Workflow

Our first step is to create the file ConditionalsExample.java. This is where we'll map out the logic that determines how our workflow will handle incidents. Open up your favorite Java IDE or text editor, and let's get started.

Defining the Workflow Structure

In ConditionalsExample.java, we'll start by defining our workflow structure. Here's a simple outline to get us started:

package lh.it.demo;

import io.littlehorse.sdk.common.proto.*;
import io.littlehorse.sdk.wfsdk.*;

public class ConditionalsExample {

    public static final String WF_NAME = "it-incident";
    public static final String VERIFY_TASK = "verify-incident";
    public static final String SEND_CRITICAL_ALERT_TASK = "send-critical-alert-task";
    public static final String PERIODIC_CHECK_TASK = "periodic-check-task";

    public void defineWorkflow(WorkflowThread wf) {
        // We'll flesh this out in a moment
    }

    public Workflow getWorkflow() {
        return Workflow.newWorkflow(WF_NAME, this::defineWorkflow);
    }

    // Additional methods will go here

}

Enter fullscreen mode Exit fullscreen mode

This sets up a class ConditionalsExample that will hold our workflow logic. We've also declared constants for our workflow name and task names, which will come in handy later. These constants have specific use case:

WF_NAME: Identifies the workflow, used to start or reference it within LittleHorse.

VERIFY_TASK: Represents a task that checks and categorizes the incident details.

SEND_CRITICAL_ALERT_TASK: Triggers an alert mechanism for incidents deemed critical.

PERIODIC_CHECK_TASK: Executes regular checks to update the status of an ongoing incident.

The variables we define in our workflow are not standalone; they interact and often depend on each other. For instance, the incidentSeverity variable determines whether to run the VERIFY_TASK or not. Similarly, the outcome of VERIFY_TASK might affect whether SEND_CRITICAL_ALERT_TASK is executed.

Implementing Conditional Logic

Now, we'll add the brains of our operation: the conditional logic. This logic checks incident severity and routes the workflow accordingly:

// Inside ConditionalsExample.defineWorkflow method

WfRunVariable incidentSeverity = wf.addVariable("severity", VariableType.INT);

WorkflowCondition isCritical = wf.condition(incidentSeverity, Comparator.GREATER_THAN, 5);

wf.doIf(
    isCritical,
    ifBody -> {
        ifBody.execute(VERIFY_TASK, incidentSeverity);
        // We'll add more here later
    }
);

Enter fullscreen mode Exit fullscreen mode

This code snippet introduces a workflow variable incidentSeverity and a conditional isCritical. If the severity of an incident is above 5, the workflow considers it critical and executes the VERIFY_TASK. You can check out our youtube tutorial on this example to develop a comprehensive idea of how conditionals work out!

Building Out the Workflow

With our conditionals set up, we'll now chalk out what happens in the critical and non-critical paths. But before we do that, let's pause and understand why this matters.

Conditionals are powerful because they allow our workflow to be not just a static sequence of tasks but a dynamic, decision-making process. Depending on the severity of an incident, our workflow can trigger alerts, involve different team members, or escalate issues automatically.

Why can’t we test the command yet?

To test if conditionals are fully working, we need to pass a command through our terminal. We cannot run it at this point because our workflow is not fully implemented. We have just begun to sketch out the structure in ConditionalsExample.java. The lhctl run command is used to initiate a workflow when all components, including the main application file (App.java) and the task definition files, are complete and integrated.

Next Steps

In our next blog post, we'll continue building our ConditionalsExample.java, adding more tasks and integrating more advanced LittleHorse features. We'll see how our workflow can not only detect and verify incidents but also take proactive steps to resolve them.

Top comments (0)