Enable maintenance mode for your Plug based Elixir applications.
Plug.OnMaintenance, an Elixir Plug, is used to disable access to your application for some length of time. Putting application in maintenance mode can be done programmatically or via mix tasks.
For whatever reason, you want your Plug based Elixir application to be in maintenance mode for some length of time. Plug.OnMaintenance is what you need.
Add on_maitnenance to your project dependencies in mix.exs:
defp deps do
[
...
{:on_maintenance, "~> 0.7.0"}
]
endand do
mix deps.getIn case of error, you may want to do
mix deps.update --allNow that you have on_maintenance as your dependency, plug Plug.OnMaintenance in your router.ex. Let us say we have a Phoenix application.
pipeline :api do
plug Plug.OnMaintenance
# ...
endThen run this mix task
mix maintenance.init_config_store # creates sqlite db and add initial state of application (which is "not in maintenance mode")Then run the application
mix phoenix.server # just in localYou can enable/disable maintenance mode for your application via mix tasks below
mix maintenance.enable
mix maintenance.disableOr programmatically using these convenience methods below
import Plug.OnMaintenance.Util
on_maintenance?() # will check if application is in maintenance mode
enable_maintenance() # put application in maintenance mode
disable_maintenance() # disable maintenance mode503 response code should have a retry-after header.
You can enable maintenance mode for your application and can
specify how long it would take via --retry-after option!
mix maintenance.enable --retry-after=300 # application will be on maintenance for 5 minutes.When in maintenance mode, your application will respond 503 to all http requests. The default message is "application on scheduled maintenance.". Examples:
<html>
<body>Application on scheduled maintenance.</body>
</html>{"message": "Application on scheduled maintenance."}Application on scheduled maintenance.
Default message can be updated via config.exs
use Mix.Config
# ...
config :on_maintenance,
message: "Service is currently in maintenance mode. Give us few minutes. Thanks!"

