An app with a skail function that bills a batch of orders: it asks the city hall for one service invoice per order, in waves of 10, and consolidates the result when every invoice is done. Each invoice is a child run with its own history, so when the city hall is unavailable, skail retries only that invoice and the others go on. An authorized invoice sends the PDF to the customer; a rejected or pending one goes to finance, and the batch does not stop. You start the batches on a page the app serves at http://localhost:8080.
It shows three skail features:
- fan-out and fan-in: one child run per invoice, in waves of 10, joined by a wait for all of them;
- automatic retries: the simulated city hall is unavailable on about 1 call in 10, and skail retries only that invoice;
- results instead of errors: a rejection is a result, not an exception, and an invoice that fails on every attempt becomes pending, so the batch always completes.
- One of: Docker, Podman or Node.js 20.12 or newer. The run script uses the first one it finds.
- A skail environment with a workload registered for the
SKAIL_WORKLOADin.env(batchbilling:v1.0.0by default). The Quickstart shows how to create the account, the environment and the workload.
The project uses @skail/runtime and @skail/build 2.0.1-rc4.
cp .env.example .env # fill in SKAIL_SIDECAR, SKAIL_NAMESPACE and SKAIL_KEY (portal > Environments & keys > Connect)
scripts/run.sh # then open http://localhost:8080On Windows, in PowerShell: copy .env.example .env, then powershell -ExecutionPolicy Bypass -File scripts\run.ps1. The script works on the Windows PowerShell built into Windows; PowerShell 7 is not needed.
The script runs the demo with what the machine has: Docker, then Podman, then Node.js. If it finds none of them, it says what to install. Without the script, the commands are npm install, npm run build and npm start.
Pick the size of the batch (10, 20 or 50 invoices) and click Start a batch. Each invoice is a square that changes color as it goes: waiting for its wave, issuing, city hall down and retrying, then authorized, rejected or pending; the ones the city hall refused at first show how many attempts they took. The Customer inbox gets the PDFs of the authorized invoices, and the Finance inbox gets the rejected and pending ones, with the reason. The page refreshes itself every second, and each step is also printed to the console.
The simulated city hall rejects the orders whose number is a multiple of 5 (invalid customer tax id), leaves pending the multiples of 7 and authorizes the rest. Its answer is the same for the same order, so a retry gets the same invoice.
| What you click | What happens | Result |
|---|---|---|
| Start a batch with 10 invoices | one wave of 10 | completed: 7 authorized, 2 rejected, 1 pending |
| Start a batch with 20 invoices | two waves of 10 | completed: 14 authorized, 4 rejected, 2 pending |
| Start a batch with 50 invoices | five waves of 10 | completed: 34 authorized, 10 rejected, 6 pending |
| any batch, when the city hall is unavailable on a call | skail retries only that invoice, about 15 seconds later | the same result; the invoice shows its attempts |
An invoice never shows up in both inboxes, nor twice in the same one. The amount billed adds only the authorized invoices.
The app does three jobs in one process: it serves the page, it hosts the worker that runs the functions (createSkailPlatform, with the bundle built by npm run build), and it starts each batch through the skail HTTP API. The city hall, the customer's mailbox and the finance inbox are simulated in the same process. The commands run in an isolated VM, so they reach the city hall and the inboxes over HTTP, on the app's own port under /internal/, as they would reach real services.
POST {SKAIL_SIDECAR}/api/v1/trigger/batchbilling/v1.0.0/BillBatch/{batchId}
["{batchId}", 20]The functions, in src/billing-functions.ts:
@SkailFunction()
async BillBatch(batchId: string, size: number): Promise<BatchResult> {
const orders = generateOrders(batchId, size);
const invoices: Invoice[] = [];
for (const wave of chunk(orders, BatchLimits.waveSize)) {
const issuing = wave.map((order) => this.IssueInvoice(batchId, size, order));
invoices.push(...(await Skail.whenAll(issuing)));
}
const result = consolidate(batchId, invoices);
await this.ReportResult(batchId, size, result, invoices);
return result;
}
@SkailFunction()
async IssueInvoice(batchId: string, size: number, order: Order): Promise<Invoice> {
let invoice: Invoice;
try {
invoice = await this.IssueAtCityHall(batchId, size, order);
} catch (error) {
if (error instanceof SkailNonDeterministicError) throw error;
invoice = pendingInvoice(order, "city hall unavailable");
}
if (invoice.status === "Authorized") await this.SendPdfToCustomer(batchId, size, invoice);
else await this.NotifyFinance(batchId, size, invoice);
return invoice;
}- The orders come from arithmetic over the batch id and the size, so
BillBatchstays deterministic; in a real system a command would load them. - Each
IssueInvoicecall becomes a child run, with its own history and its own entry in the Monitor. The function creates the 10 child runs of a wave before waiting, so they run in parallel, and the next wave starts when the current one ends: the city hall never gets more than 10 requests at once. - After
whenAll, the invoices are read in the order they were created, not in the order they finished. - A rejected or pending invoice is a result: the city hall answered, and
NotifyFinancehands it to finance. Only an outage throws, so skail retriesIssueAtCityHallfor that invoice, up to 5 times (@SkailCommand({ retryCount: 5 })). If every attempt fails,IssueInvoicecatches the error and the invoice becomes pending. - The order id (batch id and order number) is the idempotency key at the city hall, and the invoice number keeps each inbox from getting the same invoice twice when a command is retried.
| File | What it has |
|---|---|
src/billing-functions.ts |
The functions and their commands. |
src/model.ts |
The orders, the invoices and the result of the batch. |
src/billing-desk.ts |
Starts the batches for the page, plays the city hall and the two inboxes, and keeps what the page shows. |
page/index.html |
The page, with the skail branding. |
src/skail-api.ts |
The trigger through the skail HTTP API. |
src/main.ts |
Reads .env, serves the page, its API and the simulated services, and starts the worker. |
scripts/build-skail.mjs |
Builds the bundle into .skail/ for SKAIL_WORKLOAD; run npm run build again if it changes. |
scripts/run.sh, scripts/run.ps1, Dockerfile |
Run the demo with Docker, Podman or Node.js, whatever the machine has. |
- Run
scripts/run.sh, open the page, pick 50 invoices and click Start a batch. - While the first waves run, press Ctrl+C. The app closes; the batch stays in skail.
- Run
scripts/run.shagain and open the page. skail hands the app the batch: it goes on from where it was and ends with the 50 invoices. The invoices issued before the restart are not issued again, and the final result fills them in on the page. An invoice that was in the middle of its call to the city hall when the app closed can take a few minutes to be handed out again; the batch waits for it.
In the portal, open the Monitor of your environment and click the batch: it lists the IssueInvoice child runs inside each whenAll and the final ReportResult. Each IssueInvoice shows its own commands: IssueAtCityHall, then SendPdfToCustomer or NotifyFinance. An invoice retried after an outage shows the failed attempts of its IssueAtCityHall.
- There is no button to stop a batch: once started, it runs to the end. The page shows the latest batch.
- Apps running the same
SKAIL_WORKLOADon the same environment share the same runs: use one workload (or environment) per person.
This is a demo, not production code. It exists only to show how skail works and is provided "as is", without warranty of any kind. The city hall, the customer's mailbox and the finance inbox are simulated: no real invoice is issued or sent.
