This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This an example of how a runtime exception inside of a flow | |
# sends a :normal exit message. | |
# | |
# To run this code add the Flow dependency to the mix.exs file: | |
# {:flow, "~> 0.11"} | |
# | |
# The behaviour that I get is the stacktrace is printed to STDOUT, | |
# and the flush() function outputs a message like this: | |
# {:EXIT, #PID<0.144.0>, :normal} | |
# But I am expecting a non-normal exit message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start a supervisor with two child specifications | |
iex(1)> {:ok, pid} = MySupervisor.start_link([{Parser, :start_link, []}, {Parser, :start_link, []}]) | |
{:ok, #PID<0.112.0>} | |
# See the two processes that the supervisor started | |
iex(2)> MySupervisor.list_processes(pid) | |
%{#PID<0.113.0> => {Parser, :start_link, []}, | |
#PID<0.114.0> => {Parser, :start_link, []}} | |
# Get a reference to one of the Parser process pid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## OTP Callbacks | |
def handle_info({:EXIT, dead_pid, _reason}, state) do | |
# Start new process based on dead_pid spec | |
{new_pid, child_spec} = state | |
|> Map.get(dead_pid) | |
|> start_child() | |
# Remove the dead_pid and insert the new_pid with its spec | |
new_state = state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get a reference to one of the Parser process pid | |
iex(4)> parser_pid = MySupervisor.list_processes(pid) |> Map.keys |> List.first | |
#PID<0.124.0> | |
# Send a valid message | |
iex(5)> GenServer.call(parser_pid, "100") | |
100 | |
# Send an invalid message | |
iex(6)> GenServer.call(parser_pid, "tyler") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start a supervisor with two child specifications | |
iex(1)> {:ok, pid} = MySupervisor.start_link([{Parser, :start_link, []}, {Parser, :start_link, []}]) | |
{:ok, #PID<0.123.0>} | |
# See the two processes that the supervisor started | |
iex(2)> MySupervisor.list_processes(pid) | |
%{#PID<0.124.0> => {Parser, :start_link, []}, | |
#PID<0.125.0> => {Parser, :start_link, []}} | |
# Verify the supervisor has linked to the processes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## lib/MySupervisor.ex | |
defmodule MySupervisor do | |
use GenServer | |
## API | |
def start_link(child_spec_list) do | |
GenServer.start_link(__MODULE__, child_spec_list) | |
end | |
def list_processes(pid) do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a new Parser process | |
iex(1)> {:ok, pid} = Parser.start_link() | |
{:ok, #PID<0.112.0>} | |
# Figure out our iex process' PID | |
iex(2)> self() | |
#PID<0.110.0> | |
# See that our iex process is linked to the Parser process | |
iex(3)> Process.info(self(), :links) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Parser do | |
use GenServer | |
def start_link() do | |
GenServer.start_link(__MODULE__, :ok, []) | |
end | |
def handle_call(message, _from, state) do | |
i = String.to_integer(message) | |
{:reply, i, state} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a new Parser process | |
iex(2)> {:ok, pid} = Parser.start_link() | |
{:ok, #PID<0.118.0>} | |
# Verify the process is alive | |
iex(3)> Process.alive?(pid) | |
true | |
# Send it a valid message | |
iex(4)> GenServer.call(pid, "100") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an example of how to express Infinity in Json using the Play! Scala Json library. | |
// Infinity cannot be expressed as a Scalar value in Json, so I am using an object to hold it instead. | |
// | |
// Example of a finite value: | |
// { | |
// "value": 12.009, | |
// "isInfinite": false | |
// } | |
// | |
// Example of an infinite value: |