1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * \author guonix
17 * \date Oct 2020
18 * \brief
19 */
20
21#pragma once
22
23#include <list>
24#include <memory>
25#include <mutex>
26#include <vector>
27
28namespace proxima {
29namespace be {
30namespace query {
31
32//! Predefined class
33class Task;
34//! Alias for TaskPtr
35using TaskPtr = std::shared_ptr<Task>;
36using TaskPtrList = std::list<TaskPtr>;
37
38/**
39 * Task interface, most of API's should be readonly, except execute &
40 * wait_finish the priority of updating status grant to TaskController, which
41 * derived by Schedule]
42 */
43class Task {
44 public:
45 /*!
46 * Status indicated the stage of task
47 */
48 enum Status {
49 //! Initialized, wait to schedule.
50 INITIALIZED,
51 //! Has been scheduled, not running yet.
52 SCHEDULED,
53 //! Running, not finished yet.
54 RUNNING,
55 //! Finished,
56 FINISHED,
57 };
58
59 public:
60 //! Destructor
61 virtual ~Task() = default;
62
63 public:
64 //! Retrieve task name
65 virtual const std::string &name() const = 0;
66
67 //! Retrieve exit code off task
68 virtual int exit_code() const = 0;
69
70 //! Run task, 0 for success otherwise failed
71 virtual int run() = 0;
72
73 //! Retrieve the status of task, readonly
74 virtual Status status() const = 0;
75
76 //! Update status
77 virtual void status(Status status) = 0;
78
79 //! true for task in running stage, otherwise return false
80 virtual bool running() const = 0;
81
82 //! true for task has been finished, otherwise return false
83 virtual bool finished() const = 0;
84
85 //! Executes the task object exactly once, even if called concurrently, from
86 // several threads. return immediately if invoke run method after run_once.
87 virtual int run_once() = 0;
88
89 //! Wait until task has been finished, return value same with finished()
90 virtual bool wait_finish() = 0;
91};
92
93
94} // namespace query
95} // namespace be
96} // namespace proxima
97