top of page

Part 3 - LittleHorse Advanced Concept Series: Variable Mutations and External Events

Updated: Mar 15

Welcome back to our journey through the advanced concepts of IT Incident Management using LittleHorse. After mastering conditionals and loops, it's time to learn about variable mutations and external events, which bring a whole new level of dynamism and responsiveness to our workflows.


Wondering about the power of Variable Mutations?


Think of variable mutations as the gears that shift your workflow in response to changing conditions. They're the updates and changes to the data your workflow acts upon, ensuring your process stays relevant and accurate to the situation at hand.


In our `ConditionalsExample.java`, we've been managing incidents based on their severity. But what happens when an incident's status changes or is resolved? That's where variable mutations come into play.


wf.registerInterruptHandler("incident-resolved", handler -> {
    handler.mutate(incidentResolved, VariableMutationType.ASSIGN, true);
});

This snippet shows how we can use an interrupt handler to listen for an "incident-resolved" event and then mutate the `incidentResolved` variable to `true`, effectively ending our loop of periodic checks.


External events are like the messengers of the workflow world, bringing news from the outside that can influence the course of our processes. They provide a way for our workflows to interact with the world beyond the code, responding to real-world actions, triggers and/or changes.


In our incident management scenario, an external event might be the resolution of an IT issue, reported by an external system or a user action. Handling this in LittleHorse looks something like this:


// Using LittleHorse CLI to simulate an external event

lhctl postEvent [WF_RUN_ID] incident-resolved BOOL true

By executing this command, we simulate the external event that our workflow is waiting for, which, in turn, triggers the variable mutation we set up earlier.


Bringing It All Together: App.java and IncidentWorker.java

Our `App.java` is responsible for orchestrating the registration of workflows, task workers, and handling external events. It ensures that all parts of our system are ready and responsive to the inputs they receive.


Meanwhile, `IncidentWorker.java` is where the action happens. It's here that we define the tasks our workflow can perform, from verifying incidents to sending alerts and performing periodic checks. Each task is a response to the conditions and events our workflow encounters.


@LHTaskMethod("periodic-check-task")
public String periodicCheckTask(String incidentId) {
    System.out.println("Performing periodic check for Incident ID: " + incidentId);
    return "Periodic Check Completed";
}

Testing Our Workflow with Variable Mutations and External Events


Now that we've integrated variable mutations and external events into our workflow, we can start to see the full picture of how dynamic and responsive our incident management system can be. By simulating an external event using the LittleHorse CLI, we can observe how our workflow adapts, moving from active monitoring to resolution.


Looking Forward


As we wrap up this blog, we've seen how variable mutations and external events add layers of flexibility and responsiveness to our workflows. These concepts are not just crucial for IT Incident Management but can be applied to a wide range of scenarios where workflows need to adapt to changing conditions and external inputs.


Stay tuned for our next post, where we'll tie all these concepts together in a comprehensive summary, showcasing the full potential of LittleHorse in managing complex workflows. Until then, happy coding, and may your workflows be as dynamic and adaptable as ever!


6 views0 comments

Comments


bottom of page