Skip to content

Commit ac4d11c

Browse files
authored
1060 buffer reset (#1061)
Implemented buffer reset contents capability
1 parent 33ce568 commit ac4d11c

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

adapters/python/bridge/buffer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import shutil
34
import signal
45
import time
56
from typing import Dict, Optional, Tuple, Union
@@ -97,6 +98,9 @@ def __init__(self):
9798
self.message_dump = MessageDumpConfig()
9899
self.idle_polling_period = opt_config('IDLE_POLLING_PERIOD', 0.005, float)
99100
self.stats_log_interval = opt_config('STATS_LOG_INTERVAL', 60, int)
101+
self.buffer_reset_on_restart = opt_config(
102+
'BUFFER_RESET_ON_RESTART', False, strtobool
103+
)
100104
self.metrics = MetricsConfig()
101105

102106

@@ -572,6 +576,18 @@ def main():
572576
# To gracefully shut down the adapter on SIGTERM (raise KeyboardInterrupt)
573577
signal.signal(signal.SIGTERM, signal.getsignal(signal.SIGINT))
574578
config = Config()
579+
if config.buffer_reset_on_restart and os.path.exists(config.buffer.path):
580+
logger.info(
581+
'Resetting the buffer contents. Removing %s contents.', config.buffer.path
582+
)
583+
# iterate over the directory and remove all files
584+
for file in os.listdir(config.buffer.path):
585+
# if a regular file, remove it
586+
if os.path.isfile(os.path.join(config.buffer.path, file)):
587+
os.remove(os.path.join(config.buffer.path, file))
588+
# if a directory, remove it
589+
elif os.path.isdir(os.path.join(config.buffer.path, file)):
590+
shutil.rmtree(os.path.join(config.buffer.path, file))
575591
queue = PersistentQueueWithCapacity(
576592
config.buffer.path,
577593
config.buffer.len + config.buffer.service_messages,

docs/source/savant_101/10_adapters.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,10 @@ The Buffer Bridge Adapter buffers messages from a source and sends them to a mod
17971797
- ``MESSAGE_DUMP_PATH``: a directory to dump message segment files; default is ``/tmp/buffer-adapter-dump``;
17981798
- ``MESSAGE_DUMP_SEGMENT_DURATION``: a duration of a message segment in seconds; default is ``60``.
17991799
- ``MESSAGE_DUMP_SEGMENT_TEMPLATE``: a template for message segment file names; default is ``dump-%Y-%m-%d-%H-%M-%S.msgpack``.
1800+
- ``BUFFER_RESET_ON_RESTART``: a flag indicating whether to reset the buffer contents on restart; default is ``False``.
1801+
1802+
.. note::
1803+
When the buffer is reset on restart, the adapter will remove the buffer directory and create a new one.
18001804

18011805
Running the adapter with Docker:
18021806

samples/buffer_adapter/docker-compose.l4t.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ services:
4141
- MESSAGE_DUMP_ENABLED=False # Set to True to enable message dumping for later analysis
4242
- MESSAGE_DUMP_SEGMENT_DURATION=30
4343
- MESSAGE_DUMP_PATH=/tmp/message-dump
44+
- BUFFER_RESET_ON_RESTART=True
4445
command: python -m adapters.python.bridge.buffer
4546

4647
module:

scripts/run_bridge.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def build_common_bridge_envs(
4646
help='Maximum amount of messages in the buffer.',
4747
show_default=True,
4848
)
49+
@click.option(
50+
'--buffer-reset-on-restart',
51+
default=False,
52+
is_flag=True,
53+
help='Reset the buffer contents on restart.',
54+
)
4955
@click.option(
5056
'--mount-buffer-path',
5157
default=False,
@@ -126,6 +132,7 @@ def buffer_bridge(
126132
metrics_extra_labels: str,
127133
docker_image: str,
128134
buffer_path: str,
135+
buffer_reset_on_restart: bool,
129136
):
130137
"""Buffers messages from a source to BUFFER_PATH and sends them to a module.
131138
@@ -152,6 +159,7 @@ def buffer_bridge(
152159
f'METRICS_FRAME_PERIOD={metrics_frame_period}',
153160
f'METRICS_HISTORY={metrics_history}',
154161
f'METRICS_EXTRA_LABELS={metrics_extra_labels}',
162+
f'BUFFER_RESET_ON_RESTART={buffer_reset_on_restart}',
155163
]
156164
if metrics_time_period:
157165
envs.append(f'METRICS_TIME_PERIOD={metrics_time_period}')

0 commit comments

Comments
 (0)