🎀 Gleam tangled site soon? 👀

Using the erlang/OTP application callbacks #3

Closed strawmelonjuice.nl
Closed opened by and assigned to strawmelonjuice.nl

The server should implement the Erlang/OTP application callbacks instead of manually starting with let assert and process.sleep_forever()

Something like in bowtie_server.gleam

/// The Erlang/OTP application start callback.
/// Responsible to start the "top" supervisor process and return its Pid
/// to the application controller.
pub fn start(_app: atom.Atom, _type: a) -> Result(process.Pid, actor.StartError) {
  case start_supervisor() {
    Ok(actor.Started(pid, _data)) -> {
      let sup_name = process.new_name("bowtie")
      let _ = process.register(pid, sup_name)
      Ok(pid)
    }
    Error(reason) -> Error(reason)
  }
}

/// The Erlang/OTP application stop callback.
/// This is called after all processes in the supervisor tree have
/// been shutdown by the application controller. Responsible for any
/// final clean up actions.
pub fn stop(_state: a) -> atom.Atom {
  atom.create("ok")
}

Where start_supervisor has the content of the current main() (Without the let assert and process.sleep_forever ofc)

You'd also need to add the application start module to the gleam.toml like this:

[erlang]
application_start_module = "bowtie_server"

This way the beam can actually restart the server if/when it crashes

mrrp shall be done immediately since I forgot the reason why I didn't yet

Having it as a proper application does mean it'll also start when running gleam test and gleam dev

that may be the why

but also I see only pros for bowtie, my reason comes from the currently sibling-in-code Lumina codebase where that may be problematic since the database won't run, but Bowtie will not have an external database (if it does need persistent data at a point I'm hoping to go Storail or SQLite)

that said I should also just start the db on Lumina within the justfile 😅 so I don't need this anymore

ime the main issue with it running with tests is the database in CI. But even then, with tangled CI it should be easy enough to spin up whatever DB you may need

Sign up or Sign in to add to the discussion.