| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2026, Stefan Meislinger | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Microservice task interfaces | ||
| 31 | * @ingroup gatetech_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_TECH_TASKS_HPP_INCLUDED | ||
| 35 | #define GATE_TECH_TASKS_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/tech/gate_tech_api.hpp" | ||
| 38 | #include "gate/runnables.hpp" | ||
| 39 | #include "gate/properties.hpp" | ||
| 40 | #include "gate/wrappers.hpp" | ||
| 41 | #include "gate/maps.hpp" | ||
| 42 | #include "gate/times.hpp" | ||
| 43 | #include "gate/threading.hpp" | ||
| 44 | #include "gate/threadpools.hpp" | ||
| 45 | |||
| 46 | namespace gate | ||
| 47 | { | ||
| 48 | namespace tech | ||
| 49 | { | ||
| 50 | |||
| 51 | /// @brief | ||
| 52 | class GATE_TECH_CPP_API ITaskFactory | ||
| 53 | { | ||
| 54 | public: | ||
| 55 | virtual ~ITaskFactory() noexcept; | ||
| 56 | |||
| 57 | virtual String getName() const = 0; | ||
| 58 | virtual Property const& getDefaultParameters() const = 0; | ||
| 59 | virtual String getDescription(Property const& parameters) = 0; | ||
| 60 | virtual Task createTask(Property const& parameters) = 0; | ||
| 61 | }; | ||
| 62 | |||
| 63 | |||
| 64 | /// @brief | ||
| 65 | class GATE_TECH_CPP_API TaskFactoryBase : public ITaskFactory | ||
| 66 | { | ||
| 67 | public: | ||
| 68 | TaskFactoryBase(String const& taskname, String const& description, Property const& defaultParams); | ||
| 69 | virtual ~TaskFactoryBase() noexcept; | ||
| 70 | |||
| 71 | virtual String getName() const override; | ||
| 72 | virtual Property const& getDefaultParameters() const override; | ||
| 73 | virtual String getDescription(Property const& parameters) override; | ||
| 74 | |||
| 75 | private: | ||
| 76 | String name; | ||
| 77 | String description; | ||
| 78 | Property defaultParameters; | ||
| 79 | }; | ||
| 80 | |||
| 81 | typedef Ref<ITaskFactory> TaskFactory; | ||
| 82 | |||
| 83 | |||
| 84 | /// @brief | ||
| 85 | class GATE_TECH_CPP_API ManagedTask : public IRunnable, private NonCopyable | ||
| 86 | { | ||
| 87 | public: | ||
| 88 | struct GATE_TECH_CPP_API Statistics | ||
| 89 | { | ||
| 90 | size_t started; | ||
| 91 | size_t completed; | ||
| 92 | size_t failed; | ||
| 93 | TimeCounter lastStartTimestamp; | ||
| 94 | Time lastStartTime; | ||
| 95 | time::Milliseconds lastExecutionTime; | ||
| 96 | time::Milliseconds totalExecutionTime; | ||
| 97 | |||
| 98 | Statistics(); | ||
| 99 | }; | ||
| 100 | |||
| 101 | typedef uintptr_t id_t; | ||
| 102 | |||
| 103 | ManagedTask(id_t id, Ref<ThreadPool> workers, TaskFactory factory, Property const& parameters, time::Milliseconds autoRepeat = time::Milliseconds(0)); | ||
| 104 | ~ManagedTask() noexcept; | ||
| 105 | |||
| 106 | id_t getId(); | ||
| 107 | Time getLastStarted(); | ||
| 108 | TimeCounter getLastStartedTimestamp(); | ||
| 109 | Time getNextPlannedStart(); | ||
| 110 | TimeCounter getNextPlannedStartTimestamp(); | ||
| 111 | Task::StatusEnum getStatus(); | ||
| 112 | |||
| 113 | public: | ||
| 114 | virtual void run(); | ||
| 115 | |||
| 116 | private: | ||
| 117 | id_t id; | ||
| 118 | Ref<ThreadPool> threadpool; | ||
| 119 | TaskFactory factory; | ||
| 120 | Property parameters; | ||
| 121 | time::Milliseconds repeatInterval; | ||
| 122 | Mutex mutex; | ||
| 123 | Task task; | ||
| 124 | Statistics stats; | ||
| 125 | }; | ||
| 126 | |||
| 127 | typedef Ref<ManagedTask> ManagedTaskRef; | ||
| 128 | |||
| 129 | |||
| 130 | /// @brief | ||
| 131 | struct GATE_TECH_CPP_API TaskConfig | ||
| 132 | { | ||
| 133 | String description; | ||
| 134 | String taskName; | ||
| 135 | Property parameters; | ||
| 136 | time::Milliseconds repeatInterval; | ||
| 137 | }; | ||
| 138 | |||
| 139 | |||
| 140 | /// @brief | ||
| 141 | class GATE_TECH_CPP_API TaskManager : private NonCopyable | ||
| 142 | { | ||
| 143 | public: | ||
| 144 | typedef ManagedTask::id_t id_t; | ||
| 145 | |||
| 146 | struct Info | ||
| 147 | { | ||
| 148 | String name; | ||
| 149 | String description; | ||
| 150 | Task::StatusEnum status; | ||
| 151 | ManagedTask::Statistics statistics; | ||
| 152 | |||
| 153 | ✗ | inline Info() | |
| 154 | ✗ | : status(Task::Status_Prepared) | |
| 155 | { | ||
| 156 | ✗ | } | |
| 157 | }; | ||
| 158 | |||
| 159 | TaskManager(); | ||
| 160 | ~TaskManager() noexcept; | ||
| 161 | |||
| 162 | void addTaskFactory(Ref<ITaskFactory> const& factory); | ||
| 163 | Property getTaskDefaultParameters(String const& taskName); | ||
| 164 | |||
| 165 | id_t addNewTask(String const& name, Property const& parameters, time::Milliseconds autoRepeat); | ||
| 166 | Array<id_t> getTasks(); | ||
| 167 | void removeTask(id_t taskId); | ||
| 168 | Info getTaskInfo(id_t taskId); | ||
| 169 | |||
| 170 | void startTask(id_t id); | ||
| 171 | |||
| 172 | private: | ||
| 173 | Mutex mutex; | ||
| 174 | Ref<ThreadPool> workers; | ||
| 175 | Map<id_t, ManagedTaskRef> tasks; | ||
| 176 | }; | ||
| 177 | |||
| 178 | } // end of namespace tech | ||
| 179 | } // end of namespace gate | ||
| 180 | |||
| 181 | #endif | ||
| 182 |