-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'kafka-rewrite' of https://github.com/neuralbertatech/na…
…tKit into kafka-rewrite
- Loading branch information
Showing
6 changed files
with
91 additions
and
25 deletions.
There are no files selected for viewing
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 @@ | ||
NATKIT_HOSTNAME=localhost # The address that external clients will see the server at |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
natKit/common/src/python/natKit/common/kafka/stream_watcher.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,51 @@ | ||
from natkit.common.kafka import Stream | ||
from natkit.common.kafka import TopicName | ||
from natkit.common.kafka import KafkaManager | ||
|
||
from threading import Thread | ||
|
||
from time import time | ||
from time import sleep | ||
|
||
from typing import NoReturn | ||
from typing import Optional | ||
|
||
|
||
class StreamWatcher(Thread): | ||
def __init__(self, name: TopicName, kafka_manager: KafkaManager): | ||
self.topic_name = name | ||
self._kafka_manager = _kafka_manager | ||
self.has_stream_been_found = False | ||
self._should_continue_search = True | ||
self._polling_rate = 1.0 # 1 second | ||
|
||
self.start() | ||
|
||
def run(self) -> NoReturn: | ||
self._search() | ||
|
||
def get_topic_name(self) -> Optional[TopicName]: | ||
if self.has_stream_been_found: | ||
return self.topic_name | ||
else: | ||
return None | ||
|
||
def stop(self) -> NoReturn: | ||
self._should_continue_search = False | ||
|
||
def _search(self): | ||
while self.should_continue_search: | ||
start_time = time() | ||
|
||
topic_names = self._kafka_manager.query_topic_names() | ||
for name in topic_names: | ||
if name.topic_string == self.topic_name.topic_string: | ||
self.topic_name = name | ||
self.has_stream_been_found = True | ||
self._should_continue_search = False | ||
break | ||
|
||
end_time = time() | ||
time_diff = end_time - start_time | ||
if time_diff < self._polling_rate: | ||
sleep(self._polling_rate - time_diff) |