comparison src/server/server.py @ 1008:1593e00078d2

server: reconnect SERVICE_PROFILE if it has been disconnected: if the profile is disconnected twice in a short time (currently 15 s), Libervia will not try to reconnect it and will log an error message instead.
author Goffi <goffi@goffi.org>
date Fri, 12 Jan 2018 18:03:34 +0100
parents d0b27d1e2d50
children 6b573b4b5bb0
comparison
equal deleted inserted replaced
1007:c1c74d97a691 1008:1593e00078d2
47 import tempfile 47 import tempfile
48 import shutil 48 import shutil
49 import uuid 49 import uuid
50 import urlparse 50 import urlparse
51 import urllib 51 import urllib
52 import time
52 from httplib import HTTPS_PORT 53 from httplib import HTTPS_PORT
53 import libervia 54 import libervia
54 from libervia.server import websockets 55 from libervia.server import websockets
55 from libervia.server.pages import LiberviaPage 56 from libervia.server.pages import LiberviaPage
56 from libervia.server.utils import quote 57 from libervia.server.utils import quote
1146 1147
1147 def __init__(self, sat_host): 1148 def __init__(self, sat_host):
1148 web_resource.Resource.__init__(self) 1149 web_resource.Resource.__init__(self)
1149 self.register = None 1150 self.register = None
1150 self.sat_host = sat_host 1151 self.sat_host = sat_host
1152 self._last_service_prof_disconnect = time.time()
1151 self.signalDeferred = {} # dict of deferred (key: profile, value: Deferred) 1153 self.signalDeferred = {} # dict of deferred (key: profile, value: Deferred)
1152 # which manages the long polling HTTP request with signals 1154 # which manages the long polling HTTP request with signals
1153 self.queue = {} 1155 self.queue = {}
1154 1156
1155 def plugRegister(self, register): 1157 def plugRegister(self, register):
1243 # request = self.sat_host.waiting_profiles.getRequest(profile) 1245 # request = self.sat_host.waiting_profiles.getRequest(profile)
1244 # if request: 1246 # if request:
1245 # self.sat_host._logged(profile, request) 1247 # self.sat_host._logged(profile, request)
1246 1248
1247 def disconnected(self, profile): 1249 def disconnected(self, profile):
1250 if profile == C.SERVICE_PROFILE:
1251 # if service profile has been disconnected, we try to reconnect it
1252 # if we can't we show error message
1253 # and if we have 2 disconnection in a short time, we don't try to reconnect
1254 # and display an error message
1255 disconnect_delta = time.time() - self._last_service_prof_disconnect
1256 if disconnect_delta < 15:
1257 log.error(_(u'Service profile disconnected twice in a short time, please check connection'))
1258 else:
1259 log.info(_(u"Service profile has been disconnected, but we need it! Reconnecting it..."))
1260 self.sat_host.bridge.connect(profile,
1261 self.sat_host.options['passphrase'],
1262 {},
1263 errback=lambda failure_: log.error(_(u"Can't reconnect service profile, please check connection: {reason}").format(reason=failure_))
1264 )
1265 self._last_service_prof_disconnect = time.time()
1266 return
1267
1248 if not profile in self.sat_host.prof_connected: 1268 if not profile in self.sat_host.prof_connected:
1249 log.error("'disconnected' signal received for a not connected profile") 1269 log.info(_(u"'disconnected' signal received for a not connected profile ({profile})").format(profile=profile))
1250 return 1270 return
1251 self.sat_host.prof_connected.remove(profile) 1271 self.sat_host.prof_connected.remove(profile)
1252 if profile in self.signalDeferred: 1272 if profile in self.signalDeferred:
1253 self.signalDeferred[profile].callback(("disconnected",)) 1273 self.signalDeferred[profile].callback(("disconnected",))
1254 del self.signalDeferred[profile] 1274 del self.signalDeferred[profile]