Write a portable script
A portable script runs unchanged in the web app, the TUI and (later) as a worker. This guide writes one that stops the workers of a chosen status, with a dry run.
1. Define main
"""Stop the workers with a status (a dry run by default)."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from virtufin.domain_devkit.vf_types import Vf
async def main(params: dict[str, Any], vf: Vf) -> dict[str, Any]:
dry_run = params.get("dryRun", True)
picked = [w for w in await vf.workers.list() if w["status"] == params["status"]]
for w in picked:
print(f"{'would stop' if dry_run else 'stopping'} {w['name']}")
if not dry_run:
await vf.workers.stop({"id": w["id"]})
return {"stopped": 0 if dry_run else len(picked)}
mainisasyncand takes exactlyparamsandvf.- Everything the script does on the platform goes through
vf(Scripts and the library). printis the output, the returned value the result.- Import
Vfonly underTYPE_CHECKING: the hosts do not havevirtufin-domain-devkit.
2. Give it a form
{
"title": "Stop workers by status",
"type": "object",
"required": ["status"],
"properties": {
"status": { "enum": ["RUNNING", "LOADING", "LOAD_FAILED"], "default": "RUNNING" },
"dryRun": { "type": "boolean", "title": "Only list them", "default": true }
}
}
The form follows the script form format. Read each field with a
default (params.get(...)): an empty field is absent.
3. Check it locally
Install the package with its types and run mypy over the script:
uv pip install virtufin-domain-devkit mypy
mypy my_script.py
mypy checks every vf.<group>.<action> and its input against the catalog. To try main, call it
with a fake vf whose methods return made-up data, as domain-devkit's
tests/test_script_library.py does.
4. Don'ts
- No
import os,open(...),subprocess,urllib,requests: the web app's sandbox has no network, files or environment, and a worker host will refuse them. - No credentials in the code:
await vf.secret("NAME"). - No worker version or venue URL written in the code when a lookup exists: the newest version is the
last of
registry.list'sversions, venue protocols come from feed presets.