From 83ddafa4f548ddc5888c8db9d34058c274809222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1igo=20Etxaniz?= Date: Mon, 10 Aug 2026 18:34:34 +0000 Subject: [PATCH] example script --- README.md | 8 ++++++- ryzen7_script.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ schedule.yaml | 11 +++++++++ signals.yml | 7 ++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 ryzen7_script.py create mode 100644 schedule.yaml create mode 100644 signals.yml diff --git a/README.md b/README.md index 07aa6d7..7c0866d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # ryzen7-script -Customization test script for the RYZEN-7 gateway on castro — reads host signals through the sandbox sidecar and runs in an image-builder image. \ No newline at end of file +Customization test script for the RYZEN-7 gateway on castro — reads host signals through the sandbox sidecar and runs in an image-builder image. + +Reads `ryzen7-cpu`, `ryzen7-ram` and `ryzen7-disk` (43, 44, 45) once a second, and writes their maximum to `ryzen7-load-index` (54) as an indicator of how loaded the host is. All three inputs are percentages on the same scale, so the maximum is meaningful without normalisation. + +`ryzen7-load-index` is an i7state `state_signal` with a timeout, so it reads NaN when the job stops writing. A stale value is therefore impossible: if the number is there, the job is alive. When an input is missing or not yet reporting, the script skips the write rather than writing a value derived from partial data, which lets the signal time out honestly. + +Runs in `docker.izazpi.cloud/infra/infra-python:0.1.12`, which carries the `i7lib` wheel. There is no library folder on the host — `i7lib` reaches the script only through the image, so a new `i7lib` version means a new image tag here. diff --git a/ryzen7_script.py b/ryzen7_script.py new file mode 100644 index 0000000..12102d4 --- /dev/null +++ b/ryzen7_script.py @@ -0,0 +1,62 @@ +import math + +from i7lib.client import run +from i7lib.timeutils import now, to_datetime + +INPUTS = ["ryzen7-cpu", "ryzen7-ram", "ryzen7-disk"] +ID_LOAD_INDEX = 54 +NAME_LOAD_INDEX = "ryzen7-load-index" +LOG_EVERY = 60 + +tick = 0 + + +def log(msg): + stamp = to_datetime(now()).strftime("%Y-%m-%d %H:%M:%S") + print(f"[{stamp}] {msg}") + + +def read_inputs(plant): + values = [] + for name in INPUTS: + value = plant.state.get(name) + if value is None or math.isnan(value): + return None + values.append(value) + return values + + +def report_missing(plant): + missing = [name for name in INPUTS if name not in plant.state] + if missing: + log(f"WARNING: not exposed to this job: {', '.join(missing)}") + + +def write_load_index(plant, load_index): + batch = plant.new_write_batch() + batch.add(ID_LOAD_INDEX, load_index, NAME_LOAD_INDEX) + plant.send(batch) + + +def logic(plant): + global tick + + if tick == 0: + report_missing(plant) + tick += 1 + + values = read_inputs(plant) + if values is None: + log("skipping write: an input is missing or not yet reporting") + return + + load_index = max(values) + write_load_index(plant, load_index) + + if tick % LOG_EVERY == 1: + readings = " ".join(f"{n}={v:.2f}" for n, v in zip(INPUTS, values)) + log(f"{readings} -> {NAME_LOAD_INDEX}={load_index:.2f}") + + +if __name__ == "__main__": + run(logic, interval_ms=1000, offset_ms=200) diff --git a/schedule.yaml b/schedule.yaml new file mode 100644 index 0000000..465d6a3 --- /dev/null +++ b/schedule.yaml @@ -0,0 +1,11 @@ +name: ryzen7-script +active: true +schedule: "* * * * *" +action: ensure +runOnStartup: true +imageName: docker.izazpi.cloud/infra/infra-python:0.1.12 +command: ["python", "/app/ryzen7_script.py"] +maxCpuCores: 0.5 +maxMemoryMb: 128 +autoremove: false +logOption: 1 diff --git a/signals.yml b/signals.yml new file mode 100644 index 0000000..5867663 --- /dev/null +++ b/signals.yml @@ -0,0 +1,7 @@ +- { id: 43, name: 'ryzen7-cpu', read: 1, write: 0 } +- { id: 44, name: 'ryzen7-ram', read: 1, write: 0 } +- { id: 45, name: 'ryzen7-disk', read: 1, write: 0 } +- { id: 46, name: 'ryzen7-net-tx', read: 1, write: 0 } +- { id: 47, name: 'ryzen7-net-rx', read: 1, write: 0 } +- { id: 48, name: 'ryzen7-temp', read: 1, write: 0 } +- { id: 54, name: 'ryzen7-load-index', read: 0, write: 1 }