Building a Domain Model in Elixir Part 2

I have now got a bit further with my domain model of the Pandemic game. There are very few complex examples of functional domain models. The problem is that problems that benefit from this approach are normally too complex to make a good example.

Pandemic is a complex cooperative board game. Players race to find cures for 4 diseases. There are lots of ways to lose. It’s a Euro game so it has built in termination options. You lose if:

– You can’t draw from the player deck

– On the 8th outbreak

– If you run out of any one of the 4 disease counters and are unable to place one.

The team wins if all 4 diseases have a cure.

https://github.com/chriseyre2000/pandemic

This now has a model of the cities with the links between them. The this was test driven with some useful checks. I used exhaustive property tests to check the link validity. This includes checks that all links are reversable, that no city links to itself and that there are no orphan cities. These caught a number of cut and paste errors.

The next part is a model of the board state. This includes the initial draw of cards, the infection of new cities and the cascade of an outbreak.

This follows an Elixir pattern of using functions in modules using struts. Every non-query function takes and returns the struct defined in the module. This makes composition much easier.

Installing Lumen

Lumen is an alternative BEAM Implementation.
Warning that this is still at a very early stage. It promises the ability to build WebAssembly

I am not currently interested in using Rust to build it from sources, but there are now images published here

https://github.com/lumen/lumen/releases

I used the following to install the tarball (I use a mac):

sudo tar xf lumen-0.1.0-nightly-x86_64-apple-darwin.tar.gz -C /opt

This deploys the bundle to: /opt/lumen

The compiler is now available at: /opt/lumen/bin/lumen

Here are more details on lumen: https://getlumen.org/

On having a Useless Superpower

I am going to admit to having a minor superpower. I can hear high pitch sounds that most people can’t.

This is not at all useful as it results in me getting a headache in lots of shops or museums (and in one especially annoying instance a job interview). It’s typically a 50hz signal.

Minimalist Clustering Demo

This is based on code from a video that I have previously linked to.
However Elixir has made a few breaking changes since then.

This is going to demonstrate how to cluster two local Elixir nodes, deploy code from one to the other and then upgrade it while it is running.

You need to put the following code in a file called blabber.ex

defmodule Blabber do

  def start do

    spawn(fn -> loop(0) end)

  end

  def loop(uptime) do

    receive do

      :stop ->

        IO.puts "Shutting blabber down ..."

        exit(:normal)

      after 1000 ->

        IO.puts "Nice! #{uptime} seconds of bgu-free uptime on #{node()}."

    end

    Blabber.loop(uptime + 1)

  end

end

Next you need to start iex with a name and a cookie (in the same folder as blabber.ex):

iex --sname cat --cookie super-secret

In a second terminal start another iex session

iex --sname dog --cookie super-secret

From the first terminal connect the two nodes together:

Node.connect :"dog@MacBook-Pro"

Compile the code in the cat node:

c ("blabber.ex", ".")

We need to use this version of the compile function as by default since Elixir 1.5 the beam file is not output by default.

The process can be started on the cat node with:

Blabber.start

Now you can transfer the compiled beam file to all linked nodes using:

nl Blabber

Now from the dog node you can start the process:

Blabber.start

At this point we notice the typo in the code. It say bgu-free rather than bug-free.
Edit the source file save the change.

On the cat node recompile this:

c ("blabber.ex", ".")

This will immediately fix the problem on the cat node (and leave the count updating).

If you use:

nl Blabber

then the solution will be moved to the other node as well.

This demonstrates the simplest possible Clustering arrangement. It is simple to extend this to a move sophisticated service.