In this third and final part of this series, we will look at what happens during an application upgrade.
Let's get going!
Set-up: Create an Appup File in Elixir
As with a single module, we need new compiled code for a fresh version of an application.
But an application can consist of many modules and have running processes. So we need a scenario to specify what to upgrade and how to do it.
These scenarios are called application upgrade files (.appup files).
Let's try to make an appup file for our application. Imagine that we want to upgrade our counters so that we can specify counter increment speed.
Prepare a New Version of the Application in Elixir
First, we'll add our application to git and tag the old version:
Now we should make a new version.
In mix.exs
, update the version to 0.1.1
:
We want interval
to be an external parameter, not a module attribute. Make the following changes in lib/our_new_app/counter.ex
:
Now add a code_change
callback to the same file:
And change supervisor specs in lib/our_new_app/counter_sup.ex
:
Let's construct our_new_app.appup
file.
We need to update our supervision specs and pass a new tick interval (250) to change_code
callback:
Note that this file is written in Erlang syntax.
Now tag your new version:
Try to upgrade 0.1.0
to 0.1.1
.
Checkout, compile, and run 0.1.0
version:
In a separate shell and different directory, checkout the new version and put the appup file in the appropriate place:
Run the Application Upgrade in Elixir
We are ready to upgrade the application. In the running iex session, do the following:
You can see that the old application is running.
Run an "orphaned" counter outside the supervision tree (we will need it later):
Check that your appup file is correct and the OTP knows how to upgrade your application:
Check your counter processes and their pids:
Upgrade the application!
Let's see what happened:
Our upgrade process was successfully completed.
You can see that:
- The new version of the application is now running.
- Our child counters are alive — they updated their state and are functioning.
- From the internal state of
OurNewApp.CounterSup
, we see those child specifications for our counters updated too! Now, if they die, they will properly restart.
But what about the error GenServer #PID<0.147.0> terminating
?
Recall that
#PID<0.147.0>
is the pid of our "orphaned" counter, which was running outside of the supervision tree. As the application upgrade process traverses the supervision tree and updates processes,
the "orphaned" counter's state was not updated. But the code of the OurNewApp.Counter
did update, so the "orphaned" counter process died: new code met its old state.
We've seen how to upgrade a single running application.
We needed only two special tools for that: an .appup
file and :release_handler.upgrade_app/2
function.
It was also crucial for us to follow OTP principles.
Wrap-up and What to Learn Next
I hope you've enjoyed this whirlwind ride through production code upgrades in Elixir! We started with my guide to hot code reloading, followed by the best use of supervisors when building applications.
This final article has demonstrated how following OTP principles can show us the way to powerful application code upgrades.
It's worth noting that the application code upgrade I've demonstrated here still has disadvantages. The critical issue is that if the whole OS beam process restarts, this will load our application's old code (unless we take some action).
How can we handle this potential problem, I hear you ask? With so-called release upgrades. This awesome article from 'Learn you some Erlang' is a good starting point to dive into release upgrades.
Thanks again for taking the time to read this series — and happy coding!
P.S. If you'd like to read Elixir Alchemy posts as soon as they get off the press, subscribe to our Elixir Alchemy newsletter and never miss a single post!