Search in sources :

Example 1 with TaskManager

use of com.enonic.xp.impl.task.distributed.TaskManager in project xp by enonic.

the class TaskServiceImplTest method getTasks.

@Test
void getTasks() {
    final TaskId someTaskId1 = TaskId.from("someTask1");
    taskService.getTaskInfo(someTaskId1);
    verify(taskManager).getTaskInfo(eq(someTaskId1));
    final TaskManager taskManager = mock(TaskManager.class);
    taskService.setClusteredTaskManager(taskManager);
    taskService.getAllTasks();
    verify(taskManager).getAllTasks();
    taskService.unsetClusteredTaskManager(taskManager);
    taskService.getRunningTasks();
    verifyNoMoreInteractions(taskManager);
    verify(this.taskManager).getRunningTasks();
}
Also used : TaskId(com.enonic.xp.task.TaskId) TaskManager(com.enonic.xp.impl.task.distributed.TaskManager) Test(org.junit.jupiter.api.Test)

Example 2 with TaskManager

use of com.enonic.xp.impl.task.distributed.TaskManager in project xp by enonic.

the class TaskServiceImplTest method submitTask_DescriptorKey_offload_to_clustered.

@Test
void submitTask_DescriptorKey_offload_to_clustered() {
    final DescriptorKey descriptorKey = DescriptorKey.from("module:my-admin-tool");
    final TaskManager clusteredTaskManager = mock(TaskManager.class);
    taskService.setClusteredTaskManager(clusteredTaskManager);
    final TaskId taskId = taskService.submitTask(SubmitTaskParams.create().descriptorKey(descriptorKey).build());
    verify(clusteredTaskManager).submitTask(describedTaskCaptor.capture());
    describedTaskCaptor.getValue();
    final DescribedTask argument = describedTaskCaptor.getValue();
    assertEquals(taskId, argument.getTaskId());
}
Also used : TaskManager(com.enonic.xp.impl.task.distributed.TaskManager) TaskId(com.enonic.xp.task.TaskId) DescriptorKey(com.enonic.xp.page.DescriptorKey) DescribedTask(com.enonic.xp.impl.task.distributed.DescribedTask) Test(org.junit.jupiter.api.Test)

Example 3 with TaskManager

use of com.enonic.xp.impl.task.distributed.TaskManager in project xp by enonic.

the class ClusteredTaskManagerImpl method send.

private List<TaskInfo> send(final SerializableFunction<TaskManager, List<TaskInfo>> taskFunction) {
    final List<TaskInfo> taskInfoBuilder = new ArrayList<>();
    final Map<Member, Future<List<TaskInfo>>> resultsFromMembers = executorService.submitToAllMembers(new TasksReporterCallable(taskFunction));
    for (Future<List<TaskInfo>> responseFuture : resultsFromMembers.values()) {
        try {
            final List<TaskInfo> response = responseFuture.get(outboundTimeoutNs, TimeUnit.NANOSECONDS);
            taskInfoBuilder.addAll(response);
        } catch (TimeoutException e) {
            resultsFromMembers.values().forEach(f -> f.cancel(true));
            throw new RuntimeException(e);
        } catch (InterruptedException | ExecutionException e) {
            throw ExceptionUtil.rethrow(e);
        }
    }
    return taskInfoBuilder;
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) AllTasksReporter(com.enonic.xp.impl.task.distributed.AllTasksReporter) SingleTaskReporter(com.enonic.xp.impl.task.distributed.SingleTaskReporter) TaskInfo(com.enonic.xp.task.TaskInfo) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Component(org.osgi.service.component.annotations.Component) DescribedTask(com.enonic.xp.impl.task.distributed.DescribedTask) Duration(java.time.Duration) Map(java.util.Map) Activate(org.osgi.service.component.annotations.Activate) TaskManager(com.enonic.xp.impl.task.distributed.TaskManager) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TasksReporterCallable(com.enonic.xp.impl.task.distributed.TasksReporterCallable) MemberSelector(com.hazelcast.core.MemberSelector) Deactivate(org.osgi.service.component.annotations.Deactivate) ExceptionUtil(com.hazelcast.util.ExceptionUtil) RunningTasksReporter(com.enonic.xp.impl.task.distributed.RunningTasksReporter) Member(com.hazelcast.core.Member) BundleContext(org.osgi.framework.BundleContext) ApplicationKey(com.enonic.xp.app.ApplicationKey) OffloadedTaskCallable(com.enonic.xp.impl.task.distributed.OffloadedTaskCallable) SerializableFunction(com.enonic.xp.impl.task.distributed.SerializableFunction) TaskId(com.enonic.xp.task.TaskId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IExecutorService(com.hazelcast.core.IExecutorService) Modified(org.osgi.service.component.annotations.Modified) Reference(org.osgi.service.component.annotations.Reference) ArrayList(java.util.ArrayList) TaskInfo(com.enonic.xp.task.TaskInfo) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) TasksReporterCallable(com.enonic.xp.impl.task.distributed.TasksReporterCallable) ExecutionException(java.util.concurrent.ExecutionException) Member(com.hazelcast.core.Member) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with TaskManager

