In order to reliably remember a value, we spin up a process that does nothing except remembering. This isolates the information storage, which has maximal reliability, from information manipulation, which can have all sorts of problems.
An OTP Supervisor manages one or more processes, which can be additional Supervisors.
Start a Supervisor project as follows:
mix new --sup sequence
Creates additional files:
lib/sequence.ex lib/sequence/application.ex
The autogenerated
Application
module is explicitly not documented:@moduledoc false
Supervisor restart strategies can be:
:one_for_one
: Restart the failed server.:one_for_all
: Restart all the servers if any fail.:rest_for_one
: Restart server and child servers that appear later in thechildren
list.
GenServers can declare when they should be restarted:
:permanent
: Always restart after termination.:temporary
: Never restart after termination.:transient
: Restart only after an abnormal termination.
The default is :permanent
.
OTP-Supervisors-1
Add a Supervisor to the Stack application. Source
Changed module name from Stack
to Stack.Server
, which it is in the book.
Better defines the module’s purpose.
Made pushing new value <0 crash, since popping the empty stack does not crash in my implementation.
Renamed the project from
otpserver
tostack
now that it is used across multiple chapters.
OTP-Supervisors-2
Add a separate Stash process. Source
- Move the stack initialization to the Stash.
- With this approach, we can no longer manually initialize the stack via
Stack.Server.init
- In real-life, we’d likely have a
Stack.Server.reset
.
All notes and comments are my own opinion. Follow me at @rgacote@genserver.social