Skip to content

Commit 81c325b

Browse files
committed
supervised function
1 parent d46f0b7 commit 81c325b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- In the `gleam/otp/static_supervisor` module:
1111
- The `start_link` module has been removed.
1212
- The `start` module has been added.
13+
- The `supervised` module has been added.
1314
- Types and functions for defining child specifications have been moved to the
1415
`supervision` module.
1516
- Child ids are now generated, removing possibility of a collision.

src/gleam/otp/static_supervisor.gleam

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ pub fn auto_shutdown(builder: Builder, value: AutoShutdown) -> Builder {
144144
/// Start a new supervisor process with the configuration and children
145145
/// specified within the builder.
146146
///
147+
/// Typically you would use the `supervised` function to add your supervisor to
148+
/// a supervision tree instead of using this function directly.
149+
///
147150
/// The supervisor will be linked to the parent process that calls this
148-
/// function, ideally another supervisor.
151+
/// function.
149152
///
150153
/// If any child fails to start the supevisor first terminates all already
151154
/// started child processes with reason shutdown and then terminate itself and
@@ -171,6 +174,19 @@ pub fn start(
171174
}
172175
}
173176

177+
/// Create a `ChildSpecification` that adds this supervisor as the child of
178+
/// another, making it fault tolerant and part of the application's supervision
179+
/// tree. You should prefer to starting unsupervised supervisors with the
180+
/// `start` function.
181+
///
182+
/// If any child fails to start the supevisor first terminates all already
183+
/// started child processes with reason shutdown and then terminate itself and
184+
/// returns an error.
185+
///
186+
pub fn supervised(builder: Builder) -> ChildSpecification(Supervisor) {
187+
supervision.supervisor(fn() { start(builder) })
188+
}
189+
174190
@external(erlang, "gleam_otp_external", "convert_erlang_start_error")
175191
fn convert_erlang_start_error(dynamic: Dynamic) -> actor.StartError
176192

src/gleam/otp/supervision.gleam

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ pub type ChildSpecification(data) {
5151
)
5252
}
5353

54-
/// A regular child that is not also a supervisor.
54+
/// A regular child process.
55+
///
56+
/// You should use this unless your process is also a supervisor.
5557
///
5658
pub fn worker(
5759
run start: fn() -> Result(actor.Started(data), actor.StartError),

0 commit comments

Comments
 (0)