Skip to content

Commit

Permalink
feat: add scheduler for batching
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Nov 7, 2023
1 parent 5c99f39 commit 24e8d18
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions event_routing_backends/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"""
import json
import logging
import sched
import time
import sys

from django.dispatch import receiver
from eventtracking.backends.event_bus import EventBusRoutingBackend
Expand All @@ -12,6 +15,11 @@

logger = logging.getLogger(__name__)

scheduler = sched.scheduler(time.time, time.sleep)


def send_batch():
print("Sending events in batch")

Check warning on line 22 in event_routing_backends/handlers.py

View check run for this annotation

Codecov / codecov/patch

event_routing_backends/handlers.py#L22

Added line #L22 was not covered by tests

@receiver(TRACKING_LOG_EVENT_EMITTED)
def send_tracking_log_to_backends(
Expand Down Expand Up @@ -55,3 +63,15 @@ def send_tracking_log_to_backends(
engine.send_to_backends(processed_event.copy())
except EventEmissionExit:
logger.info("[EventEmissionExit] skipping event {}".format(event["name"]))



def repeat_task():
print("Repeat task")
scheduler.enter(5, 1, send_batch)
scheduler.enter(5, 1, repeat_task, ())

Check warning on line 72 in event_routing_backends/handlers.py

View check run for this annotation

Codecov / codecov/patch

event_routing_backends/handlers.py#L70-L72

Added lines #L70 - L72 were not covered by tests


if "consume_events" in sys.argv:
repeat_task()
scheduler.run()

Check warning on line 77 in event_routing_backends/handlers.py

View check run for this annotation

Codecov / codecov/patch

event_routing_backends/handlers.py#L76-L77

Added lines #L76 - L77 were not covered by tests

0 comments on commit 24e8d18

Please sign in to comment.