-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from SEKOIA-IO/feat/push_logs_regular_interval
feat: Thread to push logs at regular interval
- Loading branch information
Showing
5 changed files
with
83 additions
and
16 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
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,31 @@ | ||
from threading import Event, Timer | ||
|
||
|
||
class RepeatedTimer: | ||
""" | ||
Execute the given function every `interval` seconds. | ||
""" | ||
|
||
def __init__(self, interval, function): | ||
self._timer = None | ||
self.interval = interval | ||
self.function = function | ||
self.is_running = False | ||
self._stop = Event() | ||
|
||
def _run(self): | ||
self.function() | ||
self.is_running = False | ||
self.start() | ||
|
||
def start(self): | ||
if not self._stop.is_set() and not self.is_running: | ||
self._timer = Timer(self.interval, self._run) | ||
self._timer.start() | ||
self.is_running = True | ||
|
||
def stop(self): | ||
self._stop.set() | ||
if self._timer: | ||
self._timer.cancel() | ||
self.is_running = False |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from time import sleep | ||
|
||
from sekoia_automation.timer import RepeatedTimer | ||
|
||
|
||
def test_timer(): | ||
a = 0 | ||
|
||
def timed(): | ||
nonlocal a | ||
a += 1 | ||
|
||
timer = RepeatedTimer(0.1, timed) | ||
timer.start() | ||
|
||
assert timer.is_running is True | ||
sleep(0.15) | ||
assert a == 1 | ||
|
||
# Check stop | ||
timer.stop() | ||
assert timer.is_running is False |
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