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

Go to the SVN repository for this file.

1 /* $Id: remote_cgiapp.cpp 92112 2020-12-22 16:22:46Z grichenk $
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  * Author: Maxim Didenko
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
36 #define GRID_APP_NAME "remote_cgiapp"
37 
39 
40 #if defined(NCBI_OS_UNIX)
41 # include <corelib/ncbi_process.hpp>
42 # include <signal.h>
43 
44 /// @internal
45 extern "C"
47 {
48  try {
49  ncbi::CRemoteCgiApp* app =
50  dynamic_cast<ncbi::CRemoteCgiApp*>(ncbi::CNcbiApplication::Instance());
51  if (app) {
52  app->RequestShutdown();
53  }
54  }
55  catch (...) {} // Make sure we don't throw an exception through the "C" layer
56 }
57 #endif
58 
59 
61 
62 /////////////////////////////////////////////////////////////////////////////
63 //
64 
66 {
67 public:
69  virtual ~CCgiWorkerNodeJob() {}
70 
71  int Do(CWorkerNodeJobContext& job_context)
72  {
73  CNcbiIstream& is = job_context.GetIStream();
74  CNcbiOstream& os = job_context.GetOStream();
75 
76  int ret = m_App.RunJob(is, os, job_context);
77 
78  job_context.CommitJob();
79  return ret;
80  }
81 
82 private:
84 };
85 
87 {
88 public:
91  {
92  return new CCgiWorkerNodeJob(m_App);
93  }
94  virtual string GetJobVersion() const
95  {
96  return GRID_APP_VERSION_INFO;
97  }
98  virtual string GetAppName() const
99  {
100  return GRID_APP_NAME;
101  }
102  virtual string GetAppVersion() const
103  {
104  return GRID_APP_VERSION;
105  }
106 
107 private:
109 };
110 
111 
112 /////////////////////////////////////////////////////////////////////////////
113 //
115  : m_JobContext(NULL)
116 {
117  m_AppImpl.reset(new CGridWorkerNode(*this,
118  new CCgiWorkerNodeJobFactory(*this)));
119 
120 #if defined(NCBI_OS_UNIX)
121  // attempt to get server gracefully shutdown on signal
122  signal(SIGINT, CgiGridWorker_SignalHandler);
123  signal(SIGTERM, CgiGridWorker_SignalHandler);
124 #endif
125  // Enable parsing of std args
127 }
128 
130 {
132 
133  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
134  // Specify USAGE context
135  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
136  "Worker Node");
137  SetupArgDescriptions(arg_desc.release());
138 
139  IRWRegistry& reg = GetRWConfig();
140  reg.Set("netcache_client", "cache_output", "true");
141 
142  m_AppImpl->Init();
143 }
144 
146 {
147  arg_desc->AddOptionalKey("control_port",
148  "control_port",
149  "A TCP port number",
151 
153 }
154 
156 {
157  m_AppImpl->ForceSingleThread();
158  return m_AppImpl->Run();
159 }
160 
162 {
163  if (m_AppImpl.get())
164  m_AppImpl->RequestShutdown();
165 }
166 
168 {
169  return GRID_APP_VERSION_INFO;
170 }
171 
173  CWorkerNodeJobContext& job_context)
174 {
175  unique_ptr<CCgiContext> cgi_context(
176  new CCgiContext(*this, &is, &os,
178 
179  m_JobContext = &job_context;
180  CDiagRestorer diag_restorer;
181 
182  int ret;
183  try {
184  ConfigureDiagnostics(*cgi_context);
186  cgi_context->GetRequest(), cgi_context->GetResponse()) ?
187  0 : ProcessRequest(*cgi_context);
188  OnEvent(ret == 0 ? eSuccess : eError, ret);
189  cgi_context->GetResponse().Finalize();
190  OnEvent(eExit, ret);
191  } catch (exception& ex) {
192  ret = OnException(ex, os);
193  OnEvent(eException, ret);
194  }
195  OnEvent(eEndRequest, 120);
196  OnEvent(eExit, ret);
197  m_JobContext = NULL;
198  return ret;
199 }
200 
201 void CRemoteCgiApp::PutProgressMessage(const string& msg, bool send_immediately)
202 {
203  if (m_JobContext)
204  m_JobContext->PutProgressMessage(msg, send_immediately);
205 }
206 
207 
208 /////////////////////////////////////////////////////////////////////////////
209 
CArgDescriptions –.
Definition: ncbiargs.hpp:541
virtual string GetAppVersion() const
CCgiWorkerNodeJobFactory(CRemoteCgiApp &app)
virtual string GetJobVersion() const
Get the job version.
virtual IWorkerNodeJob * CreateInstance(void)
Create a job.
virtual string GetAppName() const
virtual ~CCgiWorkerNodeJob()
CRemoteCgiApp & m_App
int Do(CWorkerNodeJobContext &job_context)
Execute the job.
CCgiWorkerNodeJob(CRemoteCgiApp &app)
CDiagRestorer –.
Definition: ncbidiag.hpp:2941
Grid Worker Node.
Worker Node job context.
IRWRegistry –.
Definition: ncbireg.hpp:407
Worker Node Job Factory interface.
Worker Node Job interface.
#define GRID_APP_VERSION_INFO
#define GRID_APP_VERSION
void DisableArgDescriptions(TDisableArgDesc disable=fDisableStdArgs)
Definition: ncbiapp.cpp:1319
CNcbiRegistry & GetRWConfig(void)
Get the application's cached configuration parameters, accessible for read-write for an application's...
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
void AddOptionalKey(const string &name, const string &synopsis, const string &comment, EType type, TFlags flags=0)
Add description for optional key without default value.
Definition: ncbiargs.cpp:2427
@ eInteger
Convertible into an integer number (int or Int8)
Definition: ncbiargs.hpp:592
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: cgiapp.cpp:794
virtual void Init(void)
This method is called on the CGI application initialization – before starting to process a HTTP reque...
Definition: cgiapp.cpp:864
virtual int OnException(std::exception &e, CNcbiOstream &os)
This method is called if an exception is thrown during the processing of HTTP request.
Definition: cgiapp.cpp:1059
virtual void ConfigureDiagnostics(CCgiContext &context)
Definition: cgiapp.cpp:1275
int m_RequestFlags
Bit flags for CCgiRequest.
Definition: cgiapp.hpp:378
static bool ProcessCORSRequest(const CCgiRequest &request, CCgiResponse &response)
Process cross-origin resource sharing (CORS) request.
Definition: cgictx.cpp:839
virtual void OnEvent(EEvent event, int status)
This method is called after each request, or when the CGI is forced to skip a request,...
Definition: cgiapp.cpp:1252
virtual int ProcessRequest(CCgiContext &context)=0
This is the method you should override.
@ eException
An exception occured during the request processing.
Definition: cgiapp.hpp:193
@ eExit
No more iterations, exiting (called the very last)
Definition: cgiapp.hpp:195
@ eError
The HTTP request was processed, non-zero exit code.
Definition: cgiapp.hpp:191
@ eEndRequest
HTTP request processed, all results sent to client.
Definition: cgiapp.hpp:194
@ eSuccess
The HTTP request was processed, with zero exit code.
Definition: cgiapp.hpp:190
@ fSetDiagProperties
Old (deprecated) flag controlling diag properties.
Definition: ncbicgi.hpp:727
#define NULL
Definition: ncbistd.hpp:225
virtual void Init(void)
This method is called on the CGI application initialization – before starting to process a HTTP reque...
void CommitJob()
Confirm that a job is done and result is ready to be sent back to the client.
CNcbiOstream & GetOStream()
Get a stream where a job can write its result.
virtual string GetJobVersion() const
void RequestShutdown(void)
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
virtual int Run(void)
Do not override this method yourself! – it includes all the CGI specific machinery.
int RunJob(CNcbiIstream &is, CNcbiOstream &os, CWorkerNodeJobContext &)
unique_ptr< CGridWorkerNode > m_AppImpl
CNcbiIstream & GetIStream()
Get a stream with input data for a job.
CWorkerNodeJobContext * m_JobContext
void PutProgressMessage(const string &msg, bool send_immediately=false)
bool Set(const string &section, const string &name, const string &value, TFlags flags=0, const string &comment=kEmptyStr)
Set the configuration parameter value.
Definition: ncbireg.cpp:826
#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::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
Defines process management classes.
#define GRID_APP_NAME
void CgiGridWorker_SignalHandler(int)
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
virtual void PutProgressMessage(const string &msg, bool send_immediately, bool overwrite)
Modified on Fri Sep 20 14:57:31 2024 by modify_doxy.py rev. 669887