From 5d9e81540729b9622958012c971dbf82b5f40f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 24 Aug 2020 20:58:26 +0200 Subject: [PATCH] create looping timer --- requirements.txt | 2 +- src/app.py | 11 ++++++++ src/utils/loopingtimer.py | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/utils/loopingtimer.py diff --git a/requirements.txt b/requirements.txt index c5c9f3b..5051901 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -sentry_sdk \ No newline at end of file +sentry_sdk diff --git a/src/app.py b/src/app.py index 9bd8fd3..7cc9d87 100644 --- a/src/app.py +++ b/src/app.py @@ -29,3 +29,14 @@ if config.SENTRY_DSN: _experiments={"auto_enabling_integrations": True} ) + +def main(): + """ + Main function + :return: + """ + pass + + +if __name__ == "__main__": + main() diff --git a/src/utils/loopingtimer.py b/src/utils/loopingtimer.py new file mode 100644 index 0000000..fbfa3de --- /dev/null +++ b/src/utils/loopingtimer.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +from threading import Timer + +""" +Timer module that looptydoops the entire software +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "loopingtimer" +__version__text__ = "1" + + +class LoopingTimer: + """ + Looping timer. Executes a function and repeates it in the given interval. + """ + def __init__(self, interval, function, *args, **kwargs): + """ + Initialize class + :param interval: + :param function: + :param args: + :param kwargs: + """ + self._timer = None + self.interval = interval + self.function = function + self.args = args + self.kwargs = kwargs + self.is_running = False + self.start() + + def _run(self) -> None: + """ + Run once + :return: + """ + self.is_running = False + self.start() + self.function(*self.args, **self.kwargs) + + def start(self) -> None: + """ + Start running + :return: + """ + if not self.is_running: + self._timer = Timer(self.interval, self._run) + self._timer.start() + self.is_running = True + + def stop(self) -> None: + """ + Stop running + :return: + """ + self._timer.cancel() + self.is_running = False