Search in sources :

Example 6 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler 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 7 with TaskScheduler

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

the class TaskAPI method update.

@PUT
@Timed
@Path("{id}")
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public Map<String, Object> update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") long id, @QueryParam("action") String action) {
    LOG.debug("Graph [{}] cancel task: {}", graph, id);
    if (!ACTION_CANCEL.equals(action)) {
        throw new NotSupportedException(String.format("Not support action '%s'", action));
    }
    TaskScheduler scheduler = graph(manager, graph).taskScheduler();
    HugeTask<?> task = scheduler.task(IdGenerator.of(id));
    if (!task.completed() && !task.cancelling()) {
        scheduler.cancel(task);
        if (task.cancelling() || task.cancelled()) {
            return task.asMap();
        }
    }
    assert task.completed() || task.cancelling();
    throw new BadRequestException(String.format("Can't cancel task '%s' which is completed or cancelling", id));
}
Also used : BadRequestException(jakarta.ws.rs.BadRequestException) NotSupportedException(jakarta.ws.rs.NotSupportedException) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Path(jakarta.ws.rs.Path) TaskStatus(com.baidu.hugegraph.task.TaskStatus) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(jakarta.ws.rs.PUT)

Example 8 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler 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 9 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler 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 10 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler 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)

Aggregations

TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)21 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Test (org.junit.Test)10 HugeTask (com.baidu.hugegraph.task.HugeTask)8 Id (com.baidu.hugegraph.backend.id.Id)7 Timed (com.codahale.metrics.annotation.Timed)4 Path (jakarta.ws.rs.Path)3 Produces (jakarta.ws.rs.Produces)3 HugeException (com.baidu.hugegraph.HugeException)2 GremlinJob (com.baidu.hugegraph.job.GremlinJob)2 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)2 TaskCallable (com.baidu.hugegraph.task.TaskCallable)2 GET (jakarta.ws.rs.GET)2 TimeoutException (java.util.concurrent.TimeoutException)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 GremlinRequest (com.baidu.hugegraph.api.job.GremlinAPI.GremlinRequest)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 TaskStatus (com.baidu.hugegraph.task.TaskStatus)1 BadRequestException (jakarta.ws.rs.BadRequestException)1