| The Gaudi Framework v32r1 (f65d50dc)

Service.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/Service.h"
3 #include "GaudiKernel/GaudiException.h"
4 #include "GaudiKernel/Guards.h"
5 #include "GaudiKernel/IAuditorSvc.h"
6 #include "GaudiKernel/IJobOptionsSvc.h"
7 #include "GaudiKernel/IMessageSvc.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/ISvcManager.h"
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/PropertyHolder.h"
12 #include "GaudiKernel/ServiceLocatorHelper.h"
13
14 using std::string;
15
16 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
17 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
18
19 Service::~Service() {
20 if ( m_svcManager ) m_svcManager->removeService( this ).ignore();
21 }
22
23 // IService::sysInitialize
24 StatusCode Service::sysInitialize() {
25 std::call_once( m_initFlag, &Service::sysInitialize_imp, this );
26 return m_initSC;
27 }
28
29 void Service::sysInitialize_imp() {
30
31 try {
32 m_targetState = Gaudi::StateMachine::INITIALIZED;
33 Gaudi::Guards::AuditorGuard guard( this,
34 // check if we want to audit the initialize
35 ( m_auditorInitialize ) ? auditorSvc().get() : nullptr, IAuditor::Initialize );
36
37 m_initSC = setProperties();
38 if ( !m_initSC ) return;
39
40 m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
41 if ( m_initSC.isSuccess() ) m_state = m_targetState;
42 return;
43 } catch ( const GaudiException& Exception ) {
44 fatal() << "in sysInitialize(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
45 error() << Exception << endmsg;
46 // Stat stat( chronoSvc() , Exception.tag() );
47 } catch ( const std::exception& Exception ) {
48 fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
49 error() << Exception.what() << endmsg;
50 // Stat stat( chronoSvc() , "*std::exception*" );
51 } catch ( ... ) {
52 fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
53 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
54 }
55
56 m_initSC = StatusCode::FAILURE;
57 }
58
59 //--- IService::initialize
60 StatusCode Service::initialize() {
61 ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
62 m_state = Gaudi::StateMachine::ChangeState( Gaudi::StateMachine::CONFIGURE, m_state );
63 return StatusCode::SUCCESS;
64 }
65
66 // IService::sysStart
67 StatusCode Service::sysStart() {
68 StatusCode sc;
69
70 try {
71 m_targetState = Gaudi::StateMachine::ChangeState( Gaudi::StateMachine::START, m_state );
72 Gaudi::Guards::AuditorGuard guard( this,
73 // check if we want to audit the initialize
74 ( m_auditorStart ) ? auditorSvc().get() : nullptr, IAuditor::Start );
75 sc = start();
76 if ( sc.isSuccess() ) m_state = m_targetState;
77 return sc;
78 } catch ( const GaudiException& Exception ) {
79 fatal() << "in sysStart(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
80 error() << Exception << endmsg;
81 // Stat stat( chronoSvc() , Exception.tag() );
82 } catch ( const std::exception& Exception ) {
83 fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
84 fatal() << Exception.what() << endmsg;
85 // Stat stat( chronoSvc() , "*std::exception*" );
86 } catch ( ... ) {
87 fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
88 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
89 }
90
91 return StatusCode::FAILURE;
92 }
93
94 // IService::sysStop
95 StatusCode Service::sysStop() {
96 StatusCode sc;
97
98 try {
99 m_targetState = Gaudi::StateMachine::ChangeState( Gaudi::StateMachine::STOP, m_state );
100 Gaudi::Guards::AuditorGuard guard( this,
101 // check if we want to audit the initialize
102 ( m_auditorStop ) ? auditorSvc().get() : nullptr, IAuditor::Stop );
103 sc = stop();
104 if ( sc.isSuccess() ) m_state = m_targetState;
105 return sc;
106 } catch ( const GaudiException& Exception ) {
107 fatal() << "in sysStop(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
108 error() << Exception << endmsg;
109 // Stat stat( chronoSvc() , Exception.tag() );
110 } catch ( const std::exception& Exception ) {
111 fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
112 error() << Exception.what() << endmsg;
113 // Stat stat( chronoSvc() , "*std::exception*" );
114 } catch ( ... ) {
115 fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
116 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
117 }
118
119 return StatusCode::FAILURE;
120 }
121
122 //--- IService::stop
123 StatusCode Service::stop() {
124 // stub implementation
125 return StatusCode::SUCCESS;
126 }
127
128 //--- IService::start
129 StatusCode Service::start() {
130 // stub implementation
131 return StatusCode::SUCCESS;
132 }
133
134 //--- IService::sysFinalize
135 StatusCode Service::sysFinalize() {
136
137 StatusCode sc( StatusCode::FAILURE );
138
139 try {
140 m_targetState = Gaudi::StateMachine::OFFLINE;
141 Gaudi::Guards::AuditorGuard guard( this,
142 // check if we want to audit the initialize
143 ( m_auditorFinalize ) ? auditorSvc().get() : nullptr, IAuditor::Finalize );
144 sc = finalize();
145 if ( sc.isSuccess() ) m_state = m_targetState;
146 } catch ( const GaudiException& Exception ) {
147 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
148 error() << Exception << endmsg;
149 // Stat stat( chronoSvc() , Exception.tag() ) ;
150 } catch ( const std::exception& Exception ) {
151 fatal() << " Standard std::exception is caught " << endmsg;
152 error() << Exception.what() << endmsg;
153 // Stat stat( chronoSvc() , "*std::exception*" ) ;
154 } catch ( ... ) {
155 fatal() << "UNKNOWN Exception is caught " << endmsg;
156 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
157 }
158
159 m_pAuditorSvc = nullptr;
160 return sc;
161 }
162
163 //--- IService::finalize
164 StatusCode Service::finalize() {
165 // m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
166 return StatusCode::SUCCESS;
167 }
168
169 //--- IService::sysReinitialize
170 StatusCode Service::sysReinitialize() {
171
172 StatusCode sc;
173
174 // Check that the current status is the correct one.
175 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
176 error() << "sysReinitialize(): cannot reinitialize service not initialized" << endmsg;
177 return StatusCode::FAILURE;
178 }
179
180 try {
181
182 Gaudi::Guards::AuditorGuard guard( this,
183 // check if we want to audit the initialize
184 ( m_auditorReinitialize ) ? auditorSvc().get() : nullptr,
185 IAuditor::ReInitialize );
186 sc = reinitialize();
187 return sc;
188 } catch ( const GaudiException& Exception ) {
189 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
190 error() << Exception << endmsg;
191 // Stat stat( chronoSvc() , Exception.tag() ) ;
192 } catch ( const std::exception& Exception ) {
193 fatal() << " Standard std::exception is caught " << endmsg;
194 error() << Exception.what() << endmsg;
195 // Stat stat( chronoSvc() , "*std::exception*" ) ;
196 } catch ( ... ) {
197 fatal() << "UNKNOWN Exception is caught " << endmsg;
198 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
199 }
200 return StatusCode::FAILURE;
201 }
202
203 //--- IService::sysRestart
204 StatusCode Service::sysRestart() {
205
206 StatusCode sc;
207
208 // Check that the current status is the correct one.
209 if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
210 error() << "sysRestart(): cannot restart service in state " << FSMState() << " -- must be RUNNING " << endmsg;
211 return StatusCode::FAILURE;
212 }
213
214 try {
215
216 Gaudi::Guards::AuditorGuard guard( this,
217 // check if we want to audit the initialize
218 ( m_auditorRestart ) ? auditorSvc().get() : nullptr, IAuditor::ReStart );
219 sc = restart();
220 return sc;
221 } catch ( const GaudiException& Exception ) {
222 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
223 error() << Exception << endmsg;
224 // Stat stat( chronoSvc() , Exception.tag() ) ;
225 } catch ( const std::exception& Exception ) {
226 fatal() << " Standard std::exception is caught " << endmsg;
227 error() << Exception.what() << endmsg;
228 // Stat stat( chronoSvc() , "*std::exception*" ) ;
229 } catch ( ... ) {
230 fatal() << "UNKNOWN Exception is caught " << endmsg;
231 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
232 }
233 return StatusCode::FAILURE;
234 }
235
236 //--- IService::reinitialize
237 StatusCode Service::reinitialize() {
238 /* @TODO
239 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
240 * is causing too many problems
241 *
242 // Default implementation is finalize+initialize
243 StatusCode sc = finalize();
244 if (sc.isFailure()) {
245 error() << "reinitialize(): cannot be finalized" << endmsg;
246 return sc;
247 }
248 sc = initialize();
249 if (sc.isFailure()) {
250 error() << "reinitialize(): cannot be initialized" << endmsg;
251 return sc;
252 }
253 */
254 return StatusCode::SUCCESS;
255 }
256
257 //--- IService::restart
258 StatusCode Service::restart() {
259 // Default implementation is stop+start
260 StatusCode sc = stop();
261 if ( sc.isFailure() ) {
262 error() << "restart(): cannot be stopped" << endmsg;
263 return sc;
264 }
265 sc = start();
266 if ( sc.isFailure() ) {
267 error() << "restart(): cannot be started" << endmsg;
268 return sc;
269 }
270 return StatusCode::SUCCESS;
271 }
272
273 //--- IService::getServiceName
274 const std::string& Service::name() const { return m_name; }
275
276 //--- Retrieve pointer to service locator
277 SmartIF<ISvcLocator>& Service::serviceLocator() const { return m_svcLocator; }
278
279 // Use the job options service to set declared properties
280 StatusCode Service::setProperties() {
281 const bool CREATEIF( true );
282 auto jos = serviceLocator()->service<IJobOptionsSvc>( "JobOptionsSvc", CREATEIF );
283 if ( !jos ) { throw GaudiException( "Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE ); }
284
285 // initialize messaging (except for MessageSvc)
286 if ( name() != "MessageSvc" ) {
287 // this initializes the messaging, in case property update handlers need to print
288 // and update the property value bypassing the update handler
289 m_outputLevel.value() = setUpMessaging();
290 }
291
292 return jos->setMyProperties( name(), this );
293 }
294
295 //--- Local methods
296 // Standard Constructor
297 Service::Service( std::string name, ISvcLocator* svcloc ) : m_name( std::move( name ) ), m_svcLocator( svcloc ) {
298 if ( m_name != "MessageSvc" ) { // the MessageSvc should not notify itself
299 m_outputLevel.declareUpdateHandler(
300 [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); } );
301 }
302
303 // Initialize the default value from ApplicationMgr AuditAlgorithms
304 Gaudi::Property<bool> audit( false );
305 auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" );
306 if ( appMgr && appMgr->hasProperty( "AuditServices" ) ) { audit.assign( appMgr->getProperty( "AuditServices" ) ); }
307 m_auditInit = audit;
308 m_auditorInitialize = audit;
309 m_auditorStart = audit;
310 m_auditorStop = audit;
311 m_auditorFinalize = audit;
312 m_auditorReinitialize = audit;
313 m_auditorRestart = audit;
314 }
315
316 SmartIF<IAuditorSvc>& Service::auditorSvc() const {
317 if ( !m_pAuditorSvc ) {
318 m_pAuditorSvc = serviceLocator()->service( "AuditorSvc" );
319 if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
320 }
321 return m_pAuditorSvc;
322 }
323
324 void Service::setServiceManager( ISvcManager* ism ) { m_svcManager = ism; }
IAuditor::Start
Definition: IAuditor.h:24
Service::m_outputLevel
Gaudi::Property< int > m_outputLevel
Definition: Service.h:176
Service::m_targetState
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:155
PropertyHolder.h
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:60
Gaudi::StateMachine::INITIALIZED
Definition: StateMachine.h:15
SmartIF< ISvcLocator >
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
Service::m_state
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:153
Service::m_auditorInitialize
Gaudi::Property< bool > m_auditorInitialize
Definition: Service.h:178
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:186
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:164
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
MsgStream.h
Service::sysInitialize_imp
void sysInitialize_imp()
Definition: Service.cpp:29
IAuditor::Stop
Definition: IAuditor.h:24
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:162
Service::start
StatusCode start() override
Definition: Service.cpp:129
ISvcManager
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
IAuditor::ReInitialize
Definition: IAuditor.h:24
IAuditorSvc.h
Service::sysRestart
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:204
GaudiException::tag
virtual const std::string & tag() const
name tag for the exception, or exception type
Definition: GaudiException.h:67
Service::sysStart
StatusCode sysStart() override
Initialize Service.
Definition: Service.cpp:67
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:168
std
STL namespace.
Service::m_auditInit
Gaudi::Property< bool > m_auditInit
Definition: Service.h:177
Service::m_auditorRestart
Gaudi::Property< bool > m_auditorRestart
Definition: Service.h:183
Service::m_name
std::string m_name
Service Name.
Definition: Service.h:166
Service::m_auditorReinitialize
Gaudi::Property< bool > m_auditorReinitialize
Definition: Service.h:182
std::call_once
T call_once(T... args)
ISvcManager.h
IAuditor::ReStart
Definition: IAuditor.h:24
CommonMessaging< implements< IService, IProperty, IStateful > >::setUpMessaging
MSG::Level setUpMessaging() const
Set up local caches.
Definition: CommonMessaging.h:169
Gaudi::StateMachine::CONFIGURE
Definition: StateMachine.h:24
std::string
STL class.
Service.h
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
Service::name
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:98
Service::sysStop
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:95
IJobOptionsSvc
Main interface for the JobOptions service.
Definition: IJobOptionsSvc.h:20
std::exception::what
T what(T... args)
CommonMessaging< implements< IService, IProperty, IStateful > >::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:180
Service::~Service
~Service() override
Standard Destructor.
Definition: Service.cpp:19
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiException.h
IAuditor::Finalize
Definition: IAuditor.h:24
Service::m_auditorFinalize
Gaudi::Property< bool > m_auditorFinalize
Definition: Service.h:181
Guards.h
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:107
IMessageSvc.h
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
Service::reinitialize
StatusCode reinitialize() override
Definition: Service.cpp:237
Service::stop
StatusCode stop() override
Definition: Service.cpp:123
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:163
std::exception
STL class.
Service::sysFinalize
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:135
IAuditor::Initialize
Definition: IAuditor.h:24
Service::setProperties
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:280
Gaudi::StateMachine::STOP
Definition: StateMachine.h:27
ON_DEBUG
#define ON_DEBUG
Definition: Service.cpp:16
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
Gaudi::StateMachine::RUNNING
Definition: StateMachine.h:16
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:332
Service::sysReinitialize
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:170
Gaudi::StateMachine::ChangeState
State GAUDI_API ChangeState(const Transition transition, const State state)
Function to get the new state according to the required transition, checking if the transition is all...
Definition: StateMachine.cpp:9
Service::auditorSvc
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:316
IOTest.appMgr
appMgr
Definition: IOTest.py:92
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:297
Gaudi::StateMachine::START
Definition: StateMachine.h:26
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Service::m_auditorStart
Gaudi::Property< bool > m_auditorStart
Definition: Service.h:179
ServiceLocatorHelper.h
Gaudi::Property::assign
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:659
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:169
Service::setServiceManager
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:324
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
Gaudi::Guards::AuditorGuard
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition: Guards.h:203
Gaudi::StateMachine::OFFLINE
Definition: StateMachine.h:13
Service::restart
StatusCode restart() override
Definition: Service.cpp:258
ISvcManager::removeService
virtual StatusCode removeService(IService *svc)=0
Remove a service from the "active" list of services of the factory.
Service::m_auditorStop
Gaudi::Property< bool > m_auditorStop
Definition: Service.h:180
ISvcLocator.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:92
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Service::sysInitialize
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:24
IJobOptionsSvc.h