Search in sources :

Example 1 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class TaskAPI method list.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public Map<String, Object> list(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("status") String status, @QueryParam("ids") List<Long> ids, @QueryParam("limit") @DefaultValue("100") long limit, @QueryParam("page") String page) {
    LOG.debug("Graph [{}] list tasks with status {}, ids {}, " + "limit {}, page {}", graph, status, ids, limit, page);
    TaskScheduler scheduler = graph(manager, graph).taskScheduler();
    Iterator<HugeTask<Object>> iter;
    if (!ids.isEmpty()) {
        E.checkArgument(status == null, "Not support status when query task by ids, " + "but got status='%s'", status);
        E.checkArgument(page == null, "Not support page when query task by ids, " + "but got page='%s'", page);
        // Set limit to NO_LIMIT to ignore limit when query task by ids
        limit = NO_LIMIT;
        List<Id> idList = ids.stream().map(IdGenerator::of).collect(Collectors.toList());
        iter = scheduler.tasks(idList);
    } else {
        if (status == null) {
            iter = scheduler.tasks(null, limit, page);
        } else {
            iter = scheduler.tasks(parseStatus(status), limit, page);
        }
    }
    List<Object> tasks = new ArrayList<>();
    while (iter.hasNext()) {
        tasks.add(iter.next().asMap(false));
    }
    if (limit != NO_LIMIT && tasks.size() > limit) {
        tasks = tasks.subList(0, (int) limit);
    }
    if (page == null) {
        return Maps.of("tasks", tasks);
    } else {
        return Maps.of("tasks", tasks, "page", PageInfo.pageInfo(iter));
    }
}
Also used : HugeTask(com.baidu.hugegraph.task.HugeTask) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 2 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class TaskExample method testTask.

public static void testTask(HugeGraph graph) throws InterruptedException {
    Id id = IdGenerator.of(8);
    String callable = "com.baidu.hugegraph.example.TaskExample$TestTask";
    HugeTask<?> task = new HugeTask<>(id, null, callable, "test-parameter");
    task.type("type-1");
    task.name("test-task");
    TaskScheduler scheduler = graph.taskScheduler();
    scheduler.schedule(task);
    scheduler.save(task);
    Iterator<HugeTask<Object>> iter;
    iter = scheduler.tasks(TaskStatus.RUNNING, -1, null);
    LOG.info(">>>> running task: {}", IteratorUtils.toList(iter));
    Thread.sleep(TestTask.UNIT * 33);
    task.cancel(true);
    Thread.sleep(TestTask.UNIT * 1);
    scheduler.save(task);
    // Find task not finished(actually it should be RUNNING)
    iter = scheduler.tasks(TaskStatus.CANCELLED, -1, null);
    assert iter.hasNext();
    task = iter.next();
    LOG.info(">>>> task may be interrupted");
    Thread.sleep(TestTask.UNIT * 10);
    LOG.info(">>>> restore task...");
    Whitebox.setInternalState(task, "status", TaskStatus.RUNNING);
    scheduler.restoreTasks();
    Thread.sleep(TestTask.UNIT * 80);
    scheduler.save(task);
    iter = scheduler.tasks(TaskStatus.SUCCESS, -1, null);
    assert iter.hasNext();
    task = iter.next();
    assert task.status() == TaskStatus.SUCCESS;
    assert task.retries() == 1;
}
Also used : HugeTask(com.baidu.hugegraph.task.HugeTask) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler)

Example 3 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class ExampleUtil method waitAllTaskDone.

public static void waitAllTaskDone(HugeGraph graph) {
    TaskScheduler scheduler = graph.taskScheduler();
    Iterator<HugeTask<Object>> tasks = scheduler.tasks(null, -1L, null);
    while (tasks.hasNext()) {
        try {
            scheduler.waitUntilTaskCompleted(tasks.next().id(), 20L);
        } catch (TimeoutException e) {
            throw new HugeException("Failed to wait task done", e);
        }
    }
}
Also used : HugeTask(com.baidu.hugegraph.task.HugeTask) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) HugeException(com.baidu.hugegraph.HugeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class JobBuilder method schedule.

public HugeTask<V> schedule() {
    E.checkArgumentNotNull(this.name, "Job name can't be null");
    E.checkArgumentNotNull(this.job, "Job callable can't be null");
    E.checkArgument(this.job instanceof TaskCallable, "Job must be instance of TaskCallable");
    this.graph.taskScheduler().checkRequirement("schedule");
    @SuppressWarnings("unchecked") TaskCallable<V> job = (TaskCallable<V>) this.job;
    HugeTask<V> task = new HugeTask<>(this.genTaskId(), null, job);
    task.type(this.job.type());
    task.name(this.name);
    if (this.input != null) {
        task.input(this.input);
    }
    if (this.dependencies != null && !this.dependencies.isEmpty()) {
        for (Id depend : this.dependencies) {
            task.depends(depend);
        }
    }
    TaskScheduler scheduler = this.graph.taskScheduler();
    scheduler.schedule(task);
    return task;
}
Also used : HugeTask(com.baidu.hugegraph.task.HugeTask) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) TaskCallable(com.baidu.hugegraph.task.TaskCallable)

Example 5 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class EphemeralJobBuilder method schedule.

public HugeTask<V> schedule() {
    E.checkArgumentNotNull(this.name, "Job name can't be null");
    E.checkArgumentNotNull(this.job, "Job can't be null");
    HugeTask<V> task = new HugeTask<>(this.genTaskId(), null, this.job);
    task.type(this.job.type());
    task.name(this.name);
    if (this.input != null) {
        task.input(this.input);
    }
    TaskScheduler scheduler = this.graph.taskScheduler();
    scheduler.schedule(task);
    return task;
}
Also used : HugeTask(com.baidu.hugegraph.task.HugeTask) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler)

Aggregations

HugeTask (com.baidu.hugegraph.task.HugeTask)8 TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)8 Id (com.baidu.hugegraph.backend.id.Id)5 HugeGraph (com.baidu.hugegraph.HugeGraph)3 TaskCallable (com.baidu.hugegraph.task.TaskCallable)2 Test (org.junit.Test)2 HugeException (com.baidu.hugegraph.HugeException)1 Timed (com.codahale.metrics.annotation.Timed)1 GET (jakarta.ws.rs.GET)1 Produces (jakarta.ws.rs.Produces)1 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 Before (org.junit.Before)1