mirror of
https://github.com/simple-login/app.git
synced 2024-09-29 21:21:29 +02:00
Improve error handling on event sink (#2117)
* chore: make event_sink return success * fix: add return to ConsoleEventSink
This commit is contained in:
parent
8eccb05e33
commit
aad6f59e96
@ -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
|
||||||
|
@ -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:
|
||||||
|
Loading…
Reference in New Issue
Block a user