Save 37% off PRO during our Black Friday Sale! »

Supervisors, Links and Monitors

Ce461b57b6a1f65ce5b6cc1c124673e3?s=47 Sean Cribbs
September 20, 2016

Supervisors, Links and Monitors

Using OTP tools in your Elixir application for great justice

Ce461b57b6a1f65ce5b6cc1c124673e3?s=128

Sean Cribbs

September 20, 2016
Tweet

Transcript

  1. SUPERVISORS, LINKS & MONITORS Sean Cribbs

  2. FLASHBACK: 2008 a more innocent time

  3. PLAYSKOOL: MY FIRST ERLANG twitter client mochiweb handler mnesia

  4. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH twitter

    client mochiweb handler mnesia
  5. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH twitter client mochiweb handler mnesia
  6. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia
  7. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia RESULT: Unpredictable behavior
  8. SUPERVISORS are not for restarting processes

  9. SUPERVISORS are for structuring your application

  10. HINDSIGHT IS 20/20 twitter client mochiweb handler mnesia my_app (supervisor)

  11. HINDSIGHT IS 20/20 ➤ mnesia as application dependency twitter client

    mochiweb handler mnesia my_app (supervisor)
  12. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children twitter client mochiweb handler mnesia my_app (supervisor)
  13. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia my_app (supervisor)
  14. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia RESULT: Reliability my_app (supervisor)
  15. :observer.start()

  16. SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one

  17. SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one

    㱺 Task.Supervisor
  18. SUPERVISORS use links and trap exits

  19. LINKS intertwine fates of two processes

  20. A B

  21. A B Process.link(b)

  22. file_server_2 Port (driver)

  23. file_server_2 Port (driver) shutdown!

  24. file_server_2 Port (driver) shutdown! EXIT close()

  25. None
  26. TRAP EXITS if you need to handle exit signals

  27. TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,

    true)
  28. TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,

    true) {:EXIT, pid, reason}
  29. MONITORS notify of process exit without linking

  30. A B

  31. A B Process.monitor(b)

  32. A B Process.monitor(b) {:DOWN, ref, :process, pid, reason}

  33. A B GENSERVER.CALL/3

  34. A B GENSERVER.CALL/3 ➤ Monitor the target {:ok, ref} =

    Process.monitor(b)
  35. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  36. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  37. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  38. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} ➤ :DOWN ⇒ exit(reason) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  39. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} ➤ :DOWN ⇒ exit(reason) ➤ timeout ⇒ exit(:timeout) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  40. registry conn
 sup app sup CUSTOM PROCESS REGISTRY

  41. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node
  42. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry client
  43. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child client
  44. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child conn client
  45. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection conn client
  46. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid 
 (maybe monitor?) conn client
  47. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid 
 (maybe monitor?) ➤ DOWN ⇒ Registry cleans up client
  48. TIPS AND TRICKS ➤ Don’t put too much on your

    Supervisor ➤ Child order matters ➤ Use links and monitors ➤ Read the Elixir and Erlang source
  49. REFERENCES ➤ Erlang Reference Manual: Processes
 http://erlang.org/doc/reference_manual/processes.html ➤ Erlang Reference

    Manual: Errors
 http://erlang.org/doc/reference_manual/errors.html ➤ OTP Design Principles: Overview
 http://erlang.org/doc/design_principles/des_princ.html ➤ OTP Design Principles: Supervisors
 http://erlang.org/doc/design_principles/sup_princ.html ➤ OTP Design Principles: Applications
 http://erlang.org/doc/design_principles/applications.html