OpendTect  6.6
batchprog.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert
8  Date: 14-9-1998
9  RCS: $Id$
10 ________________________________________________________________________
11 
12  Batch programs should include this header, and define a BatchProgram::go().
13  If program args are needed outside this method, BP() can be accessed.
14 
15 */
16 
17 #include "batchmod.h"
18 
19 #include "applicationdata.h"
20 #include "batchjobdispatch.h"
21 #include "enums.h"
22 
23 #include "plugins.h"
24 #include "debug.h"
25 #include "od_ostream.h"
26 #include "odruncontext.h"
27 #include "genc.h"
28 
29 
30 #ifdef __msvc__
31 # ifndef _CONSOLE
32 # include "winmain.h"
33 # endif
34 #endif
35 
37 class CommandLineParser;
38 class JobCommunic;
39 class od_ostream;
40 class Timer;
41 
66 
67 public:
68 
69  enum Status { Start, ParseFail, ParseOK, CommFail, CommOK, LogFail, LogOK,
70  WorkWait, WorkStarted, WorkFail, WorkPaused, MoreToDo, WorkOK,
71  Killed };
73 
74  mExp(Batch) void loadModules();
81  mExp(Batch) bool doWork(od_ostream&);
85  mExp(Batch) bool isOK() const;
86  const CommandLineParser& clParser() { return *clparser_; }
87  const IOPar& pars() const { return *iopar_; }
88  IOPar& pars() { return *iopar_; }
89 
90  mExp(Batch) bool pauseRequested();
91  //<! pause requested (via socket) by primary host?
92  mExp(Batch) void setResumed();
93 
94  mExp(Batch) bool errorMsg(const uiString&,bool cc_stderr=false);
95  mExp(Batch) bool infoMsg(const char*,bool cc_stdout=false);
96 
97  static const char* sKeyDataDir() { return "datadir"; }
98  static const char* sKeySurveyDir() { return "surveydir"; }
99  static const char* sKeySimpleBatch() { return "noparfile"; }
100  static const char* sKeyFinishMsg() { return "Finished batch processing.";}
101 
108 
109 private:
110 
113 
114  CommandLineParser* clparser_ = nullptr;
115  IOPar* iopar_ = nullptr;
116  od_ostream* strm_ = nullptr;
117 
119  void killNotify(bool yn);
120 
121  JobCommunic* mmComm() { return comm_; }
122  Batch::ID jobId() { return jobid_; }
123  mExp(Batch) float getPriority() const { return priority_; }
124 
125  Status status_ = Start;
127 
128  JobCommunic* comm_ = nullptr;
130  float priority_ = 0.f;
131  bool strmismine_ = true;
133  Threads::Thread* thread_ = nullptr;
135 
137 
140  mExp(Batch) bool canReceiveRequests() const;
141  mExp(Batch) void initWork();
142  mExp(Batch) void startTimer();
144  mExp(Batch) void postWork(bool res);
145  void doFinalize();
146  mExp(Batch) void endWorkCB(CallBacker*);
147 
148  mExp(Batch) bool init();
150  //<! Parses command line arguments
151  bool initComm();
152  //<! Initializes job communication with primary host
153  bool initLogging();
154  //<! Initialized logging stream
155 
156  mExp(Batch) void modulesLoaded();
157  bool canStartdoWork() const;
158 
160 
161  mExp(Batch) static void deleteInstance(int retcode);
162  friend void Execute_batch(int*,char**);
163  friend void loadModulesCB(CallBacker*);
164  friend void launchDoWorkCB(CallBacker*);
165  friend void doWorkCB(CallBacker*);
166  friend class BatchServiceServerMgr;
167 
168 };
169 
170 #ifdef __unix__
171 void Execute_batch(int*,char**);
174 void doWorkCB(CallBacker*);
175 #endif
177 
178 
179 #define mDefLoadModules() \
180  void BatchProgram::loadModules() {
181 
182 #define mLoad1Module(mod1nm) \
183  mDefLoadModules() \
184  OD::ModDeps().ensureLoaded( mod1nm ); }
185 #define mLoad2Modules(mod1nm,mod2nm) \
186  mDefLoadModules() \
187  OD::ModDeps().ensureLoaded( mod1nm ); \
188  OD::ModDeps().ensureLoaded( mod2nm ); }
189 #define mLoad3Modules(mod1nm,mod2nm,mod3nm) \
190  mDefLoadModules() \
191  OD::ModDeps().ensureLoaded( mod1nm ); \
192  OD::ModDeps().ensureLoaded( mod2nm ); \
193  OD::ModDeps().ensureLoaded( mod3nm ); }
194 
195 #define mRetError(s) \
196 { errorMsg(::toUiString(s)); mDestroyWorkers; return false; }
197 
198 #define mRetJobErr(s) \
199 { \
200  if ( comm_ ) \
201  comm_->setState( JobCommunic::JobError ); \
202  mRetError(s) \
203 }
204 
205 #define mRetHostErr(s) \
206 { \
207  if ( comm_ ) comm_->setState( JobCommunic::HostError ); \
208  mRetError(s) \
209 }
210 
211 #define mStrmWithProcID(s) \
212 strm << "\n[" << process_id << "]: " << s << "." << od_newline
213 
214 #define mMessage(s) \
215 strm << s << '.' << od_newline
216 
217 #define mSetCommState(State) \
218 if ( comm_ ) \
219 { \
220  comm_->setState( JobCommunic::State ); \
221  if ( !comm_->updateState() ) \
222  mRetHostErr( comm_->errMsg() ) \
223 }
224 
225 #ifdef __prog__
226 # ifdef __win__
227 # include "_execbatch.h"
228 # endif
229 #define mMainIsDefined
230  int doMain( int argc, char** argv )
231  {
233  SetProgramArgs( argc, argv );
234  ApplicationData app;
235  Execute_batch( &argc, argv );
238 
239  return ApplicationData::exec();
240  }
241 
242  int main( int argc, char** argv )
243  {
244  ExitProgram( doMain(argc,argv) );
245  }
246 
247 #endif // __prog__
BatchProgram::BatchProgram
BatchProgram()
doWorkCB
void doWorkCB(CallBacker *)
Definition: _execbatch.h:117
Timer::comm_
::QTimerComm * comm_
Definition: timer.h:45
BatchProgram::eventLoopStarted
Notifier< BatchProgram > eventLoopStarted
Definition: batchprog.h:102
Strat::init
void init()
BatchProgram::BP
friend BatchProgram & BP()
BatchProgram::Status
Status
Definition: batchprog.h:69
ExitProgram
int ExitProgram(int ret)
BatchProgram::mExp
mExp(Batch) void startTimer()
BatchProgram::jobId
Batch::ID jobId()
Definition: batchprog.h:122
BatchProgram::mExp
mExp(Batch) static void deleteInstance(int retcode)
NamedCallBacker
CallBacker object with a name. Use if you want your object to be able to send and receive CallBack's,...
Definition: namedobj.h:72
BatchProgram::mExp
mExp(Batch) void endWorkCB(CallBacker *)
JobCommunic
Multi-machine socket communicator Handles the communication between a client and the primary host,...
Definition: jobcommunic.h:50
mGlobal
#define mGlobal(module)
Definition: commondefs.h:180
BatchProgram::mExp
mExp(Batch) bool infoMsg(const char *
BatchProgram::doWorkCB
void doWorkCB(CallBacker *)
ObjectSet< OD::JSON::Object >
BatchProgram::startDoWork
Notifier< BatchProgram > startDoWork
Definition: batchprog.h:103
mSCB
#define mSCB(fn)
Definition: callback.h:45
BatchProgram::sKeySimpleBatch
static const char * sKeySimpleBatch()
Definition: batchprog.h:99
BatchProgram::mExp
mExp(Batch) bool doWork(od_ostream &)
BatchServiceServerMgr
Service manager for all OpendTect batch programs. Allows informing od_main on the program of the batc...
Definition: batchserviceservermgr.h:26
BatchProgram::pause
Notifier< BatchProgram > pause
Definition: batchprog.h:104
BatchProgram::mExp
mExp(Batch) void postWork(bool res)
Execute_batch
void Execute_batch(int *pargc, char **argv)
Definition: _execbatch.h:96
od_ostream.h
BatchProgram::workMonitorCB
void workMonitorCB(CallBacker *)
BatchProgram::mExp
mExp(Batch) bool isOK() const
BatchProgram
Main object for 'standard' batch programs.
Definition: batchprog.h:64
BatchProgram::sKeySurveyDir
static const char * sKeySurveyDir()
Definition: batchprog.h:98
BatchProgram::mExp
mExp(Batch) bool pauseRequested()
BatchProgram::batchprogthreadlock_
Threads::Lock batchprogthreadlock_
Definition: batchprog.h:134
genc.h
BatchProgram::canStartdoWork
bool canStartdoWork() const
CallBacker
Inherit from this class to be able to send and/or receive CallBacks.
Definition: callback.h:185
launchDoWorkCB
void launchDoWorkCB(CallBacker *cb)
Definition: _execbatch.h:126
loadModulesCB
void loadModulesCB(CallBacker *)
Definition: _execbatch.h:110
BatchProgram::mExp
mExp(Batch) bool init()
CommandLineParser
Definition: commandlineparser.h:53
BatchProgram::pars
IOPar & pars()
Definition: batchprog.h:88
BatchProgram::mmComm
JobCommunic * mmComm()
Definition: batchprog.h:121
Timer
Timer class.
Definition: timer.h:25
main
int main(int argc, char **argv)
Definition: testprog.h:49
BatchProgram::requests_
ObjectSet< OD::JSON::Object > requests_
Definition: batchprog.h:136
BatchProgram::WorkWait
@ WorkWait
Definition: batchprog.h:70
BatchProgram::resume
Notifier< BatchProgram > resume
Definition: batchprog.h:105
OD::BatchProgCtxt
@ BatchProgCtxt
Definition: odruncontext.h:27
BatchProgram::doFinalize
void doFinalize()
plugins.h
BatchProgram::mExp
mExp(Batch) void modulesLoaded()
odruncontext.h
mClass
#define mClass(module)
Definition: commondefs.h:181
BatchProgram::mExp
mExp(Batch) void loadModules()
Notifier< BatchProgram >
batchjobdispatch.h
BatchProgram::initLogging
bool initLogging()
BatchProgram::pars
const IOPar & pars() const
Definition: batchprog.h:87
BatchProgram::jobid_
Batch::ID jobid_
Definition: batchprog.h:129
BatchProgram::sKeyFinishMsg
static const char * sKeyFinishMsg()
Definition: batchprog.h:100
BatchProgram::inst_
static BatchProgram * inst_
Definition: batchprog.h:159
BatchProgram::clParser
const CommandLineParser & clParser()
Definition: batchprog.h:86
BatchProgram::initComm
bool initComm()
BatchProgram::mExp
mExp(Batch) void setResumed()
BatchProgram::mExp
mExp(Batch) void initWork()
BatchProgram::killNotify
void killNotify(bool yn)
BatchProgram::mExp
mExp(Batch) float getPriority() const
Definition: batchprog.h:123
BatchProgram::mDeclareEnumUtils
mDeclareEnumUtils(Status)
_execbatch.h
BatchProgram::~BatchProgram
~BatchProgram()
uiString
String that is able to hold international (UTF-8) strings for the user interface.
Definition: uistring.h:121
Threads::Thread
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:282
BatchProgram::mODTextTranslationClass
mODTextTranslationClass(BatchProgram)
BatchProgram::sKeyDataDir
static const char * sKeyDataDir()
Definition: batchprog.h:97
BatchProgram::parseArguments
bool parseArguments()
enums.h
applicationdata.h
BatchProgram::statelock_
Threads::Lock statelock_
Definition: batchprog.h:126
OD::SetRunContext
void SetRunContext(RunCtxt)
Batch
Definition: batchjobdispatch.h:22
BatchProgram::eventLoopStartedCB
void eventLoopStartedCB(CallBacker *)
Batch::ID
int ID
Definition: batchjobdispatch.h:24
BatchProgram::timer_
Timer * timer_
Definition: batchprog.h:132
BatchProgram::endWork
Notifier< BatchProgram > endWork
Definition: batchprog.h:107
BatchProgram::killed
Notifier< BatchProgram > killed
Definition: batchprog.h:106
SetProgramArgs
void SetProgramArgs(int argc, char **argv, bool require_valid_dataroot=true)
BatchProgram::progKilled
void progKilled(CallBacker *)
BP
BatchProgram & BP()
od_ostream
OD class for stream write common access to the user log file, or std::cout in other than od_main.
Definition: od_ostream.h:26
Threads::Lock
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class.
Definition: threadlock.h:53
NotifierAccess::notify
void notify(const CallBack &, bool first=false) const
BatchProgram::mExp
mExp(Batch) bool canReceiveRequests() const
doMain
int doMain(int, char **)
IOPar
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:55
BatchProgram::mExp
mExp(Batch) bool errorMsg(const uiString &
debug.h
winmain.h

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021