Running a Worker Until Condition is met
Introduction - Custom Poll Worker
We can use a worker and keep it running until a condition is met. This is similar to the HTTP Poll task, where you can poll an HTTP endpoint until a specific condition is met. Having a worker do the same is useful when you don't have an endpoint to call directly from Conductor.
Code Sample
@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;
}
In this example worker, we use the callbackAfterSeconds
attribute to keep the worker in progress
until a condition is met. This can be any arbitrary condition that our code defines.
Specifically, this example waits for the poll count to reach the input parameter pollCounter
.
When you return a task as IN_PROGRESS
with a callbackAfterSeconds
value set, Conductor
will schedule the same task instance to be polled exactly after the callbackAfterSeconds
value.
We are leveraging the output data to hold the context across polls, and once it reaches the desired state, we exit IN_PROGRESS
and mark the task as COMPLETED
.
Go ahead and try this worker in a workflow in our Developer Edition environment. We have already configured this in our sandbox, so it should run when you test.
Such workers can be implemented in any of the languages.