KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_task_scheduler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/log.h>
23
24#include <api/api_utils.h>
25#include <api/common/tasks.pb.h>
26#include <kiid.h>
27
28#include <exception>
29#include <optional>
30
31
32wxDEFINE_EVENT( API_TASK_RUN_EVENT, wxCommandEvent );
33
34
35static wxCommandEvent* makeRunEvent( const std::string& aToken )
36{
37 wxCommandEvent* evt = new wxCommandEvent( API_TASK_RUN_EVENT );
38 evt->SetClientData( new std::string( aToken ) );
39 return evt;
40}
41
42
44{
45 static API_TASK_SCHEDULER s_instance;
46 return s_instance;
47}
48
49
51{
52 Bind( API_TASK_RUN_EVENT, &API_TASK_SCHEDULER::onTaskRunEvent, this );
53}
54
55
57{
58 Unbind( API_TASK_RUN_EVENT, &API_TASK_SCHEDULER::onTaskRunEvent, this );
59}
60
61
62std::optional<std::string> API_TASK_SCHEDULER::StartTask( const std::shared_ptr<API_TASK>& aTask,
63 const wxString& aDescription )
64{
65 std::string token = KIID().AsStdString();
66 auto entry = std::make_shared<TASK_ENTRY>();
67 entry->token = token;
68 entry->description = aDescription;
69 entry->task = aTask;
70
71 {
72 std::lock_guard<std::mutex> lock( m_mutex );
73
74 for( const auto& [existingToken, existingEntry] : m_tasks )
75 {
76 if( !existingEntry->finished )
77 return std::nullopt;
78 }
79
80 m_tasks[ token ] = entry;
81 }
82
83 wxLogTrace( traceApi, wxString::Format( wxS( "TaskScheduler: started task %s (%s)" ), token, aDescription ) );
84
85 QueueEvent( makeRunEvent( token ) );
86
87 return token;
88}
89
90
91std::optional<kiapi::common::tasks::TaskStatusResponse> API_TASK_SCHEDULER::GetStatus( const std::string& aToken ) const
92{
93 std::shared_ptr<TASK_ENTRY> entry;
94 bool finished = false;
95 bool failed = false;
96 std::string failureMessage;
97
98 {
99 std::lock_guard<std::mutex> lock( m_mutex );
100
101 auto it = m_tasks.find( aToken );
102
103 if( it == m_tasks.end() )
104 return std::nullopt;
105
106 entry = it->second;
107 finished = entry->finished;
108 failed = entry->failed;
109 failureMessage = entry->failureMessage;
110 }
111
112 kiapi::common::tasks::TaskStatusResponse status;
113
114 if( !finished )
115 {
116 status.set_state( kiapi::common::tasks::TS_RUNNING );
117 }
118 else if( failed )
119 {
120 status.set_state( kiapi::common::tasks::TS_FAILED );
121 status.set_error_message( failureMessage );
122 }
123 else
124 {
125 entry->task->FillStatus( status );
126
127 // In case the task itself didn't update the state, we assume it's completed
128 if( status.state() == kiapi::common::tasks::TS_UNKNOWN )
129 status.set_state( kiapi::common::tasks::TS_COMPLETED );
130 }
131
132 return status;
133}
134
135
136std::shared_ptr<API_TASK> API_TASK_SCHEDULER::GetFinishedTask( const std::string& aToken ) const
137{
138 std::lock_guard<std::mutex> lock( m_mutex );
139
140 auto it = m_tasks.find( aToken );
141
142 if( it == m_tasks.end() || !it->second->finished )
143 return nullptr;
144
145 return it->second->task;
146}
147
148
150{
151 std::lock_guard<std::mutex> lock( m_mutex );
152
153 for( const auto& [token, entry] : m_tasks )
154 {
155 if( !entry->finished )
156 return true;
157 }
158
159 return false;
160}
161
162
163void API_TASK_SCHEDULER::onTaskRunEvent( wxCommandEvent& aEvent )
164{
165 std::unique_ptr<std::string> token( static_cast<std::string*>( aEvent.GetClientData() ) );
166
167 if( token )
168 runTask( *token );
169}
170
171
172void API_TASK_SCHEDULER::runTask( const std::string& aToken )
173{
174 std::shared_ptr<TASK_ENTRY> entry;
175
176 {
177 std::lock_guard<std::mutex> lock( m_mutex );
178
179 auto it = m_tasks.find( aToken );
180
181 if( it == m_tasks.end() || it->second->finished )
182 return;
183
184 entry = it->second;
185 }
186
187 bool failed = false;
188 std::string failureMessage;
189
190 try
191 {
192 entry->task->Run();
193 }
194 catch( const std::exception& aError )
195 {
196 failed = true;
197 failureMessage = aError.what();
198
199 wxLogTrace( traceApi, wxString::Format( wxS( "TaskScheduler: task %s failed: %s" ),
200 wxString::FromUTF8( entry->token ),
201 wxString::FromUTF8( aError.what() ) ) );
202 }
203 catch( ... )
204 {
205 failed = true;
206 failureMessage = "task failed with an unknown error";
207
208 wxLogTrace( traceApi, wxString::Format( wxS( "TaskScheduler: task %s failed" ),
209 wxString::FromUTF8( entry->token ) ) );
210 }
211
212 {
213 std::lock_guard<std::mutex> lock( m_mutex );
214
215 if( entry->finished )
216 return;
217
218 entry->failed = failed;
219 entry->failureMessage = failureMessage;
220 entry->finished = true;
221
222 m_finishedTokens.push_back( entry->token );
223
224 while( m_finishedTokens.size() > MAX_FINISHED_TASKS )
225 {
226 const std::string& oldest = m_finishedTokens.front();
227 auto oldestIt = m_tasks.find( oldest );
228
229 if( oldestIt != m_tasks.end() && oldestIt->second->finished )
230 m_tasks.erase( oldestIt );
231
232 m_finishedTokens.pop_front();
233 }
234 }
235}
236
237
239{
240 std::vector<std::string> pending;
241
242 {
243 std::lock_guard<std::mutex> lock( m_mutex );
244
245 for( const auto& [token, entry] : m_tasks )
246 {
247 if( !entry->finished )
248 pending.push_back( token );
249 }
250 }
251
252 for( const std::string& token : pending )
253 runTask( token );
254}
static wxCommandEvent * makeRunEvent(const std::string &aToken)
wxDEFINE_EVENT(API_TASK_RUN_EVENT, wxCommandEvent)
std::shared_ptr< API_TASK > GetFinishedTask(const std::string &aToken) const
Look up a task that has finished, for result retrieval.
std::optional< kiapi::common::tasks::TaskStatusResponse > GetStatus(const std::string &aToken) const
Get a snapshot of the task's current status.
std::deque< std::string > m_finishedTokens
void onTaskRunEvent(wxCommandEvent &aEvent)
void Pump()
Run the current task to completion, synchronously.
void runTask(const std::string &aToken)
static API_TASK_SCHEDULER & Instance()
API_TASK_SCHEDULER(const API_TASK_SCHEDULER &)=delete
static constexpr size_t MAX_FINISHED_TASKS
std::map< std::string, std::shared_ptr< TASK_ENTRY > > m_tasks
std::optional< std::string > StartTask(const std::shared_ptr< API_TASK > &aTask, const wxString &aDescription)
Register a new task and begin running it.
Definition kiid.h:46
std::string AsStdString() const
Definition kiid.cpp:270
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:33