use of com.enonic.xp.impl.task.distributed.TaskManager in project xp by enonic.

the class TaskServiceImpl method submitTask.

@Override
public TaskId submitTask(SubmitTaskParams params) {
    TaskManager taskManager;
    taskManager = clusteredTaskManagerRef.getNow(null);
    if (taskManager == null) {
        if (acceptOffloaded) {
            LOG.debug("Clustered task manager is unavailable, falling back to local task manager.");
            taskManager = localTaskManager;
        } else {
            try {
                LOG.info("Clustered task manager is unavailable, waiting...");
                taskManager = clusteredTaskManagerRef.get(5, TimeUnit.SECONDS);
            } catch (InterruptedException | TimeoutException e) {
                throw new RuntimeException(e);
            }
        }
    }
    final DistributableTask task = new DistributableTask(params.getDescriptorKey(), params.getData(), buildContext());
    taskManager.submitTask(task);
    return task.getTaskId();
}
Also used : TaskManager(com.enonic.xp.impl.task.distributed.TaskManager) DistributableTask(com.enonic.xp.impl.task.distributed.DistributableTask) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with TaskManager

use of com.enonic.xp.impl.task.distributed.TaskManager in project xp by enonic.

the class TaskServiceImplTest method submitTask_DescriptorKey_offload_to_clustered_wait_success.

@Test
void submitTask_DescriptorKey_offload_to_clustered_wait_success() {
    final DescriptorKey descriptorKey = DescriptorKey.from("module:my-admin-tool");
    final TaskManager clusteredTaskManager = mock(TaskManager.class);
    when(taskConfig.distributable_acceptInbound()).thenReturn(false);
    taskService.activate(taskConfig);
    CompletableFuture.runAsync(() -> taskService.setClusteredTaskManager(clusteredTaskManager), CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));
    final TaskId taskId = taskService.submitTask(SubmitTaskParams.create().descriptorKey(descriptorKey).build());
    verify(clusteredTaskManager).submitTask(describedTaskCaptor.capture());
    describedTaskCaptor.getValue();
    final DescribedTask argument = describedTaskCaptor.getValue();
    assertEquals(taskId, argument.getTaskId());
}
Also used : TaskManager(com.enonic.xp.impl.task.distributed.TaskManager) TaskId(com.enonic.xp.task.TaskId) DescriptorKey(com.enonic.xp.page.DescriptorKey) DescribedTask(com.enonic.xp.impl.task.distributed.DescribedTask) Test(org.junit.jupiter.api.Test)

Aggregations

TaskManager (com.enonic.xp.impl.task.distributed.TaskManager)5 TaskId (com.enonic.xp.task.TaskId)4 DescribedTask (com.enonic.xp.impl.task.distributed.DescribedTask)3 Test (org.junit.jupiter.api.Test)3 DescriptorKey (com.enonic.xp.page.DescriptorKey)2 TimeoutException (java.util.concurrent.TimeoutException)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 AllTasksReporter (com.enonic.xp.impl.task.distributed.AllTasksReporter)1 DistributableTask (com.enonic.xp.impl.task.distributed.DistributableTask)1 OffloadedTaskCallable (com.enonic.xp.impl.task.distributed.OffloadedTaskCallable)1 RunningTasksReporter (com.enonic.xp.impl.task.distributed.RunningTasksReporter)1 SerializableFunction (com.enonic.xp.impl.task.distributed.SerializableFunction)1 SingleTaskReporter (com.enonic.xp.impl.task.distributed.SingleTaskReporter)1 TasksReporterCallable (com.enonic.xp.impl.task.distributed.TasksReporterCallable)1 TaskInfo (com.enonic.xp.task.TaskInfo)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 IExecutorService (com.hazelcast.core.IExecutorService)1 Member (com.hazelcast.core.Member)1 MemberSelector (com.hazelcast.core.MemberSelector)1 ExceptionUtil (com.hazelcast.util.ExceptionUtil)1