-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupervision_tree.exs
More file actions
77 lines (54 loc) · 2.93 KB
/
Copy pathsupervision_tree.exs
File metadata and controls
77 lines (54 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Firebird Supervision Tree Example
#
# Shows how to use Firebird.Module and Firebird.Pool in OTP supervision trees.
#
# Run: mix run examples/supervision_tree.exs
IO.puts("🔥 Firebird Supervision Tree Example\n")
# ── 1. Define a declarative WASM module ─────────────────────────────────────
defmodule MyApp.Math do
use Firebird, wasm: "fixtures/math.wasm"
wasm_fn :add, args: 2, doc: "Add two numbers"
wasm_fn :multiply, args: 2, doc: "Multiply two numbers"
wasm_fn :fibonacci, args: 1, doc: "Calculate fibonacci number"
end
# ── 2. Start a supervision tree with WASM children ──────────────────────────
children = [
# Declarative module as a supervised worker
MyApp.Math,
# Pool as a supervised worker
{Firebird.Pool, wasm: "fixtures/math.wasm", size: 4, name: :math_pool}
]
{:ok, sup} = Supervisor.start_link(children, strategy: :one_for_one)
IO.puts("✅ Supervisor started with #{length(children)} children\n")
# ── 3. Use the declarative module ──────────────────────────────────────────
IO.puts("Using MyApp.Math (declarative module):")
{:ok, [result]} = MyApp.Math.add(10, 20)
IO.puts(" add(10, 20) = #{result}")
[result] = MyApp.Math.multiply!(7, 8)
IO.puts(" multiply!(7, 8) = #{result}")
[fib] = MyApp.Math.fibonacci!(10)
IO.puts(" fibonacci!(10) = #{fib}")
IO.puts(" exports: #{inspect(MyApp.Math.exports())}")
IO.puts(" function_exists?(:add) = #{MyApp.Math.function_exists?(:add)}\n")
# ── 4. Use the pool ───────────────────────────────────────────────────────
IO.puts("Using :math_pool (connection pool):")
{:ok, [result]} = Firebird.Pool.call(:math_pool, :add, [100, 200])
IO.puts(" Pool.call(:math_pool, :add, [100, 200]) = #{result}")
[result] = Firebird.Pool.call!(:math_pool, :multiply, [6, 7])
IO.puts(" Pool.call!(:math_pool, :multiply, [6, 7]) = #{result}")
status = Firebird.Pool.status(:math_pool)
IO.puts(" Pool status: #{inspect(status)}\n")
# ── 5. Concurrent pool usage ──────────────────────────────────────────────
IO.puts("Concurrent pool calls:")
tasks =
for i <- 1..10 do
Task.async(fn ->
[result] = Firebird.Pool.call!(:math_pool, :fibonacci, [20])
result
end)
end
results = Task.await_many(tasks)
IO.puts(" 10 concurrent fibonacci(20) calls all returned: #{hd(results)}\n")
# ── 6. Clean shutdown ──────────────────────────────────────────────────────
Supervisor.stop(sup)
IO.puts("✅ Supervisor stopped cleanly. All WASM instances freed.")