eve-watcher/eve_autoupdate.py

77 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Auto-update poller — this is what makes 'push' updates work.
Runs headless alongside the other watchers (launched by start-all.ps1). Every few
minutes it asks the public repo for the latest commit; when it changes (i.e. new
watcher code was published from Rocky), it pulls the update and restarts the
watchers automatically — within ~3 minutes, no logon/daily wait. Publishing new
code IS the push.
It updates the data watchers but not itself; its own code refreshes on the next
logon/daily update.py run (which restarts everything cleanly).
"""
import glob
import json
import os
import sys
import time
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import update as u # update_code(), restart_mining_watchers()
BRANCH_API = ("https://git.armoredarmadillo.com/api/v1/repos/"
"brockdarnold/eve-watcher/branches/main")
MARKER = os.path.join(HERE, ".update_sha")
POLL = 180 # seconds between checks (~3 min)
def latest_sha():
req = urllib.request.Request(BRANCH_API, headers={"User-Agent": "eve-watcher-updater"})
return json.load(urllib.request.urlopen(req, timeout=20))["commit"]["id"]
def read_marker():
try:
return open(MARKER).read().strip()
except Exception:
return ""
def write_marker(s):
try:
with open(MARKER, "w") as f:
f.write(s)
except Exception:
pass
def main():
print("[autoupdate] poller started")
if not read_marker(): # seed so we don't update on the very first tick
try:
write_marker(latest_sha())
except Exception:
pass
while True:
time.sleep(POLL)
try:
sha = latest_sha()
except Exception as e:
print(f"[autoupdate] sha check failed: {e}")
continue
if not sha or sha == read_marker():
continue
print(f"[autoupdate] new version {sha[:7]} published — updating...")
try:
u.update_code() # git or zip
u.restart_mining_watchers() # onto the new code
write_marker(sha)
print("[autoupdate] updated + restarted watchers.")
except Exception as e:
print(f"[autoupdate] update failed: {e}")
if __name__ == "__main__":
main()