forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor_tree.nula
More file actions
74 lines (62 loc) · 1.99 KB
/
Copy pathsupervisor_tree.nula
File metadata and controls
74 lines (62 loc) · 1.99 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
// Supervisor Tree — nested OTP supervisors with restart strategies.
// A root supervisor manages child supervisors and workers.
// Demonstrates the OTP built-in effect surface.
//
// Run with: nulang examples/supervisor_tree.nula
actor Worker {
state id: Int = 0
state healthy: Bool = true
behavior init(worker_id: Int) {
self.id = worker_id
perform IO.print(
"Worker " + perform Int.to_string(worker_id) + " started"
)
}
behavior work(x: Int) {
if self.healthy then
perform IO.print(
"Worker " + perform Int.to_string(self.id) +
" processed " + perform Int.to_string(x)
)
else
perform IO.print(
"Worker " + perform Int.to_string(self.id) + " is sick, skipping"
)
}
behavior sicken() {
self.healthy = false
perform IO.print(
"Worker " + perform Int.to_string(self.id) + " is now sick"
)
}
}
fn main() {
// Create a root supervisor (one_for_one strategy = 0)
let root = perform Otp.create_supervisor("root", 0)
// Create a simple-one-for-one child pool (strategy = 3)
let pool = perform Otp.create_supervisor("worker_pool", 3)
let t = perform Otp.set_template(pool, "Worker")
// Start two workers from the template
let w1 = perform Otp.start_child(pool)
let w2 = perform Otp.start_child(pool)
// Initialize and do work
w1 ! init(1)
w2 ! init(2)
w1 ! work(10)
w2 ! work(20)
// Check count
let count = perform Otp.child_count(pool)
perform IO.print(
"Active workers: " + perform Int.to_string(count)
)
// Terminate one worker normally
let r = perform Otp.terminate_child(pool, w2)
let count2 = perform Otp.child_count(pool)
perform IO.print(
"After terminate: " + perform Int.to_string(count2)
)
// Make worker 1 sick, then check it still exists (not killed)
w1 ! sicken()
w1 ! work(99)
0
}