Simplifying Hot-Upgrades for Elixir Applications
Jellyfish is a library that generates appup files, enabling hot-upgrades for Elixir applications without downtime.
Appup files describe how to upgrade and downgrade an application from one version to another. They contain:
- The application name
- Instructions to upgrade to a newer version
- Instructions to downgrade to the original version
For detailed information about the appup format, see the Erlang appup manual.
When upgrading processes, order matters:
- Processes are suspended during upgrades
- In-flight requests are handled by the old version until upgrade completes
- Upgrade dependencies first, then dependents (e.g., if
proc_adepends onproc_b, upgradeproc_bfirst) - Jellyfish automatically performs topological sorting when generating appups
References:
Add jellyfish to your dependencies in mix.exs:
def deps do
[
{:jellyfish, "~> 0.2.0"}
]
endAdd the following lines in the mix.exs project:
def project do
[
...
releases: [
your_app_name: [
steps: [:assemble, &Jellyfish.generate/1, :tar]
]
],
...
]
endOnce the mix release is called, if a previous version is found, the appup file will be automatically generated and included in the release package for executing hot-upgrades.
Jellyfish generates appup files for both your application code and its dependencies. This allows you to upgrade third-party libraries at runtime alongside your own code changes.
Warning
Not all code changes are safe for hot-upgrades. Before performing a hot-upgrade, Check if the dependency supports hot-upgrades between versions, review the changelog for structural changes (e.g., process state modifications, API changes), check stateful processes like GenServers, Agents, etc that may require special handling.
If for any reason you need to change the order of the modules or add new commands in the appup file, you have 2 options:
- Use the EDIT_APPUP environment variable to indicate to Jellifish that you want to edit the file before the release:
EDIT_APPUP=true MIX_ENV=prod mix release- Untar the release, do the changes in the appup files and tar it again.
The library focuses on generating appup files and includes them in the mix release package. It doesn't create relup files directly. The relup file is typically created during a hot upgrade with the DeployEx application.
The next sections describe how to set up and use Jellyfish with Elixir umbrella applications for hot code upgrades.
Elixir umbrella applications contain multiple apps, each with its own mix.exs file and version. However, Jellyfish expects a single consistent version for the entire umbrella application. To ensure version consistency across all apps, we recommend two approaches:"
Create a new file mix/shared.exs at the root of your umbrella project and add the following code:
defmodule Mix.Shared do
def version, do: "0.1.0"
endAdd the load of this file in the root Mix File Setup:
Code.require_file("mix/shared.exs")
defmodule Myumbrella.MixProject do
use Mix.Project
def project do
[
apps_path: "apps",
version: Mix.Shared.version(),
start_permanent: Mix.env() == :prod,
deps: deps(),
releases: [
myumbrella: [
applications: [
app_1: :permanent,
app_2: :permanent,
app_web: :permanent
],
steps: [:assemble, &Jellyfish.generate/1, :tar]
]
]
...
]
end
defp deps do
[
{:jellyfish, "~> 0.2.0"}
]
end
endEach application within the umbrella should reference the same version file:
Jellyfish dependency is not required for the child apps
defmodule App1.MixProject do
use Mix.Project
def project do
[
app: :app_1,
version: Mix.Shared.version(),
# Other configuration...
]
end
# Rest of the mix file...
endCreate a new file version.txt at the root of your umbrella project.
Root Mix File Setup:
defmodule Myumbrella.MixProject do
use Mix.Project
@version File.read!("version.txt")
def project do
[
apps_path: "apps",
version: @version,
start_permanent: Mix.env() == :prod,
deps: deps(),
releases: [
myumbrella: [
applications: [
app_1: :permanent,
app_2: :permanent,
app_web: :permanent
],
steps: [:assemble, &Jellyfish.generate/1, :tar]
]
]
]
end
defp deps do
[
{:jellyfish, "~> 0.2.0"}
]
end
endEach application within the umbrella should reference the same version file:
Jellyfish dependency is not required for the child apps
defmodule App1.MixProject do
use Mix.Project
@version File.read!("../../version.txt")
def project do
[
app: :app_1,
version: @version,
# Other configuration...
]
end
# Rest of the mix file...
endWhen building releases for hot code upgrades in umbrella applications, modifying the version file does not trigger the compiler to detect changes in mix.exs across all apps. In this scenario, all apps need to be recompiled to make the new version available to the compiler's tasks, which would normally require forcing compilation.
- Build the initial release:
# Release the version 0.1.0
MIX_ENV=prod mix assets.deploy
MIX_ENV=prod mix release- Update the version (e.g., from
0.1.0to0.1.1) inversion.txt - Build the new release with forced compilation:
MIX_ENV=prod mix deps.get # optional, in case you had updated any dependency
MIX_ENV=prod mix assets.deploy
MIX_ENV=prod mix compile --force
MIX_ENV=prod mix releaseExplore these resources for practical examples of using Jellyfish with Elixir applications:
- Deployex - Elixir application showcasing Jellyfish's capabilities in deployment with hot-upgrades.
- Calori - Elixir application using Jellyfish and being able to hot upgrade via DeployEx
- Myumbrella - Elixir umbrella application configured for using Jellyfish.
🗨️ Contact us: Feel free to contact me on Linkedin.
Copyright (c) 2024, Thiago Esteves.
DeployEx source code is licensed under the MIT License.
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/jellyfish.