diff --git a/gleam.toml b/gleam.toml index e652086..2493151 100644 --- a/gleam.toml +++ b/gleam.toml @@ -16,6 +16,7 @@ version = "1.0.0" gleam_stdlib = "~> 0.34 or ~> 1.0" gleam_erlang = "~> 0.24" gleam_otp = "~> 0.9" +prng = "~> 3.0" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index bc3e40a..812f40e 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,14 +2,17 @@ # You typically do not need to edit this file packages = [ + { name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" }, { name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" }, - { name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" }, + { name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" }, { name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" }, { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, + { name = "prng", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "prng", source = "hex", outer_checksum = "C78A80DE41469A0BB1AB3B0B0610CCE5DB70C5659A540E2E0E6C54FA38134290" }, ] [requirements] gleam_erlang = { version = "~> 0.24" } -gleam_otp = { version = "~> 0.9"} +gleam_otp = { version = "~> 0.9" } gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } gleeunit = { version = "~> 1.0" } +prng = { version = "~> 3.0"} diff --git a/src/supervisors.gleam b/src/supervisors.gleam new file mode 100644 index 0000000..253d8c8 --- /dev/null +++ b/src/supervisors.gleam @@ -0,0 +1 @@ +//// Todo \ No newline at end of file diff --git a/src/supervisors/a_shit_actor.gleam b/src/supervisors/a_shit_actor.gleam new file mode 100644 index 0000000..c78ea3f --- /dev/null +++ b/src/supervisors/a_shit_actor.gleam @@ -0,0 +1,38 @@ +//// This module implements a shit actor that crashes every now and then. +//// Having an actor that fails every now and then will help us test out our supervisors. +//// It's also a nice refresher on actors. Read the code and make sure it makes sense. + +import gleam/otp/actor +import gleam/erlang/process.{type Subject} +import prng/random + +pub fn new() -> Result(Subject(Message), actor.StartError) { + actor.start(Nil, handle_message) +} + +pub fn shutdown(subject: Subject(Message)) { + actor.send(subject, Shutdown) +} + +pub fn play_game(subject: Subject(Message)) -> String { + let msg_generator = random.weighted(#(0.9, Duck), [#(0.1, Goose)]) + let msg = random.random_sample(msg_generator) + actor.call(subject, msg, 1000) +} + +pub type Message { + Duck(client: Subject(String)) + Goose(client: Subject(String)) + Shutdown +} + +fn handle_message(message: Message, _state: Nil) -> actor.Next(Message, Nil) { + case message { + Duck(client) -> { + actor.send(client, "duck") + actor.continue(Nil) + } + Goose(_) -> panic as "Oh shit it's a fucking goose!!!!" + Shutdown -> actor.Stop(process.Normal) + } +}