Line data Source code
1 : /*! 2 : * \file esys/repo/manifest/taskbase_mannifest.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/manifest/taskbase.h" 20 : #include "esys/repo/progress/fromgitprogress.h" 21 : 22 : namespace esys::repo::manifest 23 : { 24 : 25 124 : TaskBase::TaskBase() = default; 26 : 27 365 : TaskBase::~TaskBase() = default; 28 : 29 120 : void TaskBase::set_git(std::shared_ptr<GitBase> git) 30 : { 31 120 : m_git = git; 32 : 33 120 : if (git != nullptr) git->set_progress_callback(&m_progress_callback); 34 120 : } 35 : 36 360 : std::shared_ptr<GitBase> TaskBase::get_git() const 37 : { 38 360 : return m_git; 39 : } 40 : 41 120 : void TaskBase::set_config_folder(std::shared_ptr<ConfigFolder> config_folder) 42 : { 43 120 : m_config_folder = config_folder; 44 120 : } 45 : 46 253 : std::shared_ptr<ConfigFolder> TaskBase::get_config_folder() 47 : { 48 253 : return m_config_folder; 49 : } 50 : 51 121 : void TaskBase::set_repo(std::shared_ptr<Repository> repo) 52 : { 53 121 : m_repo = repo; 54 121 : } 55 : 56 895 : std::shared_ptr<Repository> TaskBase::get_repo() 57 : { 58 895 : return m_repo; 59 : } 60 : 61 3 : void TaskBase::set_result(int result) 62 : { 63 3 : m_result = result; 64 3 : } 65 : 66 121 : int TaskBase::get_result() const 67 : { 68 121 : return m_result; 69 : } 70 : 71 0 : void TaskBase::clear_errors() 72 : { 73 0 : m_errors.clear(); 74 0 : } 75 : 76 1 : void TaskBase::add_errors(const std::string &error) 77 : { 78 1 : m_errors.push_back(error); 79 1 : } 80 : 81 3 : std::vector<std::string> &TaskBase::get_errors() 82 : { 83 3 : return m_errors; 84 : } 85 : 86 0 : const std::vector<std::string> &TaskBase::get_errors() const 87 : { 88 0 : return m_errors; 89 : } 90 : 91 122 : void TaskBase::set_id(std::size_t id) 92 : { 93 122 : m_id = id; 94 122 : } 95 : 96 668 : std::size_t TaskBase::get_id() const 97 : { 98 668 : return m_id; 99 : } 100 : 101 122 : void TaskBase::set_worker_id(int worker_id) 102 : { 103 122 : m_worker_id = worker_id; 104 122 : } 105 : 106 0 : int TaskBase::get_worker_id() const 107 : { 108 0 : return m_worker_id; 109 : } 110 : 111 244 : void TaskBase::set_progress_session(progress::ProgressSession *progress_session) 112 : { 113 244 : m_progress_session = progress_session; 114 244 : } 115 : 116 0 : progress::ProgressSession *TaskBase::get_progress_session() const 117 : { 118 0 : return m_progress_session; 119 : } 120 : 121 1154 : void TaskBase::git_progress_notif(const git::Progress &progress) 122 : { 123 1154 : progress::ProgressSession *session = nullptr; 124 1154 : std::size_t repo_index = 0; 125 1154 : bool emit_to_session = false; 126 1154 : { 127 1154 : std::lock_guard lock(m_progress_mutex); 128 : 129 1154 : if (progress.get_started()) m_running = true; 130 1154 : if (progress.get_done()) m_running = false; 131 : 132 : // Always keep the latest sample for get_progress() pollers. 133 1154 : m_progress = progress; 134 1154 : session = m_progress_session; 135 1154 : repo_index = m_id; 136 : 137 1154 : constexpr auto k_min_emit_interval = std::chrono::milliseconds(50); 138 1154 : const auto now = std::chrono::steady_clock::now(); 139 1154 : const bool step_changed = progress.get_fetch_step() != m_last_emitted_step; 140 1154 : const bool timed_out = (m_last_progress_emit.time_since_epoch().count() == 0) 141 1154 : || (now - m_last_progress_emit >= k_min_emit_interval); 142 : 143 1154 : emit_to_session = session != nullptr 144 1154 : && (progress.get_started() || progress.get_done() || step_changed || timed_out); 145 : 146 1154 : if (emit_to_session) 147 : { 148 1154 : m_last_progress_emit = now; 149 1154 : m_last_emitted_step = progress.get_fetch_step(); 150 1154 : m_last_emitted_percentage = progress.get_percentage(); 151 : } 152 0 : } 153 : 154 2308 : if (emit_to_session) session->push(progress::repo_progress_event_from_git(repo_index, progress)); 155 1154 : } 156 : 157 0 : void TaskBase::get_progress(git::Progress &progress) 158 : { 159 0 : std::lock_guard lock(m_progress_mutex); 160 : 161 0 : progress = m_progress; 162 0 : } 163 : 164 0 : void TaskBase::set_running(bool running) 165 : { 166 0 : m_running = running; 167 0 : } 168 : 169 0 : bool TaskBase::get_running() 170 : { 171 0 : std::lock_guard lock(m_progress_mutex); 172 : 173 0 : return m_running; 174 0 : } 175 : 176 : } // namespace esys::repo::manifest