Skip to content

ScriptRunner

Runs one deployed shared portable script (async def main(params, vf), openspec script-workers): on a schedule, on events, on demand. There is one worker type for every deployed script: a deployment is a ScriptRunner with its own config, secrets, topics and group, never code of its own. The runner loads the script document by reference and runs it only while the stored document matches the approved hash.

Property Value
Package virtufin-worker-scriptrunner (wheel, org PyPI feed)
Engine (MIME) application/x-python (a child python3 process)
Base class CommandWorker from virtufin-worker-devkit
Depends on virtufin-domain-devkit[ops] (the shared vf, script_hash, the worker document), virtufin-scriptrunner-devkit (its messages and run topic), virtufin-worker-registry (the worker registry)
Command topic wrk.scriptrunner.command.<worker_id> (commands, and the schedule's trigger)
Run topic wrk.scriptrunner.run.<worker_id> (com.virtufin.scriptrunner.run, RunEvent)
Answers com.virtufin.scriptrunner.response (returned by InvokeWorkerCommand)
Errors com.virtufin.scriptrunner.error (WorkerError)
Status ready, approval-needed, stuck, no-script, unreachable (worker lifecycle status)

Config

Worker config (CreateWorkerRequest.config), written by the deploy. Config and secrets are immutable: a changed approval is a new runner (a redeploy).

Key Required Value
script yes The script's NAME. The runner reads cfg.script.<NAME>, else cfg.script.hidden.<NAME>.
scripthash yes The approved script_hash (domain-devkit): SHA-256 of the canonical JSON of the document's language, code, form and bound.
writes no The approved write operations, a JSON array (["scenarios.halt"], reflected ["workmanager.StopWorker"]). Default []: no writes.
secretmap no Script secret name to the runner's secret attribute, a JSON object ({"BINANCE_KEY": "binancekey"}).
deployer yes The deployer's change-log user key: every write goes to cfg.changelog.<deployer>.
deployername no The deployer as people read it: the by of scheduled and event runs.
overlap yes, no default skip or queue: what happens to a start while a run is going.
timeoutsec no A run's timeout in seconds. Default SCRIPT_RUN_TIMEOUT_SECONDS (600, the clients' stop); at most SCRIPT_RUN_TIMEOUT_MAX_SECONDS (3600).
approvedat no When the deploy was approved (RFC 3339).

