2828import yaml
2929from opentracing .scope_managers .asyncio import AsyncioScopeManager
3030from twisted .enterprise .adbapi import ConnectionPool
31- from twisted .internet import asyncioreactor
31+ from twisted .internet import asyncioreactor , defer
3232from twisted .internet .defer import ensureDeferred
3333from twisted .python import log as twisted_log
34+ from twisted .python .failure import Failure
3435
3536from sygnal .http import PushGatewayApiServer
3637
@@ -133,10 +134,9 @@ def __init__(self, config, custom_reactor, tracer=opentracing.tracer):
133134 )
134135 raise
135136 else :
136- logger . error (
137+ raise RuntimeError (
137138 "Unknown OpenTracing implementation: %s." , tracecfg ["impl" ]
138139 )
139- sys .exit (1 )
140140
141141 db_name = config ["database" ]["name" ]
142142
@@ -190,14 +190,14 @@ async def _make_pushkins_then_start(self, port, bind_addresses, pushgateway_api)
190190 try :
191191 self .pushkins [app_id ] = await self ._make_pushkin (app_id , app_cfg )
192192 except Exception :
193- logger . exception (
193+ raise RuntimeError (
194194 "Failed to load and create pushkin for kind %s" , app_cfg ["type" ]
195195 )
196- sys .exit (1 )
197196
198197 if len (self .pushkins ) == 0 :
199- logger .error ("No app IDs are configured. Edit sygnal.yaml to define some." )
200- sys .exit (1 )
198+ raise RuntimeError (
199+ "No app IDs are configured. Edit sygnal.yaml to define some."
200+ )
201201
202202 logger .info ("Configured with app IDs: %r" , self .pushkins .keys ())
203203
@@ -213,9 +213,25 @@ def run(self):
213213 bind_addresses = self .config ["http" ]["bind_addresses" ]
214214 pushgateway_api = PushGatewayApiServer (self )
215215
216- ensureDeferred (
217- self ._make_pushkins_then_start (port , bind_addresses , pushgateway_api )
218- )
216+ @defer .inlineCallbacks
217+ def start ():
218+ try :
219+ yield ensureDeferred (
220+ self ._make_pushkins_then_start (
221+ port , bind_addresses , pushgateway_api
222+ )
223+ )
224+ except Exception :
225+ # Print the exception and bail out.
226+ print ("Error during startup:" , file = sys .stderr )
227+
228+ # this gives better tracebacks than traceback.print_exc()
229+ Failure ().printTraceback (file = sys .stderr )
230+
231+ if self .reactor .running :
232+ self .reactor .stop ()
233+
234+ self .reactor .callWhenRunning (start )
219235 self .reactor .run ()
220236
221237
0 commit comments