Improve error handling on event sink (#2117)

* chore: make event_sink return success

* fix: add return to ConsoleEventSink
This commit is contained in:
Carlos Quintana 2024-05-23 15:05:47 +02:00 committed by GitHub
parent 8eccb05e33
commit aad6f59e96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 18 deletions

View File

@ -8,15 +8,16 @@ from app.models import SyncEvent
class EventSink(ABC):
@abstractmethod
def process(self, event: SyncEvent):
def process(self, event: SyncEvent) -> bool:
pass
class HttpEventSink(EventSink):
def process(self, event: SyncEvent):
def process(self, event: SyncEvent) -> bool:
if not EVENT_WEBHOOK:
LOG.warning("Skipping sending event because there is no webhook configured")
return
return False
LOG.info(f"Sending event {event.id} to {EVENT_WEBHOOK}")
res = requests.post(
@ -29,10 +30,13 @@ class HttpEventSink(EventSink):
LOG.warning(
f"Failed to send event to webhook: {res.status_code} {res.text}"
)
return False
else:
LOG.info(f"Event {event.id} sent successfully to webhook")
return True
class ConsoleEventSink(EventSink):
def process(self, event: SyncEvent):
def process(self, event: SyncEvent) -> bool:
LOG.info(f"Handling event {event.id}")
return True

View File

@ -22,22 +22,25 @@ class Runner:
if can_process:
event_created_at = event.created_at
start_time = arrow.now()
self.__sink.process(event)
event_id = event.id
SyncEvent.delete(event.id, commit=True)
LOG.info(f"Marked {event_id} as done")
success = self.__sink.process(event)
if success:
event_id = event.id
SyncEvent.delete(event.id, commit=True)
LOG.info(f"Marked {event_id} as done")
end_time = arrow.now() - start_time
time_between_taken_and_created = start_time - event_created_at
end_time = arrow.now() - start_time
time_between_taken_and_created = start_time - event_created_at
newrelic.agent.record_custom_metric("Custom/sync_event_processed", 1)
newrelic.agent.record_custom_metric(
"Custom/sync_event_process_time", end_time.total_seconds()
)
newrelic.agent.record_custom_metric(
"Custom/sync_event_elapsed_time",
time_between_taken_and_created.total_seconds(),
)
newrelic.agent.record_custom_metric(
"Custom/sync_event_processed", 1
)
newrelic.agent.record_custom_metric(
"Custom/sync_event_process_time", end_time.total_seconds()
)
newrelic.agent.record_custom_metric(
"Custom/sync_event_elapsed_time",
time_between_taken_and_created.total_seconds(),
)
else:
LOG.info(f"{event.id} was handled by another runner")
except Exception as e: