Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Log looping call exceptions #4008

Merged
merged 4 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4008.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log exceptions in looping calls
17 changes: 5 additions & 12 deletions synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
event_processing_loop_room_count,
)
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.util import log_failure
from synapse.util.logcontext import make_deferred_yieldable, run_in_background
from synapse.util.metrics import Measure

Expand All @@ -36,17 +37,6 @@
events_processed_counter = Counter("synapse_handlers_appservice_events_processed", "")


def log_failure(failure):
logger.error(
"Application Services Failure",
exc_info=(
failure.type,
failure.value,
failure.getTracebackObject()
)
)


class ApplicationServicesHandler(object):

def __init__(self, hs):
Expand Down Expand Up @@ -112,7 +102,10 @@ def handle_event(event):

if not self.started_scheduler:
def start_scheduler():
return self.scheduler.start().addErrback(log_failure)
return self.scheduler.start().addErrback(
log_failure, "Application Services Failure",
)

run_as_background_process("as_scheduler", start_scheduler)
self.started_scheduler = True

Expand Down
31 changes: 30 additions & 1 deletion synapse/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def looping_call(self, f, msec):
"""
call = task.LoopingCall(f)
call.clock = self._reactor
call.start(msec / 1000.0, now=False)
d = call.start(msec / 1000.0, now=False)
d.addErrback(
log_failure, "Looping call died", consumeErrors=False,
)
return call

def call_later(self, delay, callback, *args, **kwargs):
Expand Down Expand Up @@ -109,3 +112,29 @@ def batch_iter(iterable, size):
sourceiter = iter(iterable)
# call islice until it returns an empty tuple
return iter(lambda: tuple(islice(sourceiter, size)), ())


def log_failure(failure, msg, consumeErrors=True):
"""Creates a function suitable for passing to `Deferred.addErrback` that
logs any failures that occur.

Args:
msg (str): Message to log
consumeErrors (bool): If true consumes the failure, otherwise passes
on down the callback chain

Returns:
func(Failure)
"""

logger.error(
msg,
exc_info=(
failure.type,
failure.value,
failure.getTracebackObject()
)
)

if not consumeErrors:
return failure