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

View File

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