You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/supervised_stack.livemd
+15-29
Original file line number
Diff line number
Diff line change
@@ -32,9 +32,13 @@ Mix.install([
32
32
33
33
## Supervised Stack
34
34
35
-
Previously we created a [Stack](./stack_server.livemd)[GenServer](https://hexdocs.pm/elixir/GenServer.html) process.
35
+
You're going to create a stack project that starts a name [Stack](./stack_server.livemd) GenServer process under a supervisor.
36
36
37
-
We've made a slight modification to the `Stack` process by adding a `start_link/1` function so this named `Stack` can be started under a [Supervisor](https://hexdocs.pm/elixir/Supervisor.html).
37
+
```
38
+
mix new stack
39
+
```
40
+
41
+
Here's an example Stack GenServer you can use.
38
42
39
43
```elixir
40
44
defmoduleStackdo
@@ -63,30 +67,13 @@ defmodule Stack do
63
67
end
64
68
```
65
69
66
-
We're able to push and pop elements off of the stack.
However, there's a bug. If we try to `pop/1` an item off of an empty stack, the process
70
+
We're able to push and pop elements off of the stack. However, there's a bug. If we try to `pop/1` an item off of an empty stack, the process
84
71
will crash due to a function clause error because the `handle_call/2` function expects a list with one or more elements.
85
72
86
73
Uncomment the following code to watch the `Stack` crash.
87
74
88
75
```elixir
89
-
# {:ok, Pid} = GenServer.start_link(Stack, [])
76
+
# {:ok, pid} = GenServer.start_link(Stack, [])
90
77
# GenServer.call(stack_pid, :pop)
91
78
```
92
79
@@ -102,7 +89,7 @@ def handle_call(:pop, _from, []) do
102
89
end
103
90
```
104
91
105
-
Instead, you're going to start the Stack process under a supervisor so that it will be restarted when it crashes. In the Elixir cell below, start the `Stack` process under a supervisor so that it will restart with an empty stack when it crashes.
92
+
Instead, you're going to start the `Stack` process under a supervisor in your application so that it will be restarted when it crashes.
Keep in mind, if you have already started a supervisor with the `Stack` process, your livebook may crash. You can resolve this issue by simply re-running the cell below to start the supervisor again.
107
+
<!--livebook:{"break_markdown":true} -->
121
108
122
-
```elixir
109
+
### Crash The Stack
123
110
124
-
```
125
-
126
-
You should be able to send a `:pop` message to the `Stack` process and the [Supervisor](https://hexdocs.pm/elixir/Supervisor.html) will restart the `Stack` process.
111
+
Open the [IEx](https://hexdocs.pm/iex/IEx.html) shell and send the `Stack` a `:pop` message to cause it to crash and restart.
127
112
128
-
Uncomment and evaluate the code below to test your supervisor.
0 commit comments