Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

#!/usr/bin/env python 

 

""" 

@author Dorian Raymer 

@author Michael Meisinger 

@brief Capability Container main class 

@see http://www.oceanobservatories.org/spaces/display/syseng/CIAD+COI+SV+Python+Capability+Container 

 

A container utilizes the messaging abstractions for AMQP. 

 

""" 

 

import os 

import string 

import sys 

 

from twisted.internet import defer 

from twisted.python import failure 

from zope.interface import implements, Interface 

 

import ion.util.ionlog 

log = ion.util.ionlog.getLogger(__name__) 

 

from ion.core import ioninit 

from ion.core.cc.container_api import IContainer 

from ion.core.id import Id 

from ion.core.intercept.interceptor_system import InterceptorSystem 

from ion.core.messaging.exchange import ExchangeManager 

from ion.core.pack.application import AppLoader 

from ion.core.pack.app_manager import AppManager 

from ion.core.process.proc_manager import ProcessManager 

from ion.util.state_object import BasicLifecycleObject 

from ion.util.config import Config 

from ion.util import procutils as pu 

 

CONF = ioninit.config(__name__) 

CF_is_config = Config(CONF.getValue('interceptor_system')).getObject() 

 

class Container(BasicLifecycleObject): 

    """ 

    Represents an instance of the Capability Container. Typically, in one Twisted 

    process (= one UNIX process), there is only one instance of a CC. In test cases, 

    however, there might be more. 

    """ 

    implements(IContainer) 

 

    # Static variables 

    # Generate unique container id (and process id prefix). Avoid . chars. 

    id = '%s.%d' % (os.uname()[1], os.getpid()) 

    id = string.replace(id, ".", "_") 

 

    args = None  # Startup arguments 

    _started = False 

 

    def __init__(self): 

        BasicLifecycleObject.__init__(self) 

 

        self._fatal_error_encountered = False 

 

        # Config instance 

        self.config = None 

 

        # ExchangeManager instance 

        self.exchange_manager = None 

 

        # ProcessManager instance 

        self.proc_manager = None 

 

        # AppManager instance 

        self.app_manager = None 

 

        # InterceptorSystem 

        self.interceptor_system = None 

 

    @defer.inlineCallbacks 

    def on_initialize(self, config, *args, **kwargs): 

        """ 

        Initializes the instance of a container. Actions include 

        - Receive and parse the configuration 

        - Prepare some active objects 

        """ 

        self.config = config 

 

        # Set additional container args 

        Container.args = self.config.get('args', None) 

 

        self.exchange_manager = ExchangeManager(self) 

        yield self.exchange_manager.initialize(config, *args, **kwargs) 

 

        self.proc_manager = ProcessManager(self) 

        yield self.proc_manager.initialize(config, *args, **kwargs) 

 

        self.app_manager = AppManager(self) 

        yield self.app_manager.initialize(config, *args, **kwargs) 

 

        self.interceptor_system = InterceptorSystem() 

        yield self.interceptor_system.initialize(CF_is_config) 

 

    @defer.inlineCallbacks 

    def on_activate(self, *args, **kwargs): 

        """ 

        Activates the container. Actions include 

        - Initiate broker connection 

        - Start 

        @retval Deferred 

        """ 

        Container._started = True 

 

        yield self.interceptor_system.activate() 

 

        yield self.exchange_manager.activate() 

 

        yield self.proc_manager.activate() 

 

        yield self.app_manager.activate() 

 

 

        ## Lifecycle event publishing disabled for now  

        # now that we've activated, can publish ContainerLifecycleEvents as we need the exchange_manager in place. 

        # this is the first chance we have to construct this publisher though. 

        #p = Process(spawnargs={'proc-name': 'ContainerLCEPubProcess'}) 

        #yield p.spawn() 

 

        #self._lc_pub = ContainerLifecycleEventPublisher(origin=self.id, process=p) 

        #yield self._lc_pub.initialize() 

        #yield self._lc_pub.activate() 

 

        # now publish the event 

        #yield self._lc_pub.create_and_publish_event(state=ContainerLifecycleEventPublisher.State.ACTIVE) 

 

    def on_deactivate(self, *args, **kwargs): 

        raise NotImplementedError("Not implemented") 

 

    @defer.inlineCallbacks 

    def on_terminate(self, *args, **kwargs): 

        """ 

        Deactivates and terminates the container. Actions include 

        - Stop and terminate all container applications 

        - Close broker connection 

        @retval Deferred 

        """ 

 

        # technically this is not correct as we're still not quite TERMINATED, but for all intents and purposes.. 

        # we have to publish before we tear down the messaging framework 

        #yield self._lc_pub.create_and_publish_event(state=self._lc_pub.State.TERMINATED) 

        #yield self._lc_pub.terminate() 

        #yield self._lc_pub._process.terminate() 

 

        if self._fatal_error_encountered: 

            log.info("Container terminating hard due to fatal error!") 

            yield defer.succeed(None) 

            defer.returnValue(None) 

 

        log.info("Terminating app_manager.") 

        yield self.app_manager.terminate() 

        log.info("app_manager Terminated.") 

 

        log.info("Terminating proc_manager.") 

        yield self.proc_manager.terminate() 

        log.info("proc_manager Terminated.") 

 

        log.info("Terminating interceptor_system.") 

        yield self.interceptor_system.terminate() 

        log.info("interceptor_system Terminated.") 

 

        log.info("Terminating exchange_manager.") 

        yield self.exchange_manager.terminate() 

        log.info("exchange_manager Terminated.") 

 

        log.info("Container closed") 

        Container._started = False 

 

    def on_error(self, *args, **kwargs): 

        """An error here is always fatal. 

        """ 

        #raise RuntimeError("Illegal state change for container") 

        self.fatalError() 

 

    # --- Container API ----------- 

 

    # Process management, handled by ProcessManager 

    def spawn_process(self, *args, **kwargs): 

        return self.proc_manager.spawn_process(*args, **kwargs) 

    def spawn_processes(self, *args, **kwargs): 

        return self.proc_manager.spawn_processes(*args, **kwargs) 

    def create_supervisor(self, *args, **kwargs): 

        return self.proc_manager.create_supervisor(*args, **kwargs) 

    def activate_process(self, *args, **kwargs): 

        return self.proc_manager.activate_process(*args, **kwargs) 

    def terminate_process(self, *args, **kwargs): 

        return self.proc_manager.terminate_process(*args, **kwargs) 

 

    # Exchange management, handled by ExchangeManager 

    def configure_messaging(self, *args, **kwargs): 

        return self.exchange_manager.configure_messaging(*args, **kwargs) 

    def new_consumer(self, *args, **kwargs): 

        return self.exchange_manager.new_consumer(*args, **kwargs) 

 

    def name_exists(self, name, scope='system'): 

        scoped_name = pu.get_scoped_name(name, scope) 

        return self.exchange_manager.queue_exists(scoped_name) 

 

    def send(self, *args, **kwargs): 

        return self.exchange_manager.send(*args, **kwargs) 

 

    # App management, handled by AppManager 

    def start_app(self, *args, **kwargs): 

        return self.app_manager.start_app(*args, **kwargs) 

    # Release management, handled by AppManager 

    def start_rel(self, *args, **kwargs): 

        return self.app_manager.start_rel(*args, **kwargs) 

 

    # Container Events 

 

    def fatalError(self, ex=None): 

        """ 

        Container event that componenets/processes can raise when something 

        goes really wrong. 

        The result of the fatalError can cause the whole container to 

        shutdown by calling reactor.stop 

        reactor.stop will call stopService on CapabilityContainer Service 

        which, in turn, will terminate this container lifecycleobject, 

        which then terminates its lifecycle objects. 

        """ 

        log.error('fatalError event') 

        log.error(str(ex)) 

        try: 

            f = failure.Failure() 

            log.info("The container suffered a fatal error event and is crashing.") 

            log.debug(str(f.getTraceback())) 

            f.printDetailedTraceback() 

            log.info("The last traceback, in full detail, was written to stdout and the debug loglevel.") 

        except failure.NoCurrentExceptionError: 

            log.info("No Exception to be logged for fatalError") 

 

        if not self._fatal_error_encountered: 

            ioninit.shutdown_or_die(30) # let's see what tenacious containers think of this little number 

            self._fatal_error_encountered = True 

            from twisted.internet import reactor 

            tp = reactor.getThreadPool() 

            for t in tp.working: 

                t._Thread__stop() 

            reactor.stop() 

 

 

    def exchangeConnectionLost(self, reason): 

        """ 

        The exchange manager notifies the container when the amqp 

        connection closes by triggering this event. 

        The connection closure could be expected (if the exchange manager 

        is terminated) or unexpected, indicating an error situation. 

        """ 

        log.info('exchangeConnectionLost %s' % (str(reason),)) 

        self.fatalError(reason) 

 

    def __str__(self): 

        return "CapabilityContainer(state=%s,%r)" % ( 

            self._get_state(), 

            self.exchange_manager.message_space) 

 

def create_new_container(): 

    """ 

    Factory for a container. 

    This also makes sure that only one container is active at any time, 

    currently. 

    """ 

    if Container._started: 

        raise RuntimeError('Already started') 

 

    c = Container() 

    ioninit.container_instance = c 

 

    return c 

 

Id.default_container_id = Container.id