Skip to main content

Running a Worker Until a Condition is met

This tutorial demonstrates how to create a custom polling worker that runs continuously until a specific condition is met.

You’ll implement this pattern using the Orkes Conductor SDK and learn how to keep a worker active by setting callbackAfterSeconds, allowing Conductor to requeue it automatically until your logic determines that completion has been reached.

This approach is useful when:

  • Your workflow depends on an external condition or background job.
  • You need to monitor progress without repeatedly invoking APIs.
  • You want the worker itself to decide when a task is complete.
Prerequisites:

The polling workflow

Use the following workflow definition to create a workflow in Orkes Conductor.

Step 1: Create a workflow in Orkes Conductor

To create a workflow:

  1. Go to Definitions > Workflow from the left navigation menu on your Conductor cluster.
  2. Select + Define workflow.
  3. In the Code tab, paste the following code:
{
"name": "poll-until-condition-workflow",
"description": "This example shows how we can use a worker and keep it running until a condition is true.",
"version": 1,
"tasks": [
{
"name": "poll-until-condition-matches-full-worker",
"taskReferenceName": "poll-until-condition-matches-full-worker",
"type": "SIMPLE",
"inputParameters": {
"pollCounter": "${workflow.input.pollCounter}",
"pollIntervalSeconds": "${workflow.input.pollIntervalSeconds}"
}
}
],
"inputParameters": [
"pollCounter",
"pollIntervalSeconds"
],
"schemaVersion": 2
}
  1. Select Save > Confirm.

Next, create an application in Orkes Conductor. This application provides the access keys (key and secret) that your SDK uses to authenticate and run the workflow.

Step 2: Create an application in Orkes Conductor

To create an application:

  1. Go to Access Control > Applications from the left navigation menu on your Conductor cluster.
  2. Create an application and get the access keys.
  3. Assign EXECUTE permission to the workflow created in the previous step.

Once done, authenticate the SDK project with the generated access keys.

Step 3: Create a worker

Use the Java SDK to create a worker that keeps running until a condition is met. Create a file named PollUntilConditionMeetsWorker.java and add the following code:

Complete source file on Github: .../workers/PollUntilConditionMeetsWorker.java
  @Override
public TaskResult execute(Task task) {
TaskResult taskResult = new TaskResult(task);
if (!task.getInputData().containsKey(POLL_COUNTER)) {
taskResult.addOutputData("message", "pollCounter param not found in input, will use default of " + defaultPollCount + " polls");
}

int pollCounter = Math.min(10, castToInt(task.getInputData().getOrDefault(POLL_COUNTER, defaultPollCount)));
int pollIntervalSeconds = Math.min(10, castToInt(task.getInputData().getOrDefault(POLL_INTERVAL_SECONDS, 5)));

// Add these to the output for context
taskResult.addOutputData(POLL_INTERVAL_SECONDS, pollIntervalSeconds + " (this test task has a max limit of 10 seconds)");
taskResult.addOutputData(POLL_COUNTER, pollCounter + " (this test task has a max limit of 10 iterations)");

// We can read current iteration from the task output as the data will be retained on the worker when polled
int currentIteration = castToInt(taskResult.getOutputData().getOrDefault(CURRENT_ITERATION, 0));

// Increment the current iteration and set to the task output
taskResult.addOutputData(CURRENT_ITERATION, ++currentIteration);
taskResult.addOutputData("updatedTime", new Date().toString());

// While condition is not met, keep task in progress
if (currentIteration < pollCounter) {
taskResult.setStatus(TaskResult.Status.IN_PROGRESS);
// Set to configured seconds to callback, and you can set this to any value as per the requirements
taskResult.setCallbackAfterSeconds(pollIntervalSeconds);
return taskResult;
}

// Set task as completed now that the poll count condition is met
taskResult.setStatus(TaskResult.Status.COMPLETED);
return taskResult;
}

This worker uses the callbackAfterSeconds attribute to control re-execution.

Here’s how it works:

  • The worker tracks the current iteration in its output data.
  • On each execution, it increments the iteration count.
  • If the currentIteration is less than pollCounter, it sets the task status to IN_PROGRESS.
  • Conductor automatically re-queues the task after the configured pollIntervalSeconds.
  • When the condition is met (iteration reaches pollCounter), the task is marked COMPLETED.

This approach maintains the worker’s state across polls without requiring an external data store.

Note

Such workers can be implemented in any language supported by Orkes Conductor SDKs.