Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit 1e23bd4

Browse files
authored
Log errors during start-up and fix the default logging config (#122)
1 parent 37a067d commit 1e23bd4

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

changelog.d/122.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Log errors during start-up.

sygnal.yaml.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ log:
9797
handlers: ["stderr"]
9898
level: "INFO"
9999

100+
disable_existing_loggers: false
101+
100102

101103
access:
102104
# Specify whether or not to trust the IP address in the `X-Forwarded-For`

sygnal/sygnal.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import yaml
2929
from opentracing.scope_managers.asyncio import AsyncioScopeManager
3030
from twisted.enterprise.adbapi import ConnectionPool
31-
from twisted.internet import asyncioreactor
31+
from twisted.internet import asyncioreactor, defer
3232
from twisted.internet.defer import ensureDeferred
3333
from twisted.python import log as twisted_log
34+
from twisted.python.failure import Failure
3435

3536
from 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

Comments
 (0)