A missing required key, an unknown overlap or a timeout out of range fails the start, naming the key. Typed platform fields, never config: topics (the command topic, plus the input topics of an event-driven deployment) and input_replay (required for an event-driven deployment, no default), groups (the deployment's group).

Secrets

Attribute Reference What
vfapikey scripts-api-key The named API key scripts: the runner's only credential at the gateway. Required.
one per mapped secret a key of virtufin-worker-secrets What vf.secret(name) answers for the names in secretmap.

Start reads the gateway's address from the Runtime's environment, then removes every VIRTUFIN_API_* setting from the process before any script runs: a script never finds the Runtime's key. A rotated scripts-api-key is picked up at the next run.

How runs start

Start How params vf.event
schedule a trigger (owner_worker_id the runner, target_topic its command topic) fires com.virtufin.api.trigger.fired script_params(form, bound, {}) None
event an event on an input topic as for schedule the event, as the Python engine hands it, without the secret attributes
on demand the run command script_params(form, bound, values) None

Process only accepts a start and returns within milliseconds. The run executes on the runner's own asyncio loop, so the engine's 30 s message timeout never bounds a run. A schedule's or an event's run id is derived from its input, so a redelivered input is the same run; an on-demand run's id is the client's.

One run at a time per deployment. A start while one runs:

  • skip: not run; the running run counts it in skipped; a run command is answered busy;
  • queue: waits, in order, at most SCRIPT_RUN_QUEUE_LIMIT (100); beyond that skipped and counted.

Commands

Protobuf, virtufin-scriptrunner-devkit / @virtufin/scriptrunner-devkit; the runner registers every schema when it starts.

Command Input Answer
run RunCommand {run_id, values_json, by} RunAnswer {run_id, acceptance, detail}, at once: ACCEPTED (runs now or waits), BUSY (another run with skip, a full queue, or stuck), APPROVAL_NEEDED (the last check found the script changed). A known run_id starts nothing and is answered ACCEPTED, already accepted.
cancel CancelCommand {run_id} CancelAnswer {run_id, canceled}: a running run ends canceled; a waiting one is recorded canceled.
status StatusCommand {} StatusAnswer: status and detail, script, hidden, approved and current hash, writes, overlap, timeout, the running run, the queue.

A run

  1. The runner reads the script document again and compares script_hash with scripthash. A changed script is not run: the run is recorded refused with error approval-needed, and the status is approval-needed until a redeploy. Only language, code, form and bound count: a new title or description needs no approval.
  2. It records the run running in the worker document and publishes started.
  3. It executes the code in a fresh module and calls main(params, vf). vf is domain-devkit's shared implementation (virtufin.domain_devkit.ops) over the gateway with the scripts key:
  4. a read passes;
  5. a write passes only when its operation is in writes; otherwise vf.OperationFailed (outcome refused) and nothing is sent;
  6. a people-only operation (killSwitch) is always refused;
  7. nothing asks anyone: the deploy was the approval;
  8. vf.env(name) is the platform variable (cfg.env), vf.secret(name) the mapped secret, else None (never a person's).
  9. What the script prints is published as output (at most every second) and every write (or refused write) as write; each write goes into the deployer's change log with script and run.
  10. At its end the runner records the run and publishes finished.
Outcome When
done main returned (its value is result)
failed main raised, the script could not be read (e.g. the key was revoked), or defines no async def main
refused the script changed since the approval (approval-needed) or is missing (no-script)
timed-out it ran past timeoutsec and was canceled
canceled the cancel command
interrupted the worker stopped, moved or lost its lease; queued starts are lost and counted in its skipped

Restart after a reload or move. A run interrupted by a reload or a move (a library release, a code reload, a pod's shutdown or crash, a lost lease) is started again once when the runner is back, with the same run id, reason, by and parameters (and the event of an event run); its record is replaced by the restarted run. A second interruption leaves it interrupted, and so do a deliberate stop and start and an undeploy. The runner keeps what a restart needs in its own state (ctx.state, kept across reloads and moves) while the run goes on, and tells the platform's start from a deliberate one by the WorkManager's catalog (GetWorker: a reloaded or moved worker still reads running; StartWorker marks a worker running only after it started). Without an answer it does not restart. Queued starts are not restarted.

A run that does not end within STUCK_GRACE_SECONDS (10 s) of its cancel (it swallows the cancel, or blocks in synchronous code) leaves the runner stuck: no run starts until the worker is restarted. Apart from the one restart after a reload or move, a run is never started again on its own.

No secret value of the runner reaches the run topic or the history: in the output, the result, an error or a write it reads [secret].

Run history

cfg.script.worker.<NAME> (cfg.script.hidden.worker.<NAME> when the script was found hidden), domain-devkit scripts/script-worker.v1.schema.json: the runner writes runs by compare-and-swap, newest first, the newest SCRIPT_RUNS_KEPT (50). Output and result are each capped at SCRIPT_RUN_OUTPUT_LIMIT_BYTES (16 KiB), then truncated. The deploy writes deployment; the runner never changes it. The document outlives the runner.

Gaps

Until access-control:

  • the scripts key grants every call: the runner's gate (approved writes only) is the only limit on what a deployed script does;
  • a deployed script runs without a sandbox in the Runtime's pod and is trusted like all worker code (it could read the Runtime's key from /proc or open the network): the approval, the gate and the environment scrub hold an honest script to its approval, they do not contain a hostile one. That is why only admins deploy;
  • a hidden script's runner, group and run topic (with its output) are visible to every API-key holder.

The worker registry

Since 0.1.2 the runner's host answers registry.list, workers.create, workers.upgrade and feeds.connect from the package virtufin-worker-registry (worker-registry's workers.toml), so the library scripts that deploy or upgrade workers run in a deployed script too. The dependency has no upper bound: every load of the runner resolves the newest registry, and a registry release reloads the runner (a run going then ends interrupted). The WorkManager fetches the workers' code with its own feed credential; the runner holds none.

In the clients

In the web app, admins deploy, redeploy and undeploy a shared script and run it on platform; everyone sees its runner's state, deployment and runs on the script's Runs tab (Deploy a script as a worker and run it on platform, reference Script workers). Each write a run makes is on the app's Changes view, marked by script <NAME> with a link to the run. The TUI runs a deployed script and follows its runs (Run a deployed script on platform).

The web app's Deploy dialog: what the deploy writes into the config above

Screenshot from the web app's docs (app.docs.virtufin.com, made-up data). The dialog's Code is scripthash, Writes writes, Overlap overlap, Timeout timeoutsec; Starts become the trigger and the input topics. A script that reads secrets also shows Secrets, which become secretmap.

Deploy one by hand: Deploy a script as a worker.

Source and changelog: ScriptRunner/, messages and fragment: ScriptRunnerDevKit/.