The Gaudi Framework  v30r3 (a5ef0a68)
ApplicationMgr.cpp
Go to the documentation of this file.
1 // Include files
2 #include "ApplicationMgr.h"
3 
4 #include "AlgorithmManager.h"
5 #include "DLLClassManager.h"
6 #include "ServiceManager.h"
7 
10 #include "GaudiKernel/IRunable.h"
11 #include "GaudiKernel/IService.h"
13 
14 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/SmartIF.h"
17 
19 
20 #include "GaudiKernel/StatusCode.h"
21 #include "GaudiKernel/System.h"
22 #include "GaudiKernel/Time.h"
23 
24 #include "GaudiCoreSvcVersion.h"
25 
26 using System::getEnv;
27 using System::isEnvSet;
28 
29 #include <algorithm>
30 #include <cassert>
31 #include <ctime>
32 #include <limits>
33 
34 #define ON_DEBUG if ( UNLIKELY( m_outputLevel <= MSG::DEBUG ) )
35 #define ON_VERBOSE if ( UNLIKELY( m_outputLevel <= MSG::VERBOSE ) )
36 
38 
39 // Implementation class for the Application Manager. In this way the
40 // ApplicationMgr class is a fully insulated concrete class. Clients
41 // (main programs) will not need to re-compile if there are changes
42 // in the implementation
43 
44 //=======================================================================
45 // Constructor
46 //=======================================================================
48 {
49  // IInterface initialization
50  addRef(); // Initial count set to 1
51 
52  // Instantiate component managers
53  m_managers[IService::interfaceID().id()] = new ServiceManager( this );
54 
55  m_svcLocator = svcManager();
56 
57  // Instantiate internal services
58  // SvcLocator/Factory HAS to be already instantiated
59  m_classManager = new DLLClassManager( this );
60 
61  AlgorithmManager* algMgr = new AlgorithmManager( this );
62  m_managers[IAlgorithm::interfaceID().id()] = algMgr;
63  // m_managers[IAlgorithm::interfaceID().id()] = new HiveAlgorithmManager(this);
64 
65  // This property is not hosted in the ApplicationMgr instance
66  declareProperty( "AlgTypeAliases", algMgr->typeAliases(),
67  "Aliases of algorithm types, to replace an algorithm type for every instance" );
68 
69  // Add action handlers to the appropriate properties
76  // ServiceMgr Initialization loop checking
78  svcManager()->setLoopCheckEnabled( m_loopCheck );
79 
80  m_svcMapping = {"EvtDataSvc/EventDataSvc",
81  "DetDataSvc/DetectorDataSvc",
82  "HistogramSvc/HistogramDataSvc",
83  "HbookCnv::PersSvc/HbookHistSvc",
84  "RootHistCnv::PersSvc/RootHistSvc",
85  "EvtPersistencySvc/EventPersistencySvc",
86  "DetPersistencySvc/DetectorPersistencySvc",
87  "HistogramPersistencySvc/HistogramPersistencySvc"};
88 }
89 
90 //============================================================================
91 // IInterface implementation: queryInterface::addRef()
92 //============================================================================
94 {
95  if ( !ppvi ) {
96  return StatusCode::FAILURE;
97  }
98 
99  // try to find own/direct interfaces:
100  StatusCode sc = base_class::queryInterface( iid, ppvi );
101  if ( sc.isSuccess() ) return sc;
102 
103  // find indirect interfaces :
104  if ( ISvcLocator::interfaceID().versionMatch( iid ) ) {
105  return serviceLocator()->queryInterface( iid, ppvi );
106  }
107  if ( ISvcManager::interfaceID().versionMatch( iid ) ) {
108  return svcManager()->queryInterface( iid, ppvi );
109  }
110  if ( IAlgManager::interfaceID().versionMatch( iid ) ) {
111  return algManager()->queryInterface( iid, ppvi );
112  }
113  if ( IClassManager::interfaceID().versionMatch( iid ) ) {
114  return m_classManager->queryInterface( iid, ppvi );
115  }
116  if ( IMessageSvc::interfaceID().versionMatch( iid ) ) {
117  *ppvi = m_messageSvc.get();
118  if ( m_messageSvc ) m_messageSvc->addRef();
119  // Note that 0 can be a valid IMessageSvc pointer value (when used for
120  // MsgStream).
121  return StatusCode::SUCCESS;
122  }
123  *ppvi = nullptr;
124  return StatusCode::FAILURE;
125 }
126 
127 //============================================================================
128 // ApplicationMgr::i_startup()
129 //============================================================================
131 {
132 
133  StatusCode sc;
134 
135  // declare factories in current module
137 
138  // Note: we cannot use CommonMessaging methods here because MessageSvc is not there yet
139  MsgStream log( nullptr, name() );
140 
141  // Create the Message service
142  auto msgsvc = svcManager()->createService( Gaudi::Utils::TypeNameString( "MessageSvc", m_messageSvcType ) );
143  if ( !msgsvc ) {
144  log << MSG::FATAL << "Error creating MessageSvc of type " << m_messageSvcType << endmsg;
145  return StatusCode::FAILURE;
146  }
147  // Get the useful interface from Message services
148  m_messageSvc = m_svcLocator->service( "MessageSvc" );
149  if ( !m_messageSvc ) {
150  log << MSG::FATAL << "Error retrieving MessageSvc." << endmsg;
151  return StatusCode::FAILURE;
152  }
153 
154  auto jobsvc = svcManager()->createService( Gaudi::Utils::TypeNameString( "JobOptionsSvc", m_jobOptionsSvcType ) );
155  // Create the Job Options service
156  if ( !jobsvc ) {
157  log << MSG::FATAL << "Error creating JobOptionsSvc" << endmsg;
158  return StatusCode::FAILURE;
159  }
160  // Get the useful interface from Message services
161  m_jobOptionsSvc = m_svcLocator->service( "JobOptionsSvc" );
162  if ( !m_jobOptionsSvc ) {
163  log << MSG::FATAL << "Error retrieving JobOptionsSvc." << endmsg;
164  return StatusCode::FAILURE;
165  }
166 
167  auto jobOptsIProp = jobsvc.as<IProperty>();
168  if ( !jobOptsIProp ) {
169  log << MSG::FATAL << "Error locating JobOptionsSvc" << endmsg;
170  return StatusCode::FAILURE;
171  }
172  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "TYPE", m_jobOptionsType ) );
173  if ( !sc.isSuccess() ) {
174  log << MSG::FATAL << "Error setting TYPE option in JobOptionsSvc" << endmsg;
175  return sc;
176  }
177 
178  if ( !m_jobOptionsPreAction.empty() ) {
179  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "PYTHONPARAMS", m_jobOptionsPreAction ) );
180  if ( !sc.isSuccess() ) {
181  log << MSG::FATAL << "Error setting JobOptionsPreAction option in JobOptionsSvc" << endmsg;
182  return sc;
183  }
184  }
185 
186  if ( !m_jobOptionsPostAction.empty() ) {
187  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "PYTHONACTION", m_jobOptionsPostAction ) );
188  if ( !sc.isSuccess() ) {
189  log << MSG::FATAL << "Error setting JobOptionsPostAction option in JobOptionsSvc" << endmsg;
190  return sc;
191  }
192  }
193 
194  if ( !m_jobOptionsPath.empty() ) { // The command line takes precedence
195  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "PATH", m_jobOptionsPath ) );
196  if ( !sc.isSuccess() ) {
197  log << MSG::FATAL << "Error setting PATH option in JobOptionsSvc" << endmsg;
198  return sc;
199  }
200  } else if ( isEnvSet( "JOBOPTPATH" ) ) { // Otherwise the Environment JOBOPTPATH
201  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "PATH", getEnv( "JOBOPTPATH" ) ) );
202  if ( !sc.isSuccess() ) {
203  log << MSG::FATAL << "Error setting PATH option in JobOptionsSvc from env" << endmsg;
204  return sc;
205  }
206  } else { // Otherwise the default
207  sc = jobOptsIProp->setProperty( Gaudi::Property<std::string>( "PATH", "../options/job.opts" ) );
208  if ( !sc.isSuccess() ) {
209  log << MSG::FATAL << "Error setting PATH option in JobOptionsSvc to default" << endmsg;
210  return sc;
211  }
212  }
213  jobOptsIProp.reset();
214 
215  // Sets my default the Output Level of the Message service to be
216  // the same as this
217  auto msgSvcIProp = msgsvc.as<IProperty>();
218  msgSvcIProp->setProperty( Gaudi::Property<int>( "OutputLevel", m_outputLevel ) ).ignore();
219  msgSvcIProp.reset();
220 
221  sc = jobsvc->sysInitialize();
222  if ( !sc.isSuccess() ) {
223  log << MSG::FATAL << "Error initializing JobOptionsSvc" << endmsg;
224  return sc;
225  }
226  sc = msgsvc->sysInitialize();
227  if ( !sc.isSuccess() ) {
228  log << MSG::FATAL << "Error initializing MessageSvc" << endmsg;
229  return sc;
230  }
231 
232  // Make sure output level caches are up to date.
234 
235  return sc;
236 }
237 
238 //============================================================================
239 // IAppMgrUI implementation: ApplicationMgr::configure()
240 //============================================================================
242 {
243 
244  // Check if the state is compatible with the transition
245  MsgStream tlog( m_messageSvc, name() );
247  tlog << MSG::INFO << "Already Configured" << endmsg;
248  return StatusCode::SUCCESS;
249  }
251  tlog << MSG::FATAL << "configure: Invalid state \"" << m_state << "\"" << endmsg;
252  return StatusCode::FAILURE;
253  }
255 
256  // Reset application return code.
258 
259  StatusCode sc;
260  sc = i_startup();
261  if ( !sc.isSuccess() ) {
262  return sc;
263  }
264 
265  {
267  // Get my own options using the Job options service
268  if ( log.level() <= MSG::DEBUG ) log << MSG::DEBUG << "Getting my own properties" << endmsg;
269  sc = m_jobOptionsSvc->setMyProperties( name(), this );
270  if ( !sc.isSuccess() ) {
271  log << MSG::WARNING << "Problems getting my properties from JobOptionsSvc" << endmsg;
272  return sc;
273  }
274  }
275 
276  // Make sure that the OutputLevel is in sync
279 
280  // Check current outputLevel to eventually inform the MessageSvc
281  if ( m_outputLevel != MSG::NIL && !m_appName.empty() ) {
282  // Print a welcome message
283  log << MSG::ALWAYS << std::endl
284  << "=================================================================="
285  << "==================================================================" << std::endl
286  << " "
287  << " Welcome to " << m_appName.value();
288 
289  if ( !m_appVersion.empty() ) {
290  log << MSG::ALWAYS << " version " << m_appVersion.value();
291  } else {
292  log << MSG::ALWAYS << " (GaudiCoreSvc "
294 #if GAUDICORESVC_PATCH_VERSION
296 #endif
297  << ")";
298  }
299 
300  // Add the host name and current time to the message
301  log << MSG::ALWAYS << std::endl
302  << " "
303  << " running on " << System::hostName() << " on " << Gaudi::Time::current().format( true ) << std::endl
304  << "=================================================================="
305  << "==================================================================" << endmsg;
306  }
307 
308  // print all own properties if the options "PropertiesPrint" is set to true
309  if ( m_propertiesPrint ) {
310  const auto& properties = getProperties();
311  log << MSG::ALWAYS << "List of ALL properties of " << System::typeinfoName( typeid( *this ) ) << "/" << this->name()
312  << " #properties = " << properties.size() << endmsg;
313  for ( const auto& property : properties ) {
314  log << "Property ['Name': Value] = " << *property << endmsg;
315  }
316  }
317 
318  // Check if StatusCode need to be checked
319  if ( m_codeCheck ) {
321  sc = svcManager()->addService( "StatusCodeSvc", -9999 );
322  if ( sc.isFailure() ) {
323  log << MSG::FATAL << "Error adding StatusCodeSvc" << endmsg;
324  return StatusCode::FAILURE;
325  } else {
326  ON_VERBOSE
327  log << MSG::VERBOSE << "added service StatusCodeSvc" << endmsg;
328  }
329  } else {
331  }
332 
333  // set the requested environment variables
334  for ( auto& var : m_environment ) {
335  const std::string& name = var.first;
336  const std::string& value = var.second;
337  std::string old = System::getEnv( name.c_str() );
338  const MSG::Level lvl = ( !old.empty() && ( old != "UNKNOWN" ) ) ? MSG::WARNING : MSG::DEBUG;
339  if ( UNLIKELY( m_outputLevel <= lvl ) ) log << lvl << "Setting " << name << " = " << value << endmsg;
340  System::setEnv( name, value );
341  }
342 
343  // Declare Service Types
344  for ( auto& j : m_svcMapping ) {
346  if ( svcManager()->declareSvcType( itm.name(), itm.type() ).isFailure() ) {
347  log << MSG::ERROR << "configure: declaring svc type:'" << j << "' failed." << endmsg;
348  return StatusCode::FAILURE;
349  } else {
350  ON_VERBOSE
351  log << MSG::VERBOSE << "declared service " << j << endmsg;
352  }
353  }
354  for ( auto& j : m_svcOptMapping ) {
356  if ( svcManager()->declareSvcType( itm.name(), itm.type() ).isFailure() ) {
357  log << MSG::ERROR << "declaring svc type:'" << j << "' failed." << endmsg;
358  return StatusCode::FAILURE;
359  }
360  }
361 
362  //--------------------------------------------------------------------------
363  // Declare other Services and Algorithms by loading DLL's
364  sc = decodeDllNameList();
365  if ( sc.isFailure() ) {
366  log << MSG::ERROR << "Failure loading declared DLL's" << endmsg;
367  return sc;
368  }
369 
370  //--------------------------------------------------------------------------
371  // Deal with the services explicitly declared by the user.
372  sc = decodeExtSvcNameList();
373  if ( sc.isFailure() ) {
374  log << MSG::ERROR << "Failure during external service association" << endmsg;
375  return sc;
376  }
377 
379  if ( sc.isFailure() ) {
380  log << MSG::ERROR << "Failure during external service creation" << endmsg;
381  return sc;
382  }
383 
384  //--------------------------------------------------------------------------
385  // Retrieve intrinsic services. If needed configure them.
386  //--------------------------------------------------------------------------
387  const Gaudi::Utils::TypeNameString evtloop_item( m_eventLoopMgr );
388  sc = svcManager()->addService( evtloop_item, ServiceManager::DEFAULT_SVC_PRIORITY * 10 );
389  if ( !sc.isSuccess() ) {
390  log << MSG::FATAL << "Error adding :" << m_eventLoopMgr << endmsg;
391  return sc;
392  } else {
393  ON_VERBOSE
394  log << MSG::VERBOSE << "added service " << evtloop_item << endmsg;
395  }
396 
398  if ( !m_runable ) {
399  log << MSG::FATAL << "Error retrieving Runable: " << m_runableType.value() << "\n Check option ApplicationMgr."
400  << m_runableType.name() << endmsg;
401  return sc;
402  }
403  m_processingMgr = m_svcLocator->service( evtloop_item );
404  if ( !m_processingMgr ) {
405  log << MSG::FATAL << "Error retrieving Processing manager: " << m_eventLoopMgr.value()
406  << "\n Check option ApplicationMgr." << m_eventLoopMgr.name() << "\n No events will be processed." << endmsg;
407  return sc;
408  }
409 
410  // Establish Update Handlers for ExtSvc and DLLs Properties
411  m_extSvcNameList.declareUpdateHandler( &ApplicationMgr::extSvcNameListHandler, this );
412  m_createSvcNameList.declareUpdateHandler( &ApplicationMgr::createSvcNameListHandler, this );
413  m_dllNameList.declareUpdateHandler( &ApplicationMgr::dllNameListHandler, this );
414 
415  if ( m_actHistory ) {
416  // Create HistorySvc with a priority to ensure it's initialized last, finalized first
417  sc = svcManager()->addService( "HistorySvc", std::numeric_limits<int>::max() );
418  if ( sc.isFailure() ) {
419  log << MSG::FATAL << "Error adding HistorySvc" << endmsg;
420  return StatusCode::FAILURE;
421  }
422  }
423 
424  log << MSG::INFO << "Application Manager Configured successfully" << endmsg;
426  return StatusCode::SUCCESS;
427 }
428 
429 //============================================================================
430 // IAppMgrUI implementation: ApplicationMgr::initialize()
431 //============================================================================
433 {
434  StatusCode sc;
435 
437 
438  // Make sure output level caches are up to date.
440 
441  // I cannot add these services in configure() because they are coming from GaudiUtils
442  // and it messes up genconf when rebuilding it.
443  if ( m_stopOnSignal ) {
444  // Instantiate the service that schedules a stop when a signal is received
445  std::string svcname( "Gaudi::Utils::StopSignalHandler" );
446  sc = svcManager()->addService( svcname );
447  if ( sc.isFailure() ) {
448  log << MSG::INFO << "Cannot instantiate " << svcname << "signals will be ignored" << endmsg;
449  }
450  }
451 
452  if ( m_stalledEventMonitoring ) {
453  // Instantiate the service that schedules a stop when a signal is received
454  std::string svcname( "StalledEventMonitor" );
455  sc = svcManager()->addService( svcname );
456  if ( sc.isFailure() ) {
457  log << MSG::INFO << "Cannot instantiate " << svcname << "signals will be ignored" << endmsg;
458  }
459  }
460 
462  log << MSG::INFO << "Already Initialized!" << endmsg;
463  return StatusCode::SUCCESS;
464  }
466  log << MSG::FATAL << "initialize: Invalid state \"" << m_state << "\"" << endmsg;
467  return StatusCode::FAILURE;
468  }
470 
471  //--------------------------------------------------------------------------
472  // Initialize the list of top Services
473  //--------------------------------------------------------------------------
474  sc = svcManager()->initialize();
475  if ( !sc.isSuccess() ) return sc;
476 
477  //--------------------------------------------------------------------------
478  // Final steps: Inform user and change internal state
479  //--------------------------------------------------------------------------
480  log << MSG::INFO << "Application Manager Initialized successfully" << endmsg;
482 
483  return sc;
484 }
485 
486 //============================================================================
487 // IAppMgrUI implementation: ApplicationMgr::start()
488 //============================================================================
490 {
491 
493  StatusCode sc;
494 
496  log << MSG::INFO << "Already Initialized!" << endmsg;
497  return StatusCode::SUCCESS;
498  }
500  log << MSG::FATAL << "start: Invalid state \"" << m_state << "\"" << endmsg;
501  return StatusCode::FAILURE;
502  }
504 
505  //--------------------------------------------------------------------------
506  // Initialize the list of top Services
507  //--------------------------------------------------------------------------
508  sc = svcManager()->start();
509  if ( !sc.isSuccess() ) return sc;
510 
511  //--------------------------------------------------------------------------
512  // Final steps: Inform user and change internal state
513  //--------------------------------------------------------------------------
514  log << MSG::INFO << "Application Manager Started successfully" << endmsg;
516 
517  return sc;
518 }
519 
520 //============================================================================
521 // IAppMgrUI implementation: ApplicationMgr::nextEvent(int)
522 //============================================================================
524 {
527  log << MSG::FATAL << "nextEvent: Invalid state \"" << m_state << "\"" << endmsg;
528  return StatusCode::FAILURE;
529  }
530  if ( !m_processingMgr ) {
532  log << MSG::FATAL << "No event processing manager specified. Check option: " << m_eventLoopMgr.name() << endmsg;
533  return StatusCode::FAILURE;
534  }
535  return m_processingMgr->nextEvent( maxevt );
536 }
537 
538 //============================================================================
539 // IAppMgrUI implementation: ApplicationMgr::stop()
540 //============================================================================
542 {
543 
545  StatusCode sc;
546 
548  log << MSG::INFO << "Already Initialized!" << endmsg;
549  return StatusCode::SUCCESS;
550  }
552  log << MSG::FATAL << "stop: Invalid state \"" << m_state << "\"" << endmsg;
553  return StatusCode::FAILURE;
554  }
556 
557  // Stop independently managed Algorithms
558  sc = algManager()->stop();
559  if ( !sc.isSuccess() ) return sc;
560 
561  //--------------------------------------------------------------------------
562  // Stop the list of top Services
563  //--------------------------------------------------------------------------
564  sc = svcManager()->stop();
565  if ( !sc.isSuccess() ) return sc;
566 
567  //--------------------------------------------------------------------------
568  // Final steps: Inform user and change internal state
569  //--------------------------------------------------------------------------
570  log << MSG::INFO << "Application Manager Stopped successfully" << endmsg;
572 
573  return sc;
574 }
575 
576 //============================================================================
577 // IAppMgrUI implementation: ApplicationMgr::finalize()
578 //============================================================================
580 {
583  log << MSG::INFO << "Already Finalized" << endmsg;
584  return StatusCode::SUCCESS;
585  }
587  log << MSG::FATAL << "finalize: Invalid state \"" << m_state << "\"" << endmsg;
588  return StatusCode::FAILURE;
589  }
591 
592  // disable message suppression in finalize
593  m_svcLocator->service<IProperty>( "MessageSvc" )
594  ->setProperty( Gaudi::Property<bool>( "enableSuppression", false ) )
595  .ignore();
596 
597  // Finalize independently managed Algorithms
598  StatusCode sc = algManager()->finalize();
599  if ( sc.isFailure() ) {
600  log << MSG::WARNING << "Failed to finalize an algorithm." << endmsg;
602  }
603 
604  // Finalize all Services
605  sc = svcManager()->finalize();
606  if ( sc.isFailure() ) {
607  log << MSG::WARNING << "Failed to finalize a service." << endmsg;
609  }
610 
611  // svcManager()->removeService( (IService*) m_processingMgr.get() );
612  // svcManager()->removeService( (IService*) m_runable.get() );
613 
614  if ( m_codeCheck ) {
616  }
617 
618  if ( sc.isSuccess() ) {
619  log << MSG::INFO << "Application Manager Finalized successfully" << endmsg;
620  } else {
621  log << MSG::ERROR << "Application Manager failed to finalize" << endmsg;
622  }
623 
625  return sc;
626 }
627 
628 //============================================================================
629 // IAppMgrUI implementation: ApplicationMgr::terminate()
630 //============================================================================
632 {
634 
636  log << MSG::INFO << "Already Offline" << endmsg;
637  return StatusCode::SUCCESS;
638  }
640  log << MSG::FATAL << "terminate: Invalid state \"" << m_state << "\"" << endmsg;
641  return StatusCode::FAILURE;
642  }
643  // release all Services
645 
646  if ( m_returnCode.value() == Gaudi::ReturnCode::Success ) {
647  log << MSG::INFO << "Application Manager Terminated successfully" << endmsg;
648  } else if ( m_returnCode.value() == Gaudi::ReturnCode::ScheduledStop ) {
649  log << MSG::INFO << "Application Manager Terminated successfully with a user requested ScheduledStop" << endmsg;
650  } else {
651  log << MSG::ERROR << "Application Manager Terminated with error code " << m_returnCode.value() << endmsg;
652  }
653 
654  { // Force a disable the auditing of finalize for MessageSvc
655  auto prop = m_messageSvc.as<IProperty>();
656  if ( prop ) {
657  prop->setProperty( Gaudi::Property<bool>( "AuditFinalize", false ) ).ignore();
658  }
659  }
660  { // Force a disable the auditing of finalize for JobOptionsSvc
661  auto prop = m_jobOptionsSvc.as<IProperty>();
662  if ( prop ) {
663  prop->setProperty( Gaudi::Property<bool>( "AuditFinalize", false ) ).ignore();
664  }
665  }
666 
667  // finalize MessageSvc
668  auto svc = m_messageSvc.as<IService>();
669  if ( !svc ) {
670  log << MSG::ERROR << "Could not get the IService interface of the MessageSvc" << endmsg;
671  } else {
672  svc->sysFinalize().ignore();
673  }
674 
675  // finalize JobOptionsSvc
676  svc = m_jobOptionsSvc.as<IService>();
677  if ( !svc ) {
678  log << MSG::ERROR << "Could not get the IService interface of the JobOptionsSvc" << endmsg;
679  } else {
680  svc->sysFinalize().ignore();
681  }
682 
684  return StatusCode::SUCCESS;
685 }
686 
687 //============================================================================
688 // Reach the required state going through all the needed transitions
689 //============================================================================
691 {
693 
694  switch ( state ) {
695 
697  switch ( m_state ) {
699  return StatusCode::SUCCESS;
700  break;
702  return terminate();
703  break;
704  default: // Gaudi::StateMachine::INITIALIZED or Gaudi::StateMachine::RUNNING
706  if ( sc.isSuccess() ) {
707  return terminate();
708  }
709  break;
710  }
711  break;
712 
714  switch ( m_state ) {
716  return StatusCode::SUCCESS;
717  break;
719  return configure();
720  break;
722  return finalize();
723  break;
724  default: // Gaudi::StateMachine::RUNNING
726  if ( sc.isSuccess() ) {
727  return finalize();
728  }
729  break;
730  }
731  break;
732 
734  switch ( m_state ) {
736  return StatusCode::SUCCESS;
737  break;
739  return initialize();
740  break;
742  return stop();
743  break;
744  default: // Gaudi::StateMachine::OFFLINE
746  if ( sc.isSuccess() ) {
747  return initialize();
748  }
749  break;
750  }
751  break;
752 
754  switch ( m_state ) {
756  return StatusCode::SUCCESS;
757  break;
759  return start();
760  break;
761  default: // Gaudi::StateMachine::OFFLINE or Gaudi::StateMachine::CONFIGURED
763  if ( sc.isSuccess() ) {
764  return start();
765  }
766  break;
767  }
768  break;
769  }
770 
771  // If I get here, there has been a problem in the recursion
772 
773  if ( ignoreFailures ) {
774  // force the new state
775  m_state = state;
776  return StatusCode::SUCCESS;
777  }
778 
779  return sc;
780 }
781 
782 //============================================================================
783 // IAppMgrUI implementation: ApplicationMgr::run()
784 //============================================================================
786 {
788 
790  if ( sc.isSuccess() ) {
792  if ( m_runable != 0 ) { // loop over the events
793  sc = m_runable->run();
794  if ( !sc.isSuccess() ) {
795  log << MSG::FATAL << "Application execution failed. Ending the job." << endmsg;
796  }
797  } else {
798  log << MSG::FATAL << "Application has no runable object. Check option:" << m_runableType.name() << endmsg;
799  }
800  }
801  if ( sc.isSuccess() ) { // try to close cleanly
803  }
804  // either the runable failed of the stut-down
805  if ( sc.isFailure() ) { // try to close anyway (but keep the StatusCode unchanged)
807  }
808  return sc;
809 }
810 
811 //============================================================================
812 // IEventProcessor implementation: executeEvent(void* par)
813 //============================================================================
815 {
818  if ( m_processingMgr ) {
819  return m_processingMgr->executeEvent( par );
820  }
821  }
822  log << MSG::FATAL << "executeEvent: Invalid state \"" << FSMState() << "\"" << endmsg;
823  return StatusCode::FAILURE;
824 }
825 
826 //============================================================================
827 // IEventProcessor implementation: executeRun(int)
828 //============================================================================
830 {
833  if ( m_processingMgr ) {
834  return m_processingMgr->executeRun( evtmax );
835  }
836  log << MSG::WARNING << "No EventLoop Manager specified " << endmsg;
837  return StatusCode::SUCCESS;
838  }
839  log << MSG::FATAL << "executeRun: Invalid state \"" << FSMState() << "\"" << endmsg;
840  return StatusCode::FAILURE;
841 }
842 
843 //============================================================================
844 // IEventProcessor implementation: stopRun(int)
845 //============================================================================
847 {
850  if ( m_processingMgr ) {
851  return m_processingMgr->stopRun();
852  }
853  log << MSG::WARNING << "No EventLoop Manager specified " << endmsg;
854  return StatusCode::SUCCESS;
855  }
856  log << MSG::FATAL << "stopRun: Invalid state \"" << FSMState() << "\"" << endmsg;
857  return StatusCode::FAILURE;
858 }
859 // Implementation of IAppMgrUI::name
860 const std::string& ApplicationMgr::name() const { return m_name; }
861 
862 // implementation of IService::state
864 // implementation of IService::state
866 
867 //============================================================================
868 // implementation of IService::reinitilaize
869 //============================================================================
871 {
873  StatusCode sc;
875  throw GaudiException( "Cannot reinitialize application if not INITIALIZED or RUNNING",
876  "ApplicationMgr::reinitialize", StatusCode::FAILURE );
877  }
880  }
881  sc = svcManager()->reinitialize();
882  if ( sc.isFailure() ) retval = sc;
883  sc = algManager()->reinitialize();
884  if ( sc.isFailure() ) retval = sc;
885  return retval;
886 }
887 
888 //============================================================================
889 // implementation of IService::reinitiaize
890 //============================================================================
892 {
894  StatusCode sc;
896  throw GaudiException( "Cannot restart application if not RUNNING", "ApplicationMgr::restart", StatusCode::FAILURE );
897  }
898  sc = svcManager()->restart();
899  if ( sc.isFailure() ) retval = sc;
900  sc = algManager()->restart();
901  if ( sc.isFailure() ) retval = sc;
902  return retval;
903 }
904 
905 //============================================================================
906 // SI Go Handler
907 //============================================================================
909 {
910 
912  StatusCode sc;
913 
914  // Re-initialize everything
915  sc = reinitialize();
916  // Execute a number of events
917  executeRun( m_evtMax );
918 
919  return;
920 }
921 
922 //============================================================================
923 // SI Exit Handler
924 //============================================================================
926 {
927  StatusCode status;
928  status = finalize();
929  status = terminate();
930  ::exit( 0 );
931 }
932 
933 //============================================================================
934 // Handle properties of the event loop manager (Top alg/Output stream list)
935 //============================================================================
937 {
938  if ( m_processingMgr ) {
939  auto props = m_processingMgr.as<IProperty>();
940  if ( props ) props->setProperty( p ).ignore();
941  }
942 }
943 
944 //============================================================================
945 // External Service List handler
946 //============================================================================
948 {
949  if ( !( decodeCreateSvcNameList() ).isSuccess() ) {
950  throw GaudiException( "Failed to create ext services", "MinimalEventLoopMgr::createSvcNameListHandler",
952  }
953 }
954 //============================================================================
955 // decodeCreateSvcNameList
956 //============================================================================
958 {
960  const auto& theNames = m_createSvcNameList.value();
961  auto it = theNames.begin();
962  auto et = theNames.end();
963  while ( result.isSuccess() && it != et ) {
964  Gaudi::Utils::TypeNameString item( *it++ );
965  if ( ( result = svcManager()->addService( item, ServiceManager::DEFAULT_SVC_PRIORITY ) ).isFailure() ) {
967  log << MSG::ERROR << "decodeCreateSvcNameList: Cannot create service " << item.type() << "/" << item.name()
968  << endmsg;
969  } else {
970  ON_DEBUG
971  {
973  log << MSG::DEBUG << "decodeCreateSvcNameList: Created service " << item.type() << "/" << item.name() << endmsg;
974  }
975  }
976  }
977  return result;
978 }
979 
980 //============================================================================
981 // External Service List handler
982 //============================================================================
984 {
985  if ( !( decodeExtSvcNameList() ).isSuccess() ) {
986  throw GaudiException( "Failed to declare ext services", "MinimalEventLoopMgr::extSvcNameListHandler",
988  }
989 }
990 
991 //============================================================================
992 // decodeExtSvcNameList
993 //============================================================================
995 {
997 
998  const auto& theNames = m_extSvcNameList.value();
999 
1000  auto it = theNames.begin();
1001  auto et = theNames.end();
1002  while ( result.isSuccess() && it != et ) {
1003  Gaudi::Utils::TypeNameString item( *it++ );
1004  if ( m_extSvcCreates ) {
1005  if ( ( result = svcManager()->addService( item, ServiceManager::DEFAULT_SVC_PRIORITY ) ).isFailure() ) {
1007  log << MSG::ERROR << "decodeExtSvcNameList: Cannot create service " << item.type() << "/" << item.name()
1008  << endmsg;
1009  }
1010  } else {
1011  if ( ( result = svcManager()->declareSvcType( item.name(), item.type() ) ).isFailure() ) {
1013  log << MSG::ERROR << "decodeExtSvcNameList: Cannot declare service " << item.type() << "/" << item.name()
1014  << endmsg;
1015  }
1016  }
1017  }
1018  return result;
1019 }
1020 
1021 //============================================================================
1022 // Dll List handler
1023 //============================================================================
1025 {
1026  if ( !( decodeDllNameList() ).isSuccess() ) {
1027  throw GaudiException( "Failed to load DLLs.", "MinimalEventLoopMgr::dllNameListHandler", StatusCode::FAILURE );
1028  }
1029 }
1030 
1031 //============================================================================
1032 // decodeDllNameList
1033 //============================================================================
1035 {
1036 
1039 
1040  // Clean up multiple entries from DLL list
1041  // -------------------------------------------------------------------------
1042  std::vector<std::string> newList;
1043  std::map<std::string, unsigned int> dllInList, duplicateList;
1044  {
1045  for ( const auto it : m_dllNameList ) {
1046  if ( 0 == dllInList[it] ) {
1047  newList.push_back( it ); // first instance of this module
1048  } else {
1049  ++duplicateList[it];
1050  } // module listed multiple times
1051  ++dllInList[it]; // increment count for this module
1052  }
1053  }
1054  // m_dllNameList = newList; // update primary list to new, filtered list (do not use the
1055  // property itself otherwise we get called again infinitely)
1056  // List modules that were in there twice..
1057  ON_DEBUG if ( !duplicateList.empty() )
1058  {
1059  log << MSG::DEBUG << "Removed duplicate entries for modules : ";
1060  for ( auto it = duplicateList.begin(); it != duplicateList.end(); ++it ) {
1061  log << it->first << "(" << 1 + it->second << ")";
1062  if ( it != --duplicateList.end() ) log << ", ";
1063  }
1064  log << endmsg;
1065  }
1066  // -------------------------------------------------------------------------
1067 
1068  const std::vector<std::string>& theNames = newList;
1069 
1070  // only load the new dlls or previously failed dlls
1071  ON_DEBUG log << MSG::DEBUG << "Loading declared DLL's" << endmsg;
1072 
1073  std::vector<std::string> successNames, failNames;
1074  for ( const auto& it : theNames ) {
1075  if ( std::find( m_okDlls.rbegin(), m_okDlls.rend(), it ) == m_okDlls.rend() ) {
1076  // found a new module name
1077  StatusCode status = m_classManager->loadModule( it );
1078  if ( status.isFailure() ) {
1079  failNames.push_back( it );
1080  result = StatusCode::FAILURE;
1081  } else {
1082  successNames.push_back( it );
1083  }
1084  }
1085  }
1086 
1087  // report back to the user and store the names of the succesfully loaded dlls
1088  if ( !successNames.empty() ) {
1089  log << MSG::INFO << "Successfully loaded modules : ";
1090  for ( auto it = successNames.begin(); it != successNames.end(); it++ ) {
1091  log << ( *it );
1092  if ( ( it + 1 ) != successNames.end() ) log << ", ";
1093  // save name
1094  m_okDlls.push_back( *it );
1095  }
1096  log << endmsg;
1097  }
1098 
1099  if ( result == StatusCode::FAILURE ) {
1100  log << MSG::WARNING << "Failed to load modules: ";
1101  for ( auto it = failNames.begin(); it != failNames.end(); it++ ) {
1102  log << ( *it );
1103  if ( ( it + 1 ) != failNames.end() ) log << ", ";
1104  }
1105  log << endmsg;
1106  }
1107  return result;
1108 }
1109 
1110 //============================================================================
1111 // Plugin debug level handler
1112 //============================================================================
1114 {
1115  // Setup debug level for the plugin system
1117  log << MSG::INFO << "Updating Gaudi::PluginService::SetDebug(level) to level=" << m_pluginDebugLevel << endmsg;
1119 }
1120 
1121 //============================================================================
1122 // Init loop check handler
1123 //============================================================================
1125 {
1127 }
1128 
1130 {
1131  resetMessaging();
1132  for ( auto& mgrItem : m_managers ) {
1133  mgrItem.second->outputLevelUpdate();
1134  }
1135 }
The ServiceManager class is in charge of the creation of concrete instances of Services.
Gaudi::StateMachine::State m_targetState
Internal State.
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:411
The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.
StatusCode initialize() override
constexpr int FinalizationFailure
Error codes for operation failures.
Definition: AppReturnCode.h:34
#define UNLIKELY(x)
Definition: Kernel.h:122
virtual StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY)=0
Add a service to the "active" list of services of the factory.
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Gaudi::Property< bool > m_propertiesPrint
virtual StatusCode stopRun()=0
Schedule a stop of the current event processing.
T empty(T...args)
virtual StatusCode run()=0
Run the class implementation.
Define general base for Gaudi exception.
StatusCode decodeDllNameList()
Gaudi::Property< int > m_returnCode
Property to record the error conditions occurring during the running.
GAUDI_API int setEnv(const std::string &name, const std::string &value, int overwrite=1)
Set an environment variables.
Definition: System.cpp:534
void SIExitHandler(Gaudi::Details::PropertyBase &theProp)
Gaudi::Property< bool > m_stopOnSignal
Property to enable/disable the "stop on signal" service.
StatusCode loadModule(const std::string &module, bool fireIncident=true) override
Gaudi::Property< bool > m_codeCheck
Gaudi::Property< int > m_evtMax
SmartIF< IRunable > m_runable
Reference to the runable object.
Implementation of property with value of concrete type.
Definition: Property.h:381
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
virtual PropertyBase & declareUpdateHandler(std::function< void(PropertyBase &)> fun)=0
set new callback for update
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
void SIGoHandler(Gaudi::Details::PropertyBase &theProp)
#define GAUDICORESVC_PATCH_VERSION
T rend(T...args)
ManagersMap m_managers
Map of known component managers.
Gaudi::Property< std::string > m_jobOptionsType
bool isSuccess() const
Definition: StatusCode.h:287
const std::string & name() const override
T endl(T...args)
static Time current()
Returns the current time.
Definition: Time.cpp:112
Gaudi::Property< std::map< std::string, std::string > > m_environment
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
virtual StatusCode executeEvent(void *par=0)=0
Process single event.
Gaudi::Property< std::string > m_appVersion
T end(T...args)
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:40
std::vector< std::string > m_okDlls
names of successfully loaded dlls
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
StatusCode nextEvent(int maxevt) override
SmartIF< IJobOptionsSvc > m_jobOptionsSvc
Reference to JobOption service.
AlgTypeAliasesMap & typeAliases()
void createSvcNameListHandler(Gaudi::Details::PropertyBase &)
bool isFailure() const
Definition: StatusCode.h:139
SmartIF< IEventProcessor > m_processingMgr
Reference to processing manager object.
StatusCode terminate() override
SmartIF< ISvcManager > & svcManager()
virtual StatusCode start()=0
Start (from INITIALIZED to RUNNING).
constexpr int ScheduledStop
Definition: AppReturnCode.h:27
Gaudi::Property< std::string > m_eventLoopMgr
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
virtual StatusCode nextEvent(int maxevt)=0
Process the next maxevt events.
void evtLoopPropertyHandler(Gaudi::Details::PropertyBase &theProp)
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
T push_back(T...args)
Interface ID class.
Definition: IInterface.h:29
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
Gaudi::Property< int > m_outputLevel
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:115
Helper class to parse a string of format "type/name".
StatusCode executeRun(int evtmax) override
implementation of IEventProcessor::executeRun(int)
virtual StatusCode stop()=0
Stop (from RUNNING to INITIALIZED).
virtual StatusCode reinitialize()=0
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Gaudi::StateMachine::State targetFSMState() const override
void extSvcNameListHandler(Gaudi::Details::PropertyBase &theProp)
void initLoopCheckHndlr(Gaudi::Details::PropertyBase &)
General service interface definition.
Definition: IService.h:18
#define ON_DEBUG
SmartIF< ISvcLocator > m_svcLocator
Reference to its own service locator (must be instantiated prior to any service!) ...
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
virtual SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype)=0
Creates and instance of a service type that has been declared beforehand and assigns it a name...
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition of the basic interface.
Definition: IInterface.h:277
virtual StatusCode setMyProperties(const std::string &client, IProperty *me)=0
Override default properties of the calling client.
Gaudi::Property< std::vector< std::string > > m_extSvcNameList
#define DECLARE_OBJECT_FACTORY(x)
Definition: ObjectFactory.h:15
void dllNameListHandler(Gaudi::Details::PropertyBase &theProp)
Gaudi::Property< std::string > m_runableType
Gaudi::Property< bool > m_stalledEventMonitoring
Property to enable/disable the monitoring and reporting of stalled events.
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
StatusCode GoToState(Gaudi::StateMachine::State state, bool ignoreFailures=false)
Reach a state from current state (whichever it is) going through the correct transitions.
StatusCode finalize() override
Gaudi::Property< std::vector< std::string > > m_svcOptMapping
virtual StatusCode initialize()=0
Initialization (from CONFIGURED to INITIALIZED).
#define GAUDICORESVC_MINOR_VERSION
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
Gaudi::Property< std::string > m_jobOptionsPreAction
Gaudi::Property< std::vector< std::string > > m_svcMapping
Gaudi::Property< std::vector< std::string > > m_dllNameList
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:433
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
implementation of IInterface::queryInterface
StatusCode executeEvent(void *par) override
implementation of IEventProcessor::executeEvent(void*)
The Application Manager class.
Gaudi::Property< bool > m_actHistory
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:62
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
StatusCode decodeExtSvcNameList()
T find(T...args)
GAUDI_API const std::string & hostName()
Host name.
Definition: System.cpp:337
Gaudi::Property< bool > m_extSvcCreates
SmartIF< DLLClassManager > m_classManager
Reference to the class manager.
Gaudi::Property< std::string > m_jobOptionsSvcType
StatusCode start() override
Gaudi::Property< std::vector< std::string > > m_createSvcNameList
StatusCode restart() override
StatusCode configure() override
T begin(T...args)
void pluginDebugPropertyHandler(Gaudi::Details::PropertyBase &theProp)
virtual StatusCode restart()=0
Initialization (from RUNNING to RUNNING, via INITIALIZED).
StatusCode stopRun() override
implementation of IEventProcessor::stopRun()
const std::string & type() const
StatusCode decodeCreateSvcNameList()
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
virtual StatusCode finalize()=0
Finalize (from INITIALIZED to CONFIGURED).
Gaudi::Property< std::string > m_jobOptionsPostAction
T c_str(T...args)
StatusCode i_startup()
Internal startup routine.
StatusCode stop() override
std::string m_name
Name.
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:42
Gaudi::Property< std::string > m_messageSvcType
#define ON_VERBOSE
StatusCode reinitialize() override
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
Gaudi::StateMachine::State m_state
Internal State.
Gaudi::Property< std::string > m_appName
Gaudi::StateMachine::State FSMState() const override
const std::string & name() const
SmartIF< ISvcLocator > & serviceLocator() const override
Needed to locate the message service.
virtual void setLoopCheckEnabled(bool en=true)=0
Set the value of the initialization loop check flag.
virtual StatusCode executeRun(int maxevt)=0
Process the maxevt events as a Run.
int maxevt
Definition: Bootstrap.cpp:276
constexpr int Success
Definition: AppReturnCode.h:18
StatusCode run() override
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
SmartIF< IAlgManager > & algManager()
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
SmartIF< IMessageSvc > m_messageSvc
Reference to the message service.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Gaudi::Property< int > m_pluginDebugLevel
Gaudi::Property< std::string > m_jobOptionsPath
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
Definition: Time.cpp:260
Gaudi::Property< bool > m_loopCheck
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
#define GAUDICORESVC_MAJOR_VERSION
T rbegin(T...args)