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

Go to the SVN repository for this file.

1 /* $Id: grid_worker_app.cpp 71027 2016-02-05 16:08:02Z sadyrovr $
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: Maxim Didenko, Anatoliy Kuznetsov, Dmitry Kazimirov
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
34 #include "grid_worker_impl.hpp"
35 
36 #include <corelib/ncbimtx.hpp>
38 
39 #if defined(NCBI_OS_UNIX)
40 # include <signal.h>
41 
42 /// @internal
43 extern "C"
44 void g_GridWorker_TermHandler(int /*sig*/)
45 {
46  try {
47  ncbi::CMutexGuard guard(ncbi::CNcbiApplication::GetInstanceMutex());
48  ncbi::CGridWorkerApp* app = dynamic_cast<ncbi::CGridWorkerApp*>(
49  ncbi::CNcbiApplication::Instance());
50  if (app) {
51  app->RequestShutdown();
52  }
53  }
54  catch (...) { // Make sure we don't throw an exception through the "C" layer
55  }
56 }
57 #endif
58 
60 
63 {
64  const CGridWorkerApp* grid_app =
65  dynamic_cast<const CGridWorkerApp*>(&m_App);
66 
67  _ASSERT(grid_app != NULL);
68  return grid_app->GetWorkerNode().GetCleanupEventSource();
69 }
70 
72 {
73  const CGridWorkerApp* grid_app =
74  dynamic_cast<const CGridWorkerApp*>(&m_App);
75 
76  return grid_app->GetWorkerNode().GetNetScheduleAPI();
77 }
78 
80 {
81  const CGridWorkerApp* grid_app =
82  dynamic_cast<const CGridWorkerApp*>(&m_App);
83 
84  return grid_app->GetWorkerNode().GetNetCacheAPI();
85 }
86 
87 /////////////////////////////////////////////////////////////////////////////
88 //
89 
91 {
92  m_WorkerNode = CGridWorkerNode(*this, job_factory);
93 
94 #if defined(NCBI_OS_UNIX)
95  // attempt to get server gracefully shutdown on signal
96  signal(SIGINT, g_GridWorker_TermHandler);
97  signal(SIGTERM, g_GridWorker_TermHandler);
98  signal(SIGUSR1, g_GridWorker_TermHandler);
99  signal(SIGUSR2, g_GridWorker_TermHandler);
100 #endif
101 }
102 
104  const SBuildInfo& build_info) :
105  CNcbiApplication(build_info)
106 {
107  Construct(job_factory);
108 }
109 
111  const CVersionInfo& version_info,
112  const SBuildInfo& build_info)
113 {
114  Construct(job_factory);
115 
116  SetVersion(version_info, build_info);
117 }
118 
120 {
121  // SetupDiag(eDS_ToStdout);
123 
125 
126  auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
127  // Specify USAGE context
128  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
129  "Worker Node");
130  // Setup arg.descriptions for this application
131  SetupArgDescriptions(arg_desc.release());
132 
133  m_WorkerNode.Init();
135 }
136 
138 {
139  arg_desc->AddOptionalKey("control_port", "control_port",
140  "A TCP port number for the worker node to listen on.",
142 
143 #ifdef NCBI_OS_UNIX
144  arg_desc->AddFlag("daemon", "Daemonize.");
145  arg_desc->AddFlag("nodaemon", "Do not daemonize.");
146 #endif
147 
148  arg_desc->AddOptionalKey("logfile", "file_name",
149  "File to which the program log should be redirected.",
151 
152  arg_desc->AddOptionalKey("procinfofile", "file_name",
153  "File to save the process ID and the control port number to.",
155 
156  arg_desc->AddOptionalKey("offline-input-dir", "in_dir_path",
157  "Directory populated with job inputs - one file per job.",
159 
160  arg_desc->AddOptionalKey("offline-output-dir", "out_dir_path",
161  "Directory to store job outputs. Requires '-offline-input-dir'",
163 
165 }
166 
168 {
169  if (!m_WorkerNodeInitContext.get())
171  new CDefaultWorkerNodeInitContext(*this));
172  return *m_WorkerNodeInitContext;
173 }
174 
176 {
177  const CArgs& args = GetArgs();
178 
179  return args["offline-input-dir"] ? m_WorkerNode->OfflineRun() :
181 #ifdef NCBI_OS_UNIX
182  args["nodaemon"] ? eOff :
183  args["daemon"] ? eOn : eDefault,
184 #endif
185  args["procinfofile"].HasValue() ?
186  args["procinfofile"].AsString() : kEmptyStr);
187 }
188 
190 {
191  if (m_WorkerNode)
193 }
194 
195 
Worker Node Job Factory interface.
void SetVersion(const CVersionInfo &version)
Set the version number for the program.
Definition: ncbiapp.cpp:886
CNetCacheAPI GetNetCacheAPI() const
virtual int Run(void)
Do not override this method yourself! It includes all the Worker Node specific machinery.
auto_ptr< IWorkerNodeJobFactory > m_JobProcessorFactory
virtual CNetCacheAPI GetNetCacheAPI() const
Get the shared NetCacheAPI object used by the worker node framework.
void g_GridWorker_TermHandler(int)
virtual IWorkerNodeCleanupEventSource * GetCleanupEventSource() const
Get interface for registering clean-up event listeners.
const IWorkerNodeInitContext & GetInitContext()
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:926
Clean-up event source for the worker node.
Definition: grid_worker.hpp:97
void RequestShutdown()
#define NULL
Definition: ncbistd.hpp:225
NetSchedule worker node application.
#define kEmptyStr
Definition: ncbistr.hpp:120
auto_ptr< IWorkerNodeInitContext > m_WorkerNodeInitContext
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
Convertible into an integer number (int or Int8)
Definition: ncbiargs.hpp:566
CGridWorkerNode GetWorkerNode() const
CGridWorkerApp(IWorkerNodeJobFactory *job_factory, const SBuildInfo &build_info=SBuildInfo())
static void SetDeleteReadOnlyFiles(ESwitch on_off_default)
Specify whether read-only files can be deleted via CDirEntry::Remove() on Windows.
Definition: ncbifile.cpp:7230
Worker Node initialize context.
void AddFlag(const string &name, const string &comment, CBoolEnum< EFlagValue > set_value=eFlagHasValueIfSet)
Add description for flag argument.
Definition: ncbiargs.cpp:2273
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:190
CGridWorkerNode m_WorkerNode
CArgDescriptions –.
Definition: ncbiargs.hpp:514
virtual void Init(void)
Initialize the application.
Definition: ncbiapp.cpp:171
Grid Worker Node.
Default implementation of a worker node initialization interface.
CVersionInfo –.
Definition: version.hpp:59
An arbitrary string.
Definition: ncbiargs.hpp:563
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
Definition: ncbiapp.hpp:681
int Run(ESwitch daemonize=eDefault, string procinfo_file_name=string())
Start job execution loop.
CArgs –.
Definition: ncbiargs.hpp:356
Name of file (must be writable)
Definition: ncbiargs.hpp:569
virtual void Init(void)
If you override this method, do call CGridWorkerApp::Init() from inside of your overriding method...
Multi-threading – mutexes; rw-locks; semaphore.
Client API for NCBI NetSchedule server.
CNetScheduleAPI GetNetScheduleAPI() const
#define NCBI_OS_UNIX
Main Worker Node application.
Client API for NetCache server.
#define _ASSERT
IWorkerNodeCleanupEventSource * GetCleanupEventSource()
This class allows to add build info (date and tag) to application version.
Definition: version.hpp:234
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:2241
void Construct(IWorkerNodeJobFactory *job_factory)
virtual CNetScheduleAPI GetNetScheduleAPI() const
Get the shared NetScheduleAPI object used by the worker node framework.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
TMutexGuard CMutexGuard
...and backward compatibility
Definition: ncbimtx.hpp:470
Modified on Tue Jul 25 19:58:54 2017 by modify_doxy.py rev. 533848