Skip to content

Commit e012b51

Browse files
committed
fix: increase default poll_time to 5s, sleep such that we get even intervals
1 parent 2859a09 commit e012b51

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/aw_watcher_input/main.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from time import sleep
2-
from datetime import datetime, timezone
31
import logging
4-
5-
import click
2+
from datetime import datetime, timezone
3+
from time import sleep
64

75
import aw_client
6+
import click
87
from aw_core import Event
98
from aw_watcher_afk.listeners import KeyboardListener, MouseListener
109

@@ -23,7 +22,7 @@ def main(testing: bool):
2322
bucket_name = "{}_{}".format(client.client_name, client.client_hostname)
2423
eventtype = "os.hid.input"
2524
client.create_bucket(bucket_name, eventtype, queued=False)
26-
poll_time = 1
25+
poll_time = 5
2726

2827
keyboard = KeyboardListener()
2928
keyboard.start()
@@ -34,7 +33,13 @@ def main(testing: bool):
3433

3534
while True:
3635
last_run = now
37-
sleep(poll_time)
36+
37+
# we want to ensure that the polling happens with a predictable cadence
38+
time_to_sleep = poll_time - datetime.now().timestamp() % poll_time
39+
# ensure that the sleep time is between 0 and poll_time (if system time is changed, this might be negative)
40+
time_to_sleep = max(min(time_to_sleep, poll_time), 0)
41+
sleep(time_to_sleep)
42+
3843
now = datetime.now(tz=timezone.utc)
3944

4045
# If input: Send a heartbeat with data, ensure the span is correctly set, and don't use pulsetime.

0 commit comments

Comments
 (0)