pumping.py: start/running/log with diff, pump_diff.sh into data_diff/ (its own lock and log), for the Stations page of aimsight-app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca366c150e
commit
799a53c102
1 file changed
+21
-14
+21
-14
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
- `tracked()`, `save(rows)`: extraction/stations.txt, one station per line, "SERIAL [position]" (position: "lat,lon" or a
|
- `tracked()`, `save(rows)`: extraction/stations.txt, one station per line, "SERIAL [position]" (position: "lat,lon" or a
|
||||||
Swiss place, for the weather, weather/meteoswiss.py; empty: an indoor site, no weather); pump.py reads it too;
|
Swiss place, for the weather, weather/meteoswiss.py; empty: an indoor site, no weather); pump.py reads it too;
|
||||||
- `start(stations, start, end, images)`: pump.sh in the background (its lock: one pumping at a time);
|
- `start(stations, start, end, images, diff)`: pump.sh in the background (its lock: one pumping at a time); with diff,
|
||||||
- `running()`, `log()`: a pumping running (any: the scheduler's, by hand, a caller's), the end of out/pump.log.
|
pump_diff.sh (the differential images into data_diff/, its own lock: it runs beside pump.sh);
|
||||||
|
- `running(diff)`, `log(diff)`: a pumping running (any: the scheduler's, by hand, a caller's), the end of out/pump.log
|
||||||
|
(diff: out/pump_diff.log).
|
||||||
|
|
||||||
A refused request raises ValueError, with its reason. Standard library only.
|
A refused request raises ValueError, with its reason. Standard library only.
|
||||||
"""
|
"""
|
||||||
@@ -41,10 +43,14 @@ def save(rows):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def running():
|
def name(diff):
|
||||||
"""A pumping running: the lock of pump.sh is held."""
|
return "pump_diff" if diff else "pump"
|
||||||
|
|
||||||
|
|
||||||
|
def running(diff=False):
|
||||||
|
"""A pumping running: the lock of pump.sh (diff: pump_diff.sh) is held."""
|
||||||
try:
|
try:
|
||||||
with open(ROOT / "out" / "pump.lock") as lock:
|
with open(ROOT / "out" / f"{name(diff)}.lock") as lock:
|
||||||
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return False
|
return False
|
||||||
@@ -53,27 +59,28 @@ def running():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def log(lines=40):
|
def log(lines=40, diff=False):
|
||||||
"""The last lines of out/pump.log."""
|
"""The last lines of out/pump.log (diff: out/pump_diff.log)."""
|
||||||
try:
|
try:
|
||||||
with open(ROOT / "out" / "pump.log", "rb") as f:
|
with open(ROOT / "out" / f"{name(diff)}.log", "rb") as f:
|
||||||
f.seek(max(0, f.seek(0, os.SEEK_END) - 200 * lines))
|
f.seek(max(0, f.seek(0, os.SEEK_END) - 200 * lines))
|
||||||
return f.read().decode(errors="replace").splitlines()[-lines:]
|
return f.read().decode(errors="replace").splitlines()[-lines:]
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def start(stations, start="", end="", images=False):
|
def start(stations, start="", end="", images=False, diff=False):
|
||||||
"""pump.sh in the background, with --from start --to end (YYYY-MM-DD, end excluded), or without (the missing days of
|
"""pump.sh in the background, with --from start --to end (YYYY-MM-DD, end excluded), or without (the missing days of
|
||||||
each station up to now); --delay 300. -> the process."""
|
each station up to now); --delay 300. With diff, pump_diff.sh (without range: from the last indexed day, a station
|
||||||
|
never pumped is skipped; images ignored, its IRON images one per hour). -> the process."""
|
||||||
if not stations or not all(isinstance(s, str) and SERIAL.match(s) for s in stations):
|
if not stations or not all(isinstance(s, str) and SERIAL.match(s) for s in stations):
|
||||||
raise ValueError("no station, or not a serial number")
|
raise ValueError("no station, or not a serial number")
|
||||||
if bool(start) != bool(end) or (start and not (DAY.match(start) and DAY.match(end) and start < end)):
|
if bool(start) != bool(end) or (start and not (DAY.match(start) and DAY.match(end) and start < end)):
|
||||||
raise ValueError("a range: From and To, YYYY-MM-DD, From before To")
|
raise ValueError("a range: From and To, YYYY-MM-DD, From before To")
|
||||||
if running():
|
if running(diff):
|
||||||
raise ValueError("a pumping is already running")
|
raise ValueError("a pumping is already running")
|
||||||
args = ["--from", start, "--to", end] * bool(start) + ["--images"] * bool(images) + ["--delay", "300", *stations]
|
args = ["--from", start, "--to", end] * bool(start) + ["--images"] * bool(images and not diff) + ["--delay", "300", *stations]
|
||||||
return subprocess.Popen([str(ROOT / "extraction" / "pump.sh"), *args], cwd=ROOT,
|
return subprocess.Popen([str(ROOT / "extraction" / f"{name(diff)}.sh"), *args], cwd=ROOT,
|
||||||
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
@@ -85,7 +92,7 @@ if __name__ == "__main__": # a check, in a temporary folder: python3 extraction
|
|||||||
assert tracked() == {"NORDEND": "", "NIEDERHORN": "46.69,7.78"}, tracked()
|
assert tracked() == {"NORDEND": "", "NIEDERHORN": "46.69,7.78"}, tracked()
|
||||||
assert save([{"serial": "A", "position": " Interlaken BE "}, {"serial": "B"}, {"serial": "A"}]) == {"A": "Interlaken BE", "B": ""}
|
assert save([{"serial": "A", "position": " Interlaken BE "}, {"serial": "B"}, {"serial": "A"}]) == {"A": "Interlaken BE", "B": ""}
|
||||||
assert (ROOT / "extraction" / "stations.txt").read_text() == "A Interlaken BE\nB\n"
|
assert (ROOT / "extraction" / "stations.txt").read_text() == "A Interlaken BE\nB\n"
|
||||||
assert not running() and log() == []
|
assert not running() and log() == [] and not running(diff=True) and log(diff=True) == []
|
||||||
for bad in ((save, [{"serial": "../x"}]), (start, ["A;rm"]), (start, ["A"], "2026-11-10"), (start, ["A"], "2026-11-10", "2026-11-09")):
|
for bad in ((save, [{"serial": "../x"}]), (start, ["A;rm"]), (start, ["A"], "2026-11-10"), (start, ["A"], "2026-11-10", "2026-11-09")):
|
||||||
try:
|
try:
|
||||||
bad[0](*bad[1:])
|
bad[0](*bad[1:])
|
||||||
|
|||||||
Reference in new issue
Block a user