-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add management command to consume event bus every N seconds
- Loading branch information
Showing
3 changed files
with
75 additions
and
19 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
event_routing_backends/management/commands/send_to_backends.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Management command for transforming tracking log files. | ||
""" | ||
|
||
import logging | ||
import time | ||
|
||
from django.core.management.base import BaseCommand | ||
from django_redis import get_redis_connection | ||
|
||
from event_routing_backends.handlers import TRANSFORMED_EVENT_KEY_NAME, send_batch | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Transform tracking logs to an LRS or other output destination. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
""" | ||
Add the necessary arguments to the command. | ||
""" | ||
parser.add_argument( | ||
"--wait_time", | ||
type=int, | ||
help="The number of seconds to wait between each batch send to the destination.", | ||
required=True, | ||
default=5, | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Configure the command and start the transform process. | ||
""" | ||
redis = get_redis_connection() | ||
while True: | ||
queue_size = redis.llen(TRANSFORMED_EVENT_KEY_NAME) | ||
batch = redis.rpop(TRANSFORMED_EVENT_KEY_NAME, queue_size) | ||
if batch: | ||
send_batch(batch) | ||
logger.info("Successfully sent batch to the destination.") | ||
logger.info("Sleeping for %s seconds.", options["wait_time"]) | ||
time.sleep(options["wait_time"]) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters