Drive a host from a script

tinyactors run serves one host over JSON-RPC 2.0 on standard input and output. Your script spawns orders, answers their payment requests and moves a simulated clock.

Start a host on your charts

Start one host with --io-processor payments so tinyactors run routes external payment requests back to your script. Closing standard input stops the host.

shell
tinyactors run --io-processor payments --system shop order.scxml < requests.jsonl

This order chart asks payments for a charge, retries after 30 seconds of silence and gives up after three attempts.

order.scxml
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" name="order" datamodel="ecmascript" initial="open">
  <datamodel>
    <data id="total" expr="0"/>
    <data id="attempts" expr="0"/>
    <data id="charge" expr="null"/>
  </datamodel>
  <state id="open">
    <transition event="item.added" cond="_event.data.price &gt; 0">
      <assign location="total" expr="total + _event.data.price"/>
    </transition>
    <transition event="checkout" cond="total &gt; 0" target="paying"/>
    <transition event="cancel" target="cancelled"/>
  </state>
  <state id="paying">
    <onentry>
      <assign location="attempts" expr="attempts + 1"/>
      <send type="payments" target="charges" event="charge" id="pay">
        <param name="amount" expr="total"/>
      </send>
      <send event="payment.timeout" delay="30s" id="deadline"/>
    </onentry>
    <onexit>
      <cancel sendid="deadline"/>
    </onexit>
    <transition event="charge.ok" target="paid">
      <assign location="charge" expr="_event.data.charge"/>
    </transition>
    <transition event="charge.declined error.communication payment.timeout" cond="attempts &lt; 3" target="paying"/>
    <transition event="charge.declined error.communication payment.timeout" target="cancelled"/>
  </state>
  <state id="paid">
    <transition event="shipped" target="shipped"/>
  </state>
  <final id="shipped">
    <donedata>
      <param name="charge" expr="charge"/>
    </donedata>
  </final>
  <final id="cancelled"/>
</scxml>

Answer each send the actor makes

Send run.* requests to control the host and forwarded methods such as actor.state to query actors. Reply to each effect.send notification with run.send.

requests.jsonltext
{"jsonrpc":"2.0","id":1,"method":"run.spawn","params":{"chart":"order","name":"order-1"}}
{"jsonrpc":"2.0","id":2,"method":"run.send","params":{"target":1,"event":"item.added","data":{"sku":"pen","price":1200}}}
{"jsonrpc":"2.0","id":3,"method":"run.send","params":{"target":1,"event":"checkout"}}
{"jsonrpc":"2.0","id":4,"method":"run.turn","params":{"hold":true}}
{"jsonrpc":"2.0","id":5,"method":"run.send","params":{"target":1,"event":"charge.ok","sendid":"pay","origintype":"payments","data":{"charge":"ch_81"}}}
{"jsonrpc":"2.0","id":6,"method":"run.turn"}
{"jsonrpc":"2.0","id":7,"method":"actor.state","params":{"session":1}}
{"jsonrpc":"2.0","id":8,"method":"run.send","params":{"target":1,"event":"shipped"}}
{"jsonrpc":"2.0","id":9,"method":"run.turn"}
{"jsonrpc":"2.0","id":10,"method":"run.spawn","params":{"chart":"order","name":"order-2"}}
{"jsonrpc":"2.0","id":11,"method":"run.send","params":{"target":2,"event":"item.added","data":{"sku":"ink","price":450}}}
{"jsonrpc":"2.0","id":12,"method":"run.send","params":{"target":2,"event":"checkout"}}
{"jsonrpc":"2.0","id":13,"method":"run.turn"}
{"jsonrpc":"2.0","id":14,"method":"run.advanceTime","params":{"by":"30s"}}
{"jsonrpc":"2.0","id":15,"method":"run.turn"}
{"jsonrpc":"2.0","id":16,"method":"actor.get","params":{"session":2}}
output
{"jsonrpc":"2.0","id":1,"result":{"session":1,"address":"tinyactors://shop/#_scxml_1"}}
{"jsonrpc":"2.0","id":2,"result":{}}
{"jsonrpc":"2.0","id":3,"result":{}}
{"jsonrpc":"2.0","method":"effect.send","params":{"batch":1,"index":0,"session":1,"type":"payments","target":"charges","event":"charge","sendid":"pay","data":{"amount":1200},"traceparent":"00-bba5ad4a1f842e59ffef8375d9ebcaca-94e8a8f3181c87c4-03","waiting":true}}
{"jsonrpc":"2.0","id":4,"result":{"status":"held","runs":1,"fuel":19,"time":0,"nextDeadline":null}}
{"jsonrpc":"2.0","id":5,"result":{}}
{"jsonrpc":"2.0","id":6,"result":{"status":"idle","runs":1,"fuel":14,"time":0,"nextDeadline":null}}
{"jsonrpc":"2.0","id":7,"result":{"value":"paid","history":{}}}
{"jsonrpc":"2.0","id":8,"result":{}}
{"jsonrpc":"2.0","method":"effect.done","params":{"batch":3,"index":0,"session":1,"name":"order-1","final":"shipped","data":{"charge":"ch_81"}}}
{"jsonrpc":"2.0","id":9,"result":{"status":"idle","runs":1,"fuel":6,"time":0,"nextDeadline":null}}
{"jsonrpc":"2.0","id":10,"result":{"session":2,"address":"tinyactors://shop/#_scxml_2"}}
{"jsonrpc":"2.0","id":11,"result":{}}
{"jsonrpc":"2.0","id":12,"result":{}}
{"jsonrpc":"2.0","method":"effect.send","params":{"batch":4,"index":0,"session":2,"type":"payments","target":"charges","event":"charge","sendid":"pay","data":{"amount":450},"traceparent":"00-7efdaa95605dfc9c4bde97c0a78eaab8-63c3721caf3ede87-03","waiting":true}}
{"jsonrpc":"2.0","id":13,"result":{"status":"idle","runs":2,"fuel":22,"time":0,"nextDeadline":30000000000}}
{"jsonrpc":"2.0","id":14,"result":{"time":30000000000,"wallNs":30000000000}}
{"jsonrpc":"2.0","method":"effect.send","params":{"batch":5,"index":0,"session":2,"type":"payments","target":"charges","event":"charge","sendid":"pay","data":{"amount":450},"traceparent":"00-7efdaa95605dfc9c4bde97c0a78eaab8-9f49d57c96da3d8f-03","waiting":true}}
{"jsonrpc":"2.0","id":15,"result":{"status":"idle","runs":2,"fuel":14,"time":30000000000,"nextDeadline":60000000000}}
{"jsonrpc":"2.0","id":16,"result":{"session":2,"segment":0,"chart":"order","parent":null,"mailbox":0,"invocations":0,"timers":[{"sendid":"deadline","event":"payment.timeout","due":60000000000}],"timersPending":1}}

The first order is paid and shipped; the second gets no answer, and its charge goes out again at 30 seconds.

Answer a send before its sender moves on

Pass "hold": true to run.turn to keep an actor waiting after a send marked "waiting": true. Above, the order stays in paying until your script answers in request 5 and turns in request 6.

Without hold, the sender continues immediately and any reply arrives later as an ordinary event.

Move the clock yourself

Advance the host's simulated clock with run.advanceTime instead of waiting in real time. Request 14 skips 30 seconds forward so the next turn fires the second order's timeout immediately.

A session repeats byte for byte

The clock is simulated and Math.random has a fixed seed, so the same requests write the same lines, trace ids included.

Source:tinyactors-core, docs/cli.md

Generate a client from the schema

Run tinyactors run --schema to print the JSON Schema for every request, response and notification. You can also download it from the run command's page.

The protocol is experimental

It may change in any release until a second client uses it. The schema describes the release you have.

Source:tinyactors-core, docs/cli.md