Search in sources :

Example 6 with TaskInfo

use of com.enonic.xp.task.TaskInfo in project xp by enonic.

the class LocalTaskManagerImpl method updateProgress.

private void updateProgress(final TaskId taskId, final String message) {
    final TaskInfoHolder ctx = tasks.get(taskId);
    if (ctx == null) {
        return;
    }
    final TaskInfo taskInfo = ctx.getTaskInfo();
    final TaskProgress updatedProgress = taskInfo.getProgress().copy().info(message).build();
    final TaskInfo updatedInfo = taskInfo.copy().progress(updatedProgress).build();
    final TaskInfoHolder updatedCtx = ctx.copy().taskInfo(updatedInfo).build();
    tasks.put(taskId, updatedCtx);
    eventPublisher.publish(TaskEvents.updated(updatedInfo));
}
Also used : TaskInfo(com.enonic.xp.task.TaskInfo) TaskProgress(com.enonic.xp.task.TaskProgress)

Example 7 with TaskInfo

use of com.enonic.xp.task.TaskInfo in project xp by enonic.

the class LocalTaskManagerImpl method prepareRunnable.

private Runnable prepareRunnable(final DescribedTask runnableTask) {
    final TaskId id = runnableTask.getTaskId();
    final User user = Objects.requireNonNullElse(runnableTask.getTaskContext().getAuthInfo().getUser(), User.ANONYMOUS);
    final TaskInfo info = TaskInfo.create().id(id).description(runnableTask.getDescription()).name(runnableTask.getName()).state(TaskState.WAITING).startTime(Instant.now(clock)).application(runnableTask.getApplicationKey()).user(user.getKey()).build();
    final TaskInfoHolder taskInfoHolder = TaskInfoHolder.create().taskInfo(info).build();
    tasks.put(id, taskInfoHolder);
    eventPublisher.publish(TaskEvents.submitted(info));
    return new TaskRunnable(runnableTask, new ProgressReporterAdapter(id));
}
Also used : TaskInfo(com.enonic.xp.task.TaskInfo) TaskId(com.enonic.xp.task.TaskId) User(com.enonic.xp.security.User)

Example 8 with TaskInfo

use of com.enonic.xp.task.TaskInfo in project xp by enonic.

the class LocalTaskManagerImpl method removeExpiredTasks.

private void removeExpiredTasks() {
    final Instant now = Instant.now(clock);
    for (TaskInfoHolder taskCtx : tasks.values()) {
        final TaskInfo taskInfo = taskCtx.getTaskInfo();
        if (taskInfo.isDone() && taskCtx.getDoneTime() != null && taskCtx.getDoneTime().until(now, ChronoUnit.SECONDS) > KEEP_COMPLETED_MAX_TIME_SEC) {
            tasks.remove(taskInfo.getId());
            eventPublisher.publish(TaskEvents.removed(taskInfo));
        }
    }
}
Also used : TaskInfo(com.enonic.xp.task.TaskInfo) Instant(java.time.Instant)

Example 9 with TaskInfo

use of com.enonic.xp.task.TaskInfo in project xp by enonic.

the class TaskEventsTest method updated.

@Test
public void updated() {
    TaskInfo taskInfo = TaskInfo.create().id(TaskId.from("task1")).name("name1").description("Task1 description").application(ApplicationKey.from("com.enonic.myapp")).user(PrincipalKey.from("user:store:me")).startTime(Instant.parse("2017-10-01T09:00:00Z")).state(TaskState.RUNNING).build();
    final Event event = TaskEvents.updated(taskInfo);
    assertEquals(TaskEvents.TASK_UPDATED_EVENT, event.getType());
    assertEquals("task1", event.getValueAs(String.class, "id").get());
    assertEquals("name1", event.getValueAs(String.class, "name").get());
    assertEquals("Task1 description", event.getValueAs(String.class, "description").get());
    assertEquals("RUNNING", event.getValueAs(String.class, "state").get());
    assertEquals("com.enonic.myapp", event.getValueAs(String.class, "application").get());
    assertEquals("user:store:me", event.getValueAs(String.class, "user").get());
    assertEquals("2017-10-01T09:00:00Z", event.getValueAs(String.class, "startTime").get());
}
Also used : TaskInfo(com.enonic.xp.task.TaskInfo) Event(com.enonic.xp.event.Event) Test(org.junit.jupiter.api.Test)

Example 10 with TaskInfo

use of com.enonic.xp.task.TaskInfo in project xp by enonic.

the class TaskResource method getTask.

@GET
@Path("/{taskId}")
public TaskInfoJson getTask(@PathParam("taskId") final String taskIdString) {
    final TaskId taskId = TaskId.from(taskIdString);
    final TaskInfo taskInfo = taskService.getTaskInfo(taskId);
    if (taskInfo == null) {
        throw new WebApplicationException(String.format("Task [%s] was not found", taskIdString), Response.Status.NOT_FOUND);
    }
    return new TaskInfoJson(taskInfo);
}
Also used : TaskInfo(com.enonic.xp.task.TaskInfo) TaskInfoJson(com.enonic.xp.task.TaskInfoJson) TaskId(com.enonic.xp.task.TaskId) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

TaskInfo (com.enonic.xp.task.TaskInfo)25 Test (org.junit.jupiter.api.Test)15 TaskId (com.enonic.xp.task.TaskId)9 Event (com.enonic.xp.event.Event)6 TasksReporterCallable (com.enonic.xp.impl.task.distributed.TasksReporterCallable)5 Member (com.hazelcast.core.Member)4 Instant (java.time.Instant)3 List (java.util.List)3 TaskProgress (com.enonic.xp.task.TaskProgress)2 TaskService (com.enonic.xp.task.TaskService)2 TaskState (com.enonic.xp.task.TaskState)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Branch (com.enonic.xp.branch.Branch)1 RecurringJob (com.enonic.xp.core.internal.concurrent.RecurringJob)1 OsgiSupportMock (com.enonic.xp.core.internal.osgi.OsgiSupportMock)1 AllTasksReporter (com.enonic.xp.impl.task.distributed.AllTasksReporter)1 DescribedTask (com.enonic.xp.impl.task.distributed.DescribedTask)1