Skip to content

Commit a6a0879

Browse files
Revamp increment_auto_off.py to be more efficient
1 parent d6170ea commit a6a0879

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

scripts/increment_auto_off.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,28 @@
2020
response = requests.get('http://localhost:5001/settings/persist_logs', timeout=10)
2121
state = response.json()
2222

23-
if state["persist_logs"] and state["auto_off_delay"] > 0:
24-
body: dict
25-
if state["auto_off_delay"] >= 1:
26-
delay = state["auto_off_delay"] - 1
27-
body = {
28-
"persist_logs": True,
29-
"auto_off_delay": delay,
30-
}
23+
future_persist_state = state["auto_off_delay"] != 1
24+
delay = state["auto_off_delay"] - 1
25+
body = {
26+
"persist_logs": future_persist_state,
27+
"auto_off_delay": delay if future_persist_state else 14, # If no longer persisting, set to default
28+
}
29+
json_data = json.dumps(body)
30+
response = requests.post(
31+
url='http://localhost:5001/settings/persist_logs',
32+
headers={'Content-Type': 'application/json'},
33+
data=json_data,
34+
timeout=10,
35+
)
36+
37+
if response.ok:
38+
loop = False
39+
if future_persist_state:
3140
logging.info(f"Persist logs will be automatically turned off in {delay} day(s)")
3241
else:
33-
body = {
34-
"persist_logs": False,
35-
"auto_off_delay": 14, # Reset to default for next time
36-
}
3742
logging.info("Persist logs has been turned off automatically")
38-
39-
json_data = json.dumps(body)
40-
response = requests.post(
41-
url='http://localhost:5001/settings/persist_logs',
42-
headers={'Content-Type': 'application/json'},
43-
data=json_data,
44-
timeout=10,
45-
)
46-
47-
if response.ok:
48-
loop = False
4943
except Exception as exc:
50-
logger.exception(f"Auto off increment has failed with the following exception, trying again in 5 seconds: {exc}")
44+
logger.exception(f"increment_auto_off.py has failed with the following exception, trying again in 5 seconds: {exc}")
5145
finally:
5246
if loop:
5347
time.sleep(5) # Loop until there is a response.ok at the end

0 commit comments

Comments
 (0)