Skip to content

Deploy a script as a worker by hand

The web app deploys a shared script for you (an admin's Deploy dialog). This guide does the same by hand with CreateWorker, for a backend without the app or to see the parts. You need an admin's key for the context, the platform secret scripts-api-key in the worker Secret (the named API key scripts), and a shared Python script, e.g. cfg.script.HEALTH_CHECK.

What the runner reads and does: ScriptRunner.

In the clients

The usual way is the web app: Deploy a script as a worker and run it on platform (admins). The TUI runs a deployed script and follows its runs: Run a deployed script on platform. The steps below are what the app's Deploy dialog does for you.

The web app's Deploy dialog: the code's hash, the writes, the starts and the overlap

Screenshot from the web app's docs (app.docs.virtufin.com, made-up data).

1. Approve the code

Read the stored document and compute its hash and its calls with domain-devkit:

import asyncio, json
from virtufin.api import ApiClient
from virtufin.domain_devkit.script_worker import script_calls, script_hash

async def plan(name):
    async with ApiClient("api.virtufin.com", 443, tls=True, api_key="<admin key>") as api:
        doc = json.loads((await api.get_state_async("statestore", f"cfg.script.{name}"))["value"])
    print("hash", script_hash(doc))
    print(script_calls(doc["code"]))   # operations, secrets, dynamic

asyncio.run(plan("HEALTH_CHECK"))

Keep the operations that write (the catalog's kind, or ListMethods for a reflected one). Refuse to deploy a script that calls killSwitch: the runner refuses people-only operations. Lock the script (locked: true) so only admins can change it.

2. Create the runner

virtufin-worker create ScriptRunner --context prod \
  --worker-config script=HEALTH_CHECK \
  --worker-config scripthash=<hash> \
  --worker-config 'writes=["scenarios.halt"]' \
  --worker-config deployer=<your change-log user key> \
  --worker-config 'deployername=Jane Doe' \
  --worker-config overlap=skip \
  --worker-config timeoutsec=600 \
  --secret vfapikey=scripts-api-key \
  --tag name=script-health_check --tag managed-by=script:HEALTH_CHECK

For a script that reads secrets, add --worker-config 'secretmap={"BINANCE_KEY": "binancekey"}' and --secret binancekey=<platform secret key>. For an event-driven deployment add each input topic with --topic (keep wrk.scriptrunner.command.{worker_id} too) and choose --input-replay on or off.

The worker reports ready when the stored script matches the hash, approval-needed when it does not.

3. Start runs

On a schedule, a trigger owned by the runner on its command topic:

virtufin-worker trigger schedule health-check --context prod \
  --target-topic wrk.scriptrunner.command.<worker id> \
  --schedule '@every 5m' --owner-worker-id <worker id>

On demand, the run command (InvokeWorkerCommand, the input RunCommand from virtufin-scriptrunner-devkit):

from virtufin.scriptrunner import scriptrunner_pb2 as pb
from virtufin.workmanager import workmanager_pb2

request = workmanager_pb2.InvokeWorkerCommandRequest(
    id="<worker id>", command="run",
    content_type="application/x-virtufin-scriptrunner-run-command+protobuf",
    payload=pb.RunCommand(run_id="<a new uuid>", values_json="{}", by="Jane Doe").SerializeToString())
raw = await api.gateway.invoke_raw_async("workmanager", "InvokeWorkerCommand", request.SerializeToString())

4. Follow the runs

Subscribe to wrk.scriptrunner.run.<worker id> (RunEvent), or read the history in cfg.script.worker.HEALTH_CHECK. Every write is in your change log (cfg.changelog.<your user key>) with script and run. The web app's Runs tab and the TUI's runs view (v on the script) show the same history:

The web app's Runs tab of a deployed script with an on-demand run

Screenshot from the web app's docs (app.docs.virtufin.com, made-up data).

Undeploy

Delete the runner (virtufin-worker delete --context prod <worker id>): its owned trigger goes with it. The history document stays.