Skip to content

Commit

Permalink
Add sample worker for failing
Browse files Browse the repository at this point in the history
  • Loading branch information
boney9 committed Sep 27, 2023
1 parent df0bb68 commit db48a5c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/main/java/io/orkes/samples/workers/FailNTimesWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.orkes.samples.workers;

import com.netflix.conductor.client.worker.Worker;
import com.netflix.conductor.common.metadata.tasks.Task;
import com.netflix.conductor.common.metadata.tasks.TaskResult;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Map;

@Component
public class FailNTimesWorker implements Worker {
@Override
public String getTaskDefName() {
return "fail_n_times";
}

@Override
public TaskResult execute(Task task) {
TaskResult result = new TaskResult(task);
if(!task.isRetried()) {
result.addOutputData("initialStartTime", Instant.ofEpochMilli(task.getStartTime()).toString());
result.addOutputData("initialTaskExecutionTime", Instant.now().toString());
result.addOutputData("initialScheduledTime", Instant.ofEpochMilli(task.getScheduledTime()).toString());
}
int timesToFail = getIntValue("timesToFail", 1, task.getInputData());
int failedCount = getIntValue("failedCount", 0, task.getOutputData());
if(failedCount >= timesToFail) {
result.addOutputData("outputVal", "This task completed failing for configured number of times - " + timesToFail);
result.setStatus(TaskResult.Status.COMPLETED);
}
result.addOutputData("failedCount", ++failedCount);
result.addOutputData("outputVal", "This task fails (for testing) for " + timesToFail + " times");
result.setStatus(TaskResult.Status.FAILED);
return result;
}

private static int getIntValue(String paramName, int defaultValue, Map<String, Object> data) {
return ((Number) (data.getOrDefault(paramName, defaultValue))).intValue();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
conductor.server.url=https://play.orkes.io/api

conductor.security.client.key-id=b274eb68-0630-4d82-b8ff-266d3f137344
conductor.security.client.secret=yRpxiEOa4tFF9 iKlNFXyUf9V437KbuUQSYvvHW5GuSf7LL8o
conductor.security.client.key-id=_CHANGE_ME_
conductor.security.client.secret=_CHANGE_ME_

0 comments on commit db48a5c

Please sign in to comment.