-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into main
- Loading branch information
Showing
8 changed files
with
186 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
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
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,63 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import signal | ||
import threading | ||
import time | ||
from datetime import datetime | ||
|
||
is_killed_handling: bool = False | ||
is_killed_value: bool = False | ||
is_killed_lock = threading.RLock() | ||
|
||
|
||
def is_killed_reset(): | ||
global is_killed_lock | ||
with is_killed_lock: | ||
global is_killed_value | ||
is_killed_value = False | ||
|
||
|
||
def is_killed() -> bool: | ||
global is_killed_lock | ||
with is_killed_lock: | ||
global is_killed_handling | ||
if not is_killed_handling: | ||
signal.signal(signal.SIGINT, is_killed_handling_func) | ||
signal.signal(signal.SIGTERM, is_killed_handling_func) | ||
is_killed_handling = True | ||
global is_killed_value | ||
return is_killed_value | ||
|
||
|
||
def is_killed_handling_func(*args): | ||
global is_killed_lock | ||
with is_killed_lock: | ||
global is_killed_value | ||
logging.info("Handle kill signal") | ||
is_killed_value = True | ||
|
||
|
||
def sleep_with_check(seconds: float, sleep_step: float = 0.1) -> bool: | ||
start = datetime.now().timestamp() | ||
while datetime.now().timestamp() - start < seconds: | ||
time.sleep(sleep_step) | ||
if is_killed(): | ||
return False | ||
return True | ||
|
||
|
||
def await_all_futures(futures: [], sleep_step: float = 0.1) -> bool: | ||
while not is_killed(): | ||
if not sleep_with_check(1, sleep_step=sleep_step): | ||
return False | ||
has_not_done = False | ||
for f in futures: | ||
if not f.done(): | ||
has_not_done = True | ||
break | ||
if has_not_done: | ||
continue | ||
else: | ||
break | ||
return not is_killed() |
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
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,80 @@ | ||
import concurrent.futures | ||
import threading | ||
from datetime import datetime | ||
from time import sleep | ||
|
||
from gwbackupy.process_helpers import ( | ||
sleep_with_check, | ||
is_killed, | ||
is_killed_reset, | ||
is_killed_handling_func, | ||
await_all_futures, | ||
) | ||
|
||
|
||
def test_sleep_with_check(): | ||
assert not is_killed() | ||
start = datetime.now().timestamp() | ||
sleep_with_check(0.3, sleep_step=0.05) | ||
end = datetime.now().timestamp() | ||
assert end - start < 1 | ||
assert end - start >= 0.3 | ||
assert not is_killed() | ||
|
||
|
||
def do_kill(seconds: float = 0.3): | ||
sleep(seconds) | ||
is_killed_handling_func() | ||
|
||
|
||
def test_sleep_with_check_with_kill(): | ||
assert not is_killed() | ||
try: | ||
_thread = threading.Thread(target=do_kill) | ||
_thread.start() | ||
start = datetime.now().timestamp() | ||
sleep_with_check(10, sleep_step=0.05) | ||
end = datetime.now().timestamp() | ||
assert end - start < 0.5 | ||
assert end - start >= 0.3 | ||
assert is_killed() | ||
finally: | ||
is_killed_reset() | ||
|
||
|
||
def test_is_killed_reset(): | ||
assert not is_killed() | ||
try: | ||
is_killed_handling_func() | ||
assert is_killed() | ||
finally: | ||
is_killed_reset() | ||
assert not is_killed() | ||
|
||
|
||
def test_await_all_futures(): | ||
assert not is_killed() | ||
try: | ||
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1) | ||
futures = [] | ||
start = datetime.now().timestamp() | ||
for i in range(3): | ||
futures.append(executor.submit(lambda x: sleep(0.1))) | ||
assert await_all_futures(futures, sleep_step=0.05) | ||
end = datetime.now().timestamp() | ||
assert end - start >= 0.3 | ||
assert end - start < 3 | ||
|
||
futures.clear() | ||
start = datetime.now().timestamp() | ||
for i in range(3): | ||
futures.append(executor.submit(lambda x: sleep(10))) | ||
_thread = threading.Thread(target=do_kill) | ||
_thread.start() | ||
assert not await_all_futures(futures) | ||
end = datetime.now().timestamp() | ||
assert end - start >= 0.3 | ||
assert end - start < 3 | ||
is_killed_reset() | ||
finally: | ||
is_killed_reset() |