NCBI C++ ToolKit
grid_cgiapp.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef __GRID_CGIAPP_HPP
2 #define __GRID_CGIAPP_HPP
3 
4 /* $Id: grid_cgiapp.hpp 94522 2021-08-12 13:43:04Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Maxim Didenko
30  *
31  * File Description:
32  * Base class for Grid CGI Application
33  *
34  */
35 
36 #include <corelib/ncbimisc.hpp>
37 
38 #include <cgi/cgiapp_cached.hpp>
39 #include <cgi/cgictx.hpp>
40 #include <html/html.hpp>
41 #include <html/page.hpp>
42 
43 
46 
47 
48 #include <map>
49 
50 /// @file grid_cgiapp.hpp
51 /// NetSchedule Framework specs.
52 ///
53 
54 
56 
57 /** @addtogroup NetScheduleClient
58  *
59  * @{
60  */
61 
63 /////////////////////////////////////////////////////////////////////////////
64 /// Grid CGI Context
65 /// Context in which a request is processed
66 ///
68 {
69 public:
71  ~CGridCgiContext();
72 
73  /// Get an HTML page
74  ///
75  CHTMLPage& GetHTMLPage(void) { return m_Page; }
76 
77  /// Get Self URL
78  ///
79  string GetSelfURL(void) const;
80 
81  /// Get Current job key
82  ///
83  const string& GetJobKey(void) const;
84 
85  /// Get current job progress message
86  const string& GetJobProgressMessage(void) const
87  { return m_ProgressMsg; }
88 
89  /// Get a value from a CGI request. if there is no an entry with a
90  /// given name it returns an empty string.
91  const string& GetEntryValue(const string& entry_name) const;
92 
93  /// Save this entry as a cookie add it to serf url
94  void PullUpPersistentEntry(const string& entry_name);
95  void DefinePersistentEntry(const string& entry_name, const string& value);
96 
97  /// Get CGI Context
98  CCgiContext& GetCGIContext() { return m_CgiContext; }
99 
100  ///
101  void SetCompleteResponse(CNcbiIstream& is);
102  bool NeedRenderPage() const { return m_NeedRenderPage; }
103 
104  string GetHiddenFields() const;
105 
106  const string& GetJobInput() const { return m_JobInput; }
107  const string& GetJobOutput() const { return m_JobOutput; }
108 
109 private:
110 
111  /// Remove all persisted entries from cookie and self url.
112  void Clear();
113 
114 private:
116 
117  friend class CGridCgiApplication;
118  void SetJobKey(const string& job_key);
119  void SetJobProgressMessage(const string& msg)
120  { m_ProgressMsg = msg; }
121  void SetJobInput(const string& input) { m_JobInput = input; }
122  void SetJobOutput(const string& output) { m_JobOutput = output; }
123 
124 
130  string m_JobInput;
131  string m_JobOutput;
133 
134  /// A copy constructor and an assignment operator
135  /// are prohibited
136  ///
139 };
140 
141 
142 /////////////////////////////////////////////////////////////////////////////
143 ///
144 /// Grid Cgi Application
145 ///
146 /// Base class for CGI applications starting background jobs using
147 /// NetSchedule. Implements pattern for job submission, status check,
148 /// error processing, etc. All request processing is done on the back end.
149 /// CGI application is responsible for UI rendering.
150 ///
152 {
153 public:
154 
155  /// This method is called on the CGI application initialization -- before
156  /// starting to process a HTTP request or even receiving one.
157  /// If you decide to override it, remember to call
158  /// CGridCgiApplication::Init().
159  ///
160  virtual void Init(void);
161 
162  /// Do not override this method yourself! -- it includes all the GRIDCGI
163  /// specific machinery. If you override it, do call
164  /// CGridCgiApplication::ProcessRequest() from inside your overriding method.
165  ///
166  virtual int ProcessRequest(CCgiContext& ctx);
167 
168  /// Get program version (like: MyProgram v. 1.2.3)
169  ///
170  /// Program version is passed to NetSchedule queue so queue
171  /// controls versions and does not allow obsolete clients
172  /// to connect and submit or execute jobs
173  ///
174  virtual string GetProgramVersion(void) const = 0;
175 
176 protected:
177 
178  /// Show a page with input data
179  ///
180  virtual void ShowParamsPage(CGridCgiContext& ctx) const = 0;
181 
182  /// Collect parameters from HTML form
183  /// If this method returns false that means that input parameters were
184  /// not specified (or were incorrect). In this case method ShowParamsPage
185  /// will be called.
186  ///
187  virtual bool CollectParams(CGridCgiContext& ctx) = 0;
188 
189  /// This method is called when a job is ready to be send to a the queue.
190  /// Override this method to prepare input data for the worker node.
191  ///
192  virtual void PrepareJobData(CGridClient& grid_client) = 0;
193 
194  /// This method is called just after a job has been submitted.
195  /// Override this method to render information HTML page.
196  ///
197  virtual void OnJobSubmitted(CGridCgiContext& /*ctx*/) {}
198 
199  /// This method is call when a worker node finishes its job and
200  /// result is ready to be retrieved.
201  /// Override this method to get a result from a worker node
202  /// and render a result HTML page
203  ///
204  virtual void OnJobDone(CGridClient& grid_client, CGridCgiContext& ctx) = 0;
205 
206  /// This method is called when worker node reported a failure.
207  /// Override this method to get a error message and render
208  /// a error HTML page.
209  ///
210  virtual void OnJobFailed(const string& /*msg*/, CGridCgiContext& /*ctx*/) {}
211 
212  /// This method is called if job was canceled during its execution.
213  /// Override this message to show a job cancellation message.
214  ///
215  virtual void OnJobCanceled(CGridCgiContext& /*ctx*/) {}
216 
217  /// This method is call when a job is taken by a worker node
218  /// to be processed.
219  ///
220  virtual void OnJobRunning(CGridCgiContext& /*ctx*/) {}
221 
222  /// This method is call when a job is in NetSchedule queue and
223  /// is waiting for a worker node.
224  ///
225  virtual void OnJobPending(CGridCgiContext& /*ctx*/) {}
226 
227  /// This method is call at the very end of the request processing
228  ///
230 
231  /// This method is call at the very beginning of the request processing
232  ///
234 
235  /// This method is call when a job couldn't be submitted
236  /// because of NetSchedule queue is full
237  ///
238  virtual void OnQueueIsBusy(CGridCgiContext&);
239 
240  /// Return page name. It is used when an instance of CHTMLPage
241  /// class is created.
242  ///
243  virtual string GetPageTitle(void) const = 0;
244 
245  /// Return a name of a file this HTML page template. It is used when
246  /// an instance of CHTMLPage class is created.
247  ///
248  virtual string GetPageTemplate(void) const = 0;
249 
250  /// When job is still running this method is called to check if
251  /// cancel has been requested via the user interface(HTML).
252  /// If so the job will be canceled.
253  ///
254  virtual bool JobStopRequested(void) const { return false; }
255 
256  /// Get a Grid Client.
257  ///
258  CGridClient& GetGridClient(void) { return *m_GridClient; }
259 
260  /// Initialize grid client
261  ///
262  void InitGridClient();
263 
264  void RenderRefresh(CHTMLPage& page,
265  const string& url,
266  int delay);
267 
268  virtual bool IsCachingNeeded(const CCgiRequest& request) const;
269 
270 private:
273  bool x_JobStopRequested(const CGridCgiContext&) const;
274  bool x_CheckJobStatus(CGridCgiContext& grid_ctx);
275 
278  unique_ptr<CGridClient> m_GridClient;
280 
281 };
282 
283 /////////////////////////////////////////////////////////////////////////////
284 
286 
287 #endif //__GRID_CGIAPP_HPP
CCgiRequest::
Definition: ncbicgi.hpp:685
Grid Cgi Application.
Grid CGI Context Context in which a request is processed.
Definition: grid_cgiapp.hpp:68
Grid Client (the submitter).
CHTMLPage –.
Definition: page.hpp:161
Client API for NetCache server.
Client API for NCBI NetSchedule server.
CS_CONTEXT * ctx
Definition: t0006.c:12
static void Init(void)
Definition: cursor6.c:76
static SQLCHAR output[256]
Definition: print.c:5
NetSchedule Framework specs.
CCgiContext & m_CgiContext
CGridCgiContext(const CGridCgiContext &)
A copy constructor and an assignment operator are prohibited.
TPersistedEntries m_PersistedEntries
TCgiEntries m_ParsedQueryString
virtual void OnJobCanceled(CGridCgiContext &)
This method is called if job was canceled during its execution.
void SetJobProgressMessage(const string &msg)
virtual void PrepareJobData(CGridClient &grid_client)=0
This method is called when a job is ready to be send to a the queue.
CHTMLPage & GetHTMLPage(void)
Get an HTML page.
Definition: grid_cgiapp.hpp:75
void SetJobInput(const string &input)
virtual void OnBeginProcessRequest(CGridCgiContext &)
This method is call at the very beginning of the request processing.
virtual bool JobStopRequested(void) const
When job is still running this method is called to check if cancel has been requested via the user in...
virtual string GetPageTitle(void) const =0
Return page name.
const string & GetJobInput() const
CCgiContext & GetCGIContext()
Get CGI Context.
Definition: grid_cgiapp.hpp:98
virtual void OnJobPending(CGridCgiContext &)
This method is call when a job is in NetSchedule queue and is waiting for a worker node.
virtual void OnJobDone(CGridClient &grid_client, CGridCgiContext &ctx)=0
This method is call when a worker node finishes its job and result is ready to be retrieved.
unique_ptr< CGridClient > m_GridClient
virtual void ShowParamsPage(CGridCgiContext &ctx) const =0
Show a page with input data.
CHTMLPage & m_Page
CCgiResponse * m_Response
virtual void OnJobRunning(CGridCgiContext &)
This method is call when a job is taken by a worker node to be processed.
bool NeedRenderPage() const
const string & GetJobOutput() const
virtual bool CollectParams(CGridCgiContext &ctx)=0
Collect parameters from HTML form If this method returns false that means that input parameters were ...
CGridClient & GetGridClient(void)
Get a Grid Client.
const string & GetJobProgressMessage(void) const
Get current job progress message.
Definition: grid_cgiapp.hpp:86
virtual void OnJobSubmitted(CGridCgiContext &)
This method is called just after a job has been submitted.
virtual void OnJobFailed(const string &, CGridCgiContext &)
This method is called when worker node reported a failure.
virtual string GetProgramVersion(void) const =0
Get program version (like: MyProgram v.
CGridCgiContext & operator=(const CGridCgiContext &)
virtual void OnEndProcessRequest(CGridCgiContext &)
This method is call at the very end of the request processing.
void SetJobOutput(const string &output)
CNetScheduleAPI m_NSClient
virtual string GetPageTemplate(void) const =0
Return a name of a file this HTML page template.
map< string, string > TPersistedEntries
CNetCacheAPI m_NetCacheAPI
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
#define NCBI_XGRIDCGI_EXPORT
Definition: ncbi_export.h:1131
HTML classes.
static int input()
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Miscellaneous common-use basic types and functionality.
NetSchedule client specs.
The HTML page.
Modified on Wed Jun 19 17:05:48 2024 by modify_doxy.py rev. 669887