use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class WorkflowTaskCoordinatorTests method testTaskException.
@Test
public void testTaskException() {
Worker worker = Worker.create("test", task -> {
throw new NoSuchMethodError();
});
TaskClient client = Mockito.mock(TaskClient.class);
WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(1).withWorkerQueueSize(1).withSleepWhenRetry(100000).withUpdateRetryCount(1).withTaskClient(client).withWorkerNamePrefix("test-worker-").build();
when(client.batchPollTasksInDomain(anyString(), isNull(), anyString(), anyInt(), anyInt())).thenReturn(ImmutableList.of(new Task()));
when(client.ack(any(), any())).thenReturn(true);
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
assertEquals("test-worker-0", Thread.currentThread().getName());
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertEquals(TaskResult.Status.FAILED, result.getStatus());
latch.countDown();
return null;
}).when(client).updateTask(any());
coordinator.init();
Uninterruptibles.awaitUninterruptibly(latch);
Mockito.verify(client).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class WorkflowTaskCoordinator method init.
/**
* Starts the polling.
* Must be called after the constructor {@link #WorkflowTaskCoordinator(EurekaClient, TaskClient, int, int, int, int, Iterable, String)}
* or the builder {@link Builder#build()} method
*/
public synchronized void init() {
if (threadCount == -1) {
threadCount = workers.size();
}
logger.info("Initialized the worker with {} threads", threadCount);
MetricsContainer.incrementInitializationCount(this.getClass().getCanonicalName());
this.workerQueue = new LinkedBlockingQueue<Runnable>(workerQueueSize);
AtomicInteger count = new AtomicInteger(0);
this.executorService = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, workerQueue, (runnable) -> {
Thread thread = new Thread(runnable);
thread.setName(workerNamePrefix + count.getAndIncrement());
return thread;
});
this.scheduledExecutorService = Executors.newScheduledThreadPool(workers.size());
workers.forEach(worker -> {
scheduledExecutorService.scheduleWithFixedDelay(() -> pollForTask(worker), worker.getPollingInterval(), worker.getPollingInterval(), TimeUnit.MILLISECONDS);
});
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class TaskPollExecutorTest method testLargePayloadCanFailUpdateWithRetry.
@Test
public void testLargePayloadCanFailUpdateWithRetry() {
Task task = testTask();
Worker worker = mock(Worker.class);
when(worker.getPollingInterval()).thenReturn(3000);
when(worker.getTaskDefName()).thenReturn(TEST_TASK_DEF_NAME);
when(worker.execute(any())).thenReturn(new TaskResult(task));
TaskClient taskClient = Mockito.mock(TaskClient.class);
when(taskClient.pollTask(any(), any(), any())).thenReturn(task);
when(taskClient.ack(any(), any())).thenReturn(true);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertNull(result.getReasonForIncompletion());
result.setReasonForIncompletion("some_reason");
throw new ConductorClientException();
}).when(taskClient).evaluateAndUploadLargePayload(any(TaskResult.class), any());
TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 3, new HashMap<>(), "test-worker-");
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(worker).onErrorUpdate(any());
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
Uninterruptibles.awaitUninterruptibly(latch);
// When evaluateAndUploadLargePayload fails indefinitely, task update shouldn't be called.
verify(taskClient, times(0)).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class TaskPollExecutorTest method testTaskPollException.
@Test
public void testTaskPollException() {
Task task = testTask();
Worker worker = mock(Worker.class);
when(worker.getPollingInterval()).thenReturn(3000);
when(worker.getTaskDefName()).thenReturn("test");
when(worker.execute(any())).thenReturn(new TaskResult(task));
TaskClient taskClient = Mockito.mock(TaskClient.class);
when(taskClient.pollTask(any(), any(), any())).thenThrow(ConductorClientException.class).thenReturn(task);
TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-");
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertEquals(IN_PROGRESS, result.getStatus());
assertEquals(task.getTaskId(), result.getTaskId());
latch.countDown();
return null;
}).when(taskClient).updateTask(any());
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
Uninterruptibles.awaitUninterruptibly(latch);
verify(taskClient).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class TaskPollExecutorTest method testTaskPollDomain.
@Test
public void testTaskPollDomain() {
TaskClient taskClient = Mockito.mock(TaskClient.class);
String testDomain = "foo";
Map<String, String> taskToDomain = new HashMap<>();
taskToDomain.put(TEST_TASK_DEF_NAME, testDomain);
TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, taskToDomain, "test-worker-");
String workerName = "test-worker";
Worker worker = mock(Worker.class);
when(worker.getTaskDefName()).thenReturn(TEST_TASK_DEF_NAME);
when(worker.getIdentity()).thenReturn(workerName);
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(taskClient).pollTask(TEST_TASK_DEF_NAME, workerName, testDomain);
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
Uninterruptibles.awaitUninterruptibly(latch);
verify(taskClient).pollTask(TEST_TASK_DEF_NAME, workerName, testDomain);
}
Aggregations