comparison libervia/server/server.py @ 1361:626b7bbb7f90

server: mechanism to exit Libervia with an exit code: the new `SysExit` exception can now be used to quit Libervia with a given shell exit code.
author Goffi <goffi@goffi.org>
date Sat, 14 Nov 2020 22:24:58 +0100
parents 389a83eefe62
children 45ebeea1bacd
comparison
equal deleted inserted replaced
1360:389a83eefe62 1361:626b7bbb7f90
71 # following value are set from twisted.plugins.libervia_server initialise 71 # following value are set from twisted.plugins.libervia_server initialise
72 # (see the comment there) 72 # (see the comment there)
73 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = None 73 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = None
74 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF 74 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF
75 | inotify.IN_MOVED_TO) 75 | inotify.IN_MOVED_TO)
76
77
78 class SysExit(Exception):
79
80 def __init__(self, exit_code, message=""):
81 self.exit_code = exit_code
82 self.message = message
83
84 def __str__(self):
85 return f"System Exit({self.exit_code}): {self.message}"
76 86
77 87
78 class FilesWatcher(object): 88 class FilesWatcher(object):
79 """Class to check files modifications using iNotify""" 89 """Class to check files modifications using iNotify"""
80 _notifier = None 90 _notifier = None
1074 self.vhost_root, [server.GzipEncoderFactory()] 1084 self.vhost_root, [server.GzipEncoderFactory()]
1075 ) 1085 )
1076 self.site = server.Site(wrapped) 1086 self.site = server.Site(wrapped)
1077 self.site.sessionFactory = LiberviaSession 1087 self.site.sessionFactory = LiberviaSession
1078 1088
1079 def initEb(self, failure):
1080 log.error(_("Init error: {msg}").format(msg=failure))
1081 reactor.stop()
1082 return failure
1083
1084 def _bridgeCb(self): 1089 def _bridgeCb(self):
1085 self.bridge.getReady( 1090 self.bridge.getReady(
1086 lambda: self.initialised.callback(None), 1091 lambda: self.initialised.callback(None),
1087 lambda failure: self.initialised.errback(Exception(failure)), 1092 lambda failure: self.initialised.errback(Exception(failure)),
1088 ) 1093 )
1089 self.initialised.addCallback(lambda __: defer.ensureDeferred(self.backendReady())) 1094 self.initialised.addCallback(lambda __: defer.ensureDeferred(self.backendReady()))
1090 self.initialised.addErrback(self.initEb)
1091 1095
1092 def _bridgeEb(self, failure_): 1096 def _bridgeEb(self, failure_):
1093 if isinstance(failure_, BridgeExceptionNoService): 1097 if isinstance(failure_, BridgeExceptionNoService):
1094 print("Can't connect to SàT backend, are you sure it's launched ?") 1098 print("Can't connect to SàT backend, are you sure it's launched ?")
1095 else: 1099 else:
1414 @param callback: callable to call on service stop 1418 @param callback: callable to call on service stop
1415 @param *args: list of arguments of the callback 1419 @param *args: list of arguments of the callback
1416 @param **kwargs: list of keyword arguments of the callback""" 1420 @param **kwargs: list of keyword arguments of the callback"""
1417 self._cleanup.insert(0, (callback, args, kwargs)) 1421 self._cleanup.insert(0, (callback, args, kwargs))
1418 1422
1423 def initEb(self, failure):
1424 from twisted.application import app
1425 if failure.check(SysExit):
1426 if failure.value.message:
1427 log.error(failure.value.message)
1428 app._exitCode = failure.value.exit_code
1429 reactor.stop()
1430 else:
1431 log.error(_("Init error: {msg}").format(msg=failure))
1432 app._exitCode = C.EXIT_INTERNAL_ERROR
1433 reactor.stop()
1434 return failure
1435
1419 def startService(self): 1436 def startService(self):
1420 """Connect the profile for Libervia and start the HTTP(S) server(s)""" 1437 """Connect the profile for Libervia and start the HTTP(S) server(s)"""
1421 self._init() 1438 self._init()
1422 1439
1423 def eb(e): 1440 def eb(e):
1445 ) 1462 )
1446 else: 1463 else:
1447 self._startService() 1464 self._startService()
1448 1465
1449 self.initialised.addCallback(initOk) 1466 self.initialised.addCallback(initOk)
1467 self.initialised.addErrback(self.initEb)
1450 1468
1451 ## URLs ## 1469 ## URLs ##
1452 1470
1453 def putChildSAT(self, path, resource): 1471 def putChildSAT(self, path, resource):
1454 """Add a child to the sat resource""" 1472 """Add a child to the sat resource"""