-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chuck Norris! (or: Added code for chapter 9)
- Loading branch information
1 parent
daf3574
commit 34a85e3
Showing
11 changed files
with
499 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Chucky - A Distributed and Fault-Tolerant Chuck Norris Facts Disher | ||
|
||
![Chuck](http://i.imgur.com/wwFsWiA.jpg) | ||
|
||
## Step 1: Figure out your hostname | ||
|
||
``` | ||
% hostname -s | ||
imac | ||
``` | ||
|
||
## Step 2: Configure `config/NODE_NAME.config` | ||
|
||
Here's an example: | ||
|
||
```elixir | ||
[{kernel, | ||
[{distributed, | ||
[{chucky, | ||
5000, | ||
['a@imac', {'b@imac', 'c@imac'}]}]}, | ||
{sync_nodes_mandatory, ['b@imac', 'c@imac']}, | ||
{sync_nodes_timeout, 30000} | ||
]}]. | ||
``` | ||
|
||
## Step 3: Compile | ||
|
||
|
||
``` | ||
% mix compile | ||
``` | ||
|
||
## Step 4: Run it! | ||
|
||
Open 3 different terminals, and on each of them, run these commands: | ||
|
||
``` | ||
% iex --sname a -pa _build/dev/lib/chucky/ebin --app chucky --erl "-config config/a.config" | ||
% iex --sname b -pa _build/dev/lib/chucky/ebin --app chucky --erl "-config config/b.config" | ||
% iex --sname c -pa _build/dev/lib/chucky/ebin --app chucky --erl "-config config/c.config" | ||
``` | ||
|
||
In each terminal, run: | ||
|
||
```elixir | ||
iex > Chucky.fact | ||
"In a fight between Batman and Darth Vader, the winner would be Chuck Norris." | ||
``` | ||
|
||
You can also use `Application.started_applications/1` to see where the application is being run on. | ||
|
||
## Step 5: Watching failover in action | ||
|
||
Kill the first session (`a@HOSTNAME`), then watch `b@HOSTNAME` get started: | ||
|
||
``` | ||
07:33:04.831 [info] b@manticore starting distributed | ||
07:33:12.025 [info] Application chucky exited: :stopped | ||
07:33:42.300 [info] b@manticore starting distributed | ||
``` | ||
|
||
## Step 6: Watching takeover in action | ||
|
||
Start `a@HOSTNAME` again: | ||
|
||
``` | ||
% iex --sname a -pa _build/dev/lib/chucky/ebin --app chucky --erl "-config config/a.config" | ||
``` | ||
|
||
Watch `a@HOSTNAME` take over `b@HOSTNAME`: | ||
|
||
``` | ||
07:39:49.820 [info] a@manticore is taking over b@manticore | ||
``` | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{kernel, | ||
[{distributed, | ||
[{chucky, | ||
5000, | ||
[a@manticore, {b@manticore, c@manticore}]}]}, | ||
{sync_nodes_mandatory, [b@manticore, c@manticore]}, | ||
{sync_nodes_timeout, 30000} | ||
]}]. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{kernel, | ||
[{distributed, | ||
[{chucky, | ||
5000, | ||
[a@manticore, {b@manticore, c@manticore}]}]}, | ||
{sync_nodes_mandatory, [a@manticore, c@manticore]}, | ||
{sync_nodes_timeout, 30000} | ||
]}]. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{kernel, | ||
[{distributed, | ||
[{chucky, | ||
5000, | ||
[a@manticore, {b@manticore, c@manticore}]}]}, | ||
{sync_nodes_mandatory, [a@manticore, b@manticore]}, | ||
{sync_nodes_timeout, 30000} | ||
]}]. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for | ||
# 3rd-party users, it should be done in your "mix.exs" file. | ||
|
||
# You can configure for your application as: | ||
# | ||
# config :chucky, key: :value | ||
# | ||
# And access this configuration in your application as: | ||
# | ||
# Application.get_env(:chucky, :key) | ||
# | ||
# Or configure a 3rd-party app: | ||
# | ||
# config :logger, level: :info | ||
# | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env}.exs" |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Chucky do | ||
use Application | ||
require Logger | ||
|
||
def start(type, _args) do | ||
import Supervisor.Spec | ||
children = [ | ||
worker(Chucky.Server, []) | ||
] | ||
|
||
case type do | ||
:normal -> | ||
Logger.info("Application is started on #{node}") | ||
|
||
{:takeover, old_node} -> | ||
Logger.info("#{node} is taking over #{old_node}") | ||
|
||
{:failover, old_node} -> | ||
Logger.info("#{old_node} is failing over to #{node}") | ||
end | ||
|
||
opts = [strategy: :one_for_one, name: {:global, Chucky.Supervisor}] | ||
Supervisor.start_link(children, opts) | ||
end | ||
|
||
def fact do | ||
Chucky.Server.fact | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Chucky.Server do | ||
use GenServer | ||
|
||
####### | ||
# API # | ||
####### | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, [], [name: {:global, __MODULE__}]) | ||
end | ||
|
||
def fact do | ||
GenServer.call({:global, __MODULE__}, :fact) | ||
end | ||
|
||
############# | ||
# Callbacks # | ||
############# | ||
|
||
def init([]) do | ||
:random.seed(:os.timestamp) | ||
facts = "facts.txt" | ||
|> File.read! | ||
|> String.split("\n") | ||
|
||
{:ok, facts} | ||
end | ||
|
||
def handle_call(:fact, _from, facts) do | ||
random_fact = facts | ||
|> Enum.shuffle | ||
|> List.first | ||
|
||
{:reply, random_fact, facts} | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Chucky.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[app: :chucky, | ||
version: "0.0.1", | ||
elixir: "~> 1.1", | ||
build_embedded: Mix.env == :prod, | ||
start_permanent: Mix.env == :prod, | ||
deps: deps] | ||
end | ||
|
||
def application do | ||
[applications: [:logger], | ||
# registered: [Chucky, Chucky.Supervisor, Chucky.Server], | ||
mod: {Chucky, []}] | ||
end | ||
|
||
defp deps do | ||
[] | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule ChuckyTest do | ||
use ExUnit.Case | ||
doctest Chucky | ||
|
||
test "the truth" do | ||
assert 1 + 1 == 2 | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |