Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let it crash

benmills
February 28, 2012

Let it crash

Learn about the "Let it crash" philosophy popularized by Erlang and an introduction to OTP supervisors.

benmills

February 28, 2012
Tweet

More Decks by benmills

Other Decks in Programming

Transcript

  1. When it comes to error-handling I have shown how to

    “abstract out” the errors, and argued that the program should be divided into “pure” code and code which “fixes the errors.” – Joe Armstrong Tuesday, February 28, 12
  2. process("1 + 1") # 2 process("foo") # "" process("1") #

    "" process(1) # "" Tuesday, February 28, 12
  3. def process(message): result = "" if isinstance(message, str): split_message =

    message.split() if len(split_message) == 3: a, operator, b = split_message if a.isdigit() and b.isdigit(): result = int(a) + int(b) return result Tuesday, February 28, 12
  4. def process(message): result = "" if isinstance(message, str): split_message =

    message.split() if len(split_message) == 3: a, operator, b = split_message if a.isdigit() and b.isdigit(): result = int(a) + int(b) return result Tuesday, February 28, 12
  5. % all functions will return a value or % generate

    an exception Tuesday, February 28, 12
  6. % all functions will return a value or % generate

    an exception % you can cause exceptions manually: exit(Pid, Reason). Tuesday, February 28, 12
  7. % all functions will return a value or % generate

    an exception % you can cause exceptions manually: exit(Pid, Reason). % or they will be raised for you: add(A,B) -> A + B. add(foo, 2). % {'EXIT',{badarith,[{intro,add,2}, ... Tuesday, February 28, 12
  8. % if a process exits all linked processes % will

    receive an exit signal and exit Tuesday, February 28, 12
  9. % if a process exits all linked processes % will

    receive an exit signal and exit % if the exit message is the atom ‘normal’ % the linked processes will not exit exit(normal). Tuesday, February 28, 12
  10. % if a process exits all linked processes % will

    receive an exit signal and exit % if the exit message is the atom ‘normal’ % the linked processes will not exit exit(normal). % we can stop linked processes from exiting % by setting the trap_exit process flag process_flag(trap_exit, true). Tuesday, February 28, 12
  11. % if a process exits all linked processes % will

    receive an exit signal and exit % if the exit message is the atom ‘normal’ % the linked processes will not exit exit(normal). % we can stop linked processes from exiting % by setting the trap_exit process flag process_flag(trap_exit, true). % we can override trap_exit by setting the % exit message to the atom ‘kill’ exit(kill). Tuesday, February 28, 12
  12. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, Shutdown, Type, Modules } Tuesday, February 28, 12
  13. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘temporary’ or ‘transient’ Shutdown, Type, Modules } Tuesday, February 28, 12
  14. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’ Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer Type, Modules } Tuesday, February 28, 12
  15. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’ Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer Type, % atom ‘worker’ or ‘supervisor’ Modules } Tuesday, February 28, 12
  16. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’ Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer Type, % atom ‘worker’ or ‘supervisor’ Modules % list of used modules } Tuesday, February 28, 12
  17. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’ Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer Type, % atom ‘worker’ or ‘supervisor’ Modules % list of used modules } Tuesday, February 28, 12
  18. {ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []},

    permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12
  19. {ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []},

    permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12
  20. {ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []},

    permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12
  21. {ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []},

    permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12
  22. • Erlang and OTP in Action http://www.manning.com/logan/ • Learn you

    some Erlang for Great Good http://learnyousomeerlang.com/ • erldocs http://erldocs.com/ Tuesday, February 28, 12