NCBI C++ ToolKit
job_future.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: job_future.cpp 43932 2019-09-20 19:58:40Z katargir $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Roman Katargin
27 *
28 * File Description:
29 *
30 */
31 
32 #include <ncbi_pch.hpp>
33 
34 #include <gui/utils/job_future.hpp>
35 #include <corelib/ncbi_system.hpp>
36 
38 
39 ///
40 /// CJobFutureAdapter
41 ///
42 
46 
48  : m_Callback(&callback), m_JobId(CAppJobDispatcher::eInvalidJobID)
49 {
50 
51 }
52 
54 {
55  m_JobId = CAppJobDispatcher::GetInstance().StartJob(job, "ObjManagerEngine", *this, -1, true);
56 }
57 
59 {
60  m_Callback = 0;
61 
63  return;
64 
67 }
68 
70 {
72  return;
73 
74  try {
76  }
77  catch (const std::exception&) {}
78 }
79 
81 {
83  return false;
84 
85  return (IAppJob::eRunning == CAppJobDispatcher::GetInstance().GetJobState(m_JobId));
86 }
87 
89 {
90  CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt);
91  _ASSERT(notn);
92  if (!notn) return;
93 
94  switch (notn->GetState()) {
97 
98  if (m_Callback) {
99  CRef<CObject> result = notn->GetResult();
101  return;
102  }
103  break;
104  case IAppJob::eFailed:
106 
107  if (m_Callback) {
110  return;
111  }
112  break;
113  case IAppJob::eCanceled:
115 
116  if (m_Callback) {
118  }
119  break;
120  default:
121  break;
122  }
123 }
124 
125 ///
126 /// job_future_base
127 ///
128 
129 const char* job_future_base::m_BadJobResponse = "Improper response from job";
130 
132  : m_State(eRunning), m_JobAdapter(new CJobFutureAdapter(*this))
133 {
134  m_JobAdapter->Start(job);
135 }
136 
138 {
139  if (m_JobAdapter) {
140  m_JobAdapter->Detach();
142  }
143  m_Result.Reset();
144  m_Error.Reset();
146  m_Canceled = false;
147 }
148 
150 {
151  if (m_State != eComplete)
152  throw std::bad_function_call();
153 
155 
156  if (m_Error)
157  m_Error->Rethrow();
158 }
159 
161 {
163  m_State = eComplete;
164 }
165 
167 {
168  const CJobFutureError *jobError = dynamic_cast<const CJobFutureError*>(error);
169  if (jobError) {
170  m_Error.Reset(jobError);
171  }
172  else {
173  std::exception_ptr exc =
174  std::make_exception_ptr(std::runtime_error(m_BadJobResponse));
175  m_Error.Reset(new CJobFutureError(exc));
176  }
177  m_State = eComplete;
178 }
179 
181 {
183 }
184 
186 {
187  handle_error();
188 
190  = dynamic_cast<CJobFutureResult*>(m_Result.GetPointerOrNull());
191  if (result) {
192  m_Canceled = result->GetCanceled();
193  return;
194  }
195  throw std::runtime_error(m_BadJobResponse);
196 }
197 
199 {
200  static CJobHandler _instance;
201  return _instance;
202 }
203 
205 {
206  for (auto it = m_Jobs.begin(); it != m_Jobs.end();) {
207  if ((*it)->canceled())
208  it = m_Jobs.erase(it);
209  else
210  ++it;
211  }
212 
213  for (auto it = m_Jobs.begin(); it != m_Jobs.end(); ++it) {
214  if ((*it)->complete()) {
215  m_Jobs.erase(it);
216  return true;
217  }
218  }
219  return false;
220 }
221 
223 {
225 }
226 
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
virtual void OnJobResult(CObject *result)=0
virtual void OnJobFailed(const IAppJobError *error)=0
Here is set of classes to implement launching jobs (to execute code on a worker thread using C++ Tool...
Definition: job_future.hpp:103
IJobCallback * m_Callback
Definition: job_future.hpp:131
void x_OnJobNotification(CEvent *evt)
Definition: job_future.cpp:88
bool IsRunning() const
Definition: job_future.cpp:80
void Start(IAppJob &job)
Definition: job_future.cpp:53
CAppJobDispatcher::TJobID m_JobId
Definition: job_future.hpp:132
void Rethrow() const
Definition: job_future.hpp:142
list< async_job * > m_Jobs
Definition: job_future.hpp:463
bool Process()
Definition: job_future.cpp:204
static CJobHandler & Instance()
Definition: job_future.cpp:198
void x_RemoveJob(async_job *job)
Definition: job_future.hpp:460
CObject –.
Definition: ncbiobj.hpp:180
IAppJobError.
Definition: app_job.hpp:65
IAppJob.
Definition: app_job.hpp:82
virtual ~async_job()
Definition: job_future.cpp:222
virtual void OnJobFailed(const IAppJobError *error)
Definition: job_future.cpp:166
CRef< CObject > m_Result
Definition: job_future.hpp:236
CRef< CJobFutureAdapter > m_JobAdapter
Definition: job_future.hpp:235
CConstRef< CJobFutureError > m_Error
Definition: job_future.hpp:237
virtual void OnJobResult(CObject *result)
Definition: job_future.cpp:160
virtual void OnJobCanceled()
Definition: job_future.cpp:180
static const char * m_BadJobResponse
job_future_base
Definition: job_future.hpp:239
_Rty & operator()()
Definition: job_future.hpp:282
CConstIRef< IAppJobError > GetError() const
returns non-null pointer only if job Failed
static CAppJobDispatcher & GetInstance()
CRef< CObject > GetResult() const
returns non-null pointer only if Completed or Running and has temporary results available
bool DeleteJob(TJobID job_id)
when a Job is deleted the listener is not notified
TJobID StartJob(IAppJob &job, const string &engine_name, IEngineParams *params=NULL)
Starts a Job on the specified engine in "passive mode" - no notifications or progress reports will be...
#define ON_EVENT(type, id, handler)
#define END_EVENT_MAP()
Ends definition of Command Map.
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
TJobState GetState() const
void CancelJob(TJobID job_id)
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eRunning
Definition: app_job.hpp:88
@ eFailed
Definition: app_job.hpp:90
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:1439
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:986
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define _ASSERT
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:58:02 2024 by modify_doxy.py rev. 669887