How to perform Hot Code Swapping using Distillery— #2 — A (Live Demo) GenServer State update.

Blackode
Blackode
Jan 10 · 8 min read
Photo by rawpixel on Unsplash

Brief Intro about Distillery

Create a new mix project

$ mix new hotcode
$ cd hotcode
#file: mix.exsdefp deps do
[{:distillery, "~> 2.0"}]
end
$ mix deps.get
defmodule DemoServer do
use GenServer
@vsn "1"
## Client API def start_link employee do
GenServer.start_link __MODULE__, employee, []
end
def add_money(pid, value) do
GenServer.call(pid, {:add, value})
end
## Server API def init(employee) do # initiating state with passed employee details
{:ok, employee}
end
# add the value to the state and returns :ok def handle_call({:add, value},_from, %{name: name, money: money} = state) do
{:reply, "#{value} added to #{name} ", Map.put(state, :money, money+value)}
end
end

Releasing first version

$ mix release
$ mix release.init
environment :prod do
set cookie: :crypto.hash(:sha256, System.get_env("COOKIE")) |> Base.encod16(:lower) |> String.to_atom
........
end
release :hotcode do
set version: current_version(:hotcode)
set applications: [
:runtime_tools
]
end
release :hotcode2 do
set version: "2"
set applications: [
:runtime_tools
]
end
$ mix release
default_release: :default
$ MIX_ENV=prod mix release

Starting Releases

$ cd _build/prod/rel/hotcode/bin
$ ./hotcode console
iex(hotcode@127.0.0.1)1> {:ok, pid} = DemoServer.start_link %{name: "blackode", money: 3_00_000}
{:ok, #PID<0.773.0>}
iex(hotcode@127.0.0.1)3> DemoServer.add_money pid, 400000
"400000 added to blackode "
iex(hotcode@127.0.0.1)4> :sys.get_state pid
%{money: 700000, name: "blackode"}
$ ./hotcode start
$ ./hotcode attach
Attaching to /home/john/mycode/elixir/hotcode/_build/prod/rel/hotcode/var/erl_pipes/hotcode@127.0.0.1/erlang.pipe.1 (^D to exit)
iex(hotcode@127.0.0.1)1> {:ok, pid} = DemoServer.start_link %{name: "blackode", money: 34000}
{:ok, #PID<0.773.0>}

Code Deployment

Copying tarball to remote system

$ cd  _build/prod/rel/hotcode/releases
$ cd 0.1.0
$ scp hotcode.tar.gz user@host:/home/user/.

Extracting the tarball in your remote system

ssh user@host
use@host$ mkdir hotcode
use@host$ tar -xzcf ./hotcode.tar.gz --directory hotcode

Start at the remote system

use@host$ cd hotcode/bin
use@host$ ./hotcode start
$ MIX_ENV=prod mix release --upgrade

Copying the upgraded tarball

Upgrading the release 0.2.0

use@host$ cd hotcode/binuse@host$ ./hotcode stop
use@host$ ./hotcode start
user@host$ ./hotcode upgrade "0.2.0"
iex(user@host)1> {:ok, pid} = DemoServer.start_link %{name: "blackode", salary: 50000}
{:ok, #PID<0.773.0>}
iex(hotcode@127.0.0.1)6> DemoServer.add_money pid, 50000
%{name: "blackode", salary: 100000}
# In the previous it returns a string, now it gives you the map as the state got updated.
iex(hotcode@127.0.0.1)7> :sys.get_state pid
%{name: "blackode", salary: 100000}

Conclusion

https://t.me/blackoders
if worth_clapping, do: “clap”, else: nil

Happy Coding :)

blackode

Coding, thoughts, and ideas.

Blackode

Written by

Blackode

Wrong CODE takes U to the right BUG 🐞 coding, thoughts & ideas

blackode

blackode

Coding, thoughts, and ideas.