# Example 18: Distributed Computing
# Demonstrates cluster management, job scheduling, and result aggregation
print("=== Distributed Computing Demo ===")
print("")
# Create a compute cluster
print("Creating cluster...")
let my_cluster = cluster_create("demo-cluster")
print("Cluster: " + my_cluster.name)
print("")
# Add compute nodes
print("Adding compute nodes...")
let node1 = cluster_add_node("node-1", "192.168.1.10:8080")
let node2 = cluster_add_node("node-2", "192.168.1.11:8080", ["gpu", "compute"])
let node3 = add_node("node-3", "192.168.1.12:8080", ["compute", "storage"])
print("Node 1: " + node1.id + " at " + node1.address)
print("Node 2: " + node2.id + " (capabilities: gpu, compute)")
print("Node 3: " + node3.id + " (capabilities: compute, storage)")
print("")
# Check cluster status
print("Cluster status:")
let status = cluster_status()
print(" Total nodes: " + status.total_nodes)
print(" Online nodes: " + status.online_nodes)
print(" Average load: " + status.average_load)
print("")
# List all nodes
print("All nodes:")
let all_nodes = cluster_nodes()
all_nodes | each(fn(n) => print(" - " + n.id + ": " + n.status))
print("")
# Job submission is not implemented: job_submit (and its alias submit) refuses
# with E_UNIMPLEMENTED and issues no job id, so the submissions that stood here
# are removed. job_list reads a table that only job_submit fills, so it is empty.
# Check job status
print("Job statuses:")
job_list() | each(fn(j) => print(" " + j.name + ": " + j.status))
print("")
# Remote execution (stub: remote_exec does NOT run the command -- use ssh_exec
# for real remote execution; it is approval-gated in agent mode)
print("Remote execution (simulated):")
let exec_result = remote_exec("node-1", "ls -la /data")
print(" Node: " + exec_result.node_id)
print(" Status: " + exec_result.status)
print(" Simulated: " + "${exec_result.simulated}")
print("")
# Result aggregation
print("Result aggregation examples:")
let numbers = [10, 20, 30, 40, 50]
print(" Data: " + numbers)
print(" Sum: " + aggregate(numbers, "sum"))
print(" Count: " + aggregate(numbers, "count"))
print(" First: " + aggregate(numbers, "first"))
print(" Last: " + aggregate(numbers, "last"))
print("")
# Cleanup: there is no submitted job to cancel (job_submit refuses), so the
# job_cancel call that followed the submissions is removed.
# Remove a node
print("Removing node-3...")
let removed = cluster_remove_node("node-3")
print(" Removed: " + removed)
print("")
# Final status
print("Final cluster status:")
let final_status = cluster_status()
print(" Nodes: " + final_status.total_nodes)
print(" Jobs: " + final_status.total_jobs)
print("")
print("=== Distributed Computing Demo Complete ===")