caio.co/de/chuva

Make download-dataset a bit smarter

Take dataset from args. Figures out which PID to terminate through
systemd so I can make multiple instances of the same binary without
worry

Now https://beta.chuva.caio.co is running with the ensemble model
Id
ffc8fd18f44253bfbe2c0de5f3691110223a9bf5
Author
Caio
Commit time
2025-11-27T09:26:06+01:00

Modified download-dataset/README

@@ -1,12 +1,10
This is the example python code:

https://developer.dataplatform.knmi.nl/open-data-api#example-last

-Made to point at the radar-forecast dataset:
-
- https://dataplatform.knmi.nl/dataset/radar-forecast-2-0
-
With a few modifications so I can use it as a cronjob. Namely:
+
+ - Takes dataset name/version from args

- Reads the api key from systemd-creds if available. Falls back
to env API_TOKEN for easier debugging
@@ -14,3 +12,5
- Doesn't download existing files

- Deletes old files
+
+ - Tries to kill -TERM a given service

Modified download-dataset/knmi_precip_nowcast_latest.py

@@ -70,15 +70,41
sys.exit(1)


+def restart_service(svc):
+ logger.info("Restarting %s", svc)
+ res = subprocess.run(
+ f"systemctl show --property MainPID --value {svc}.service",
+ shell=True,
+ capture_output=True,
+ )
+ if res.returncode != 0:
+ logger.error("Unable to find PID for %s: %s", svc, res.stderr)
+ return
+
+ pid = int(res.stdout)
+ # great footgun eh
+ if pid == 0:
+ logger.info("Service %s not running", svc)
+ return
+
+ res = subprocess.run(f"kill -TERM {pid}", shell=True, capture_output=True)
+ if res.returncode != 0:
+ logger.error("Unable to kill PID %d for %s: %s", pid, svc, res.stderr)
+ return
+
+
def main():
- if len(sys.argv) != 2:
- logger.error(f"usage: {sys.argv[0]} <DOWNLOAD_DIR>")
+ if len(sys.argv) != 5:
+ logger.error(
+ f"usage: {sys.argv[0]} <DATASET_NAME> <DATASET_VERSION> <DOWNLOAD_DIR> <SERVICE>"
+ )
sys.exit(1)
- download_dir = sys.argv[1]
+ dataset_name = sys.argv[1]
+ dataset_version = sys.argv[2]
+ download_dir = sys.argv[3]
+ svc = sys.argv[4]

api_key = get_api_key()
- dataset_name = "radar_forecast"
- dataset_version = "2.0"
logger.info(f"Fetching latest file of {dataset_name} version {dataset_version}")

api = OpenDataAPI(api_token=api_key)
@@ -112,13 +138,7
logger.info(f"Deleting old dataset {file}")
os.remove(file)

- logger.info("Killing moros process")
- res = subprocess.run(
- "/bin/sh -c 'kill -TERM $(pidof moros)'", shell=True, capture_output=True
- )
- if res.returncode != 0:
- logger.error("Failed to kill moros: %s", res.stderr)
-
+ restart_service(svc)
logger.info("Done")


Modified etc/systemd/download-dataset.service

@@ -5,6 +5,6

[Service]
Type=oneshot
-ExecStart=python3 /opt/caio.co/bin/knmi_precip_nowcast_latest.py /opt/caio.co/data/knmi/
+ExecStart=python3 /opt/caio.co/bin/knmi_precip_nowcast_latest.py radar_forecast 2.0 /opt/caio.co/data/knmi/ moros
# Get a token here https://developer.dataplatform.knmi.nl/open-data-api#token
SetCredential=api:YOUR_API_TOKEN