Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Last active May 12, 2018 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boddhisattva/d3667e4153cc07bd9a6cff05cea69ebc to your computer and use it in GitHub Desktop.
Save boddhisattva/d3667e4153cc07bd9a6cff05cea69ebc to your computer and use it in GitHub Desktop.
Trying out supervisors with Elixir:)

From Dave Thomas' course, trying Supervisors in IEx wrt learnings from the chapter - No Mr Dictionary, I expect you to Die

For the code changes here: https://github.com/boddhisattva/e4p/blob/using-supervisors-with-dictionary/game/dictionary/lib/dictionary/word_list.ex#L12-L14 or

defmodule Dictionary.WordList do
  @moduledoc """
  Documentation for WordList.
  """
  @me __MODULE__

  def start_link() do
    Agent.start_link(&word_list/0, name: @me)
  end

  def random_word() do
    if :rand.uniform < 0.33 do # For these 3 lines in particular... 19-21 we've tried the below thing in IEx to see 
    # how supervisors can be helpful
      Agent.get(@me, fn _ -> exit(:boom) end) 
    end 
    Agent.get(@me, &Enum.random/1)
  end

  def word_list do
    "../../assets/words.txt"
    |> Path.expand(__DIR__)
    |> File.read!()
    |> String.split(~r/\n/)
  end
end
Interactive Elixir (1.6.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Dictionary.random_word
** (exit) exited in: GenServer.call(Dictionary.WordList, {:get, #Function<0.39750120/1 in Dictionary.WordList.random_word/0>}, 5000)
    ** (EXIT) :boom
    (elixir) lib/gen_server.ex:834: GenServer.call/3
    (dictionary) lib/dictionary/word_list.ex:13: Dictionary.WordList.random_word/0
iex(1)>
04:59:51.980 [error] GenServer Dictionary.WordList terminating
** (stop) :boom
    (dictionary) lib/dictionary/word_list.ex:13: anonymous fn/1 in Dictionary.WordList.random_word/0
    (elixir) lib/agent/server.ex:12: Agent.Server.handle_call/3
    (stdlib) gen_server.erl:636: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:665: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.170.0>): {:get, #Function<0.39750120/1 in Dictionary.WordList.random_word/0>}
State: ["that", "this", "with", "from", "your", "have", "more", "will", "home", "about", "page", "search", "free", "other", "information", "time", "they", "site", "what", "which", "their", "news", "there", "only", "when", "contact", "here", "business", "also", "help", "view", "online", "first", "been", "would", "were", "services", "some", "these", "click", "like", "service", "than", "find", "price", "date", "back", "people", "list", "name", ...]
Client #PID<0.170.0> is alive
    (stdlib) gen.erl:169: :gen.do_call/4
    (elixir) lib/gen_server.ex:831: GenServer.call/3
    (dictionary) lib/dictionary/word_list.ex:13: Dictionary.WordList.random_word/0
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) src/elixir.erl:233: :elixir.eval_forms/4
    (iex) lib/iex/evaluator.ex:250: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:230: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:208: IEx.Evaluator.eval/3


nil
iex(2)> Dictionary.random_word
** (exit) exited in: GenServer.call(Dictionary.WordList, {:get, #Function<0.39750120/1 in Dictionary.WordList.random_word/0>}, 5000)
    ** (EXIT) :boom
    (elixir) lib/gen_server.ex:834: GenServer.call/3
    (dictionary) lib/dictionary/word_list.ex:13: Dictionary.WordList.random_word/0
iex(2)>
04:59:55.326 [error] GenServer Dictionary.WordList terminating
** (stop) :boom
    (dictionary) lib/dictionary/word_list.ex:13: anonymous fn/1 in Dictionary.WordList.random_word/0
    (elixir) lib/agent/server.ex:12: Agent.Server.handle_call/3
    (stdlib) gen_server.erl:636: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:665: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.170.0>): {:get, #Function<0.39750120/1 in Dictionary.WordList.random_word/0>}
State: ["that", "this", "with", "from", "your", "have", "more", "will", "home", "about", "page", "search", "free", "other", "information", "time", "they", "site", "what", "which", "their", "news", "there", "only", "when", "contact", "here", "business", "also", "help", "view", "online", "first", "been", "would", "were", "services", "some", "these", "click", "like", "service", "than", "find", "price", "date", "back", "people", "list", "name", ...]
Client #PID<0.170.0> is alive
    (stdlib) gen.erl:169: :gen.do_call/4
    (elixir) lib/gen_server.ex:831: GenServer.call/3
    (dictionary) lib/dictionary/word_list.ex:13: Dictionary.WordList.random_word/0
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) src/elixir.erl:233: :elixir.eval_forms/4
    (iex) lib/iex/evaluator.ex:250: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:230: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:208: IEx.Evaluator.eval/3


nil
iex(3)> Dictionary.random_word
"surprise"
iex(4)> Dictionary.random_word
"borough"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment