Skip to content

Maintenance mode for Plug based Elixir applications.

License

Notifications You must be signed in to change notification settings

wnuqui/on_maintenance

Repository files navigation

Plug.OnMaintenance

Build Status Inline docs Release Hex Version

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.

Contents

Installation

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"}
  ]
end

and do

mix deps.get

In case of error, you may want to do

mix deps.update --all

Setup

Now 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
  # ...
end

Then 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 local

You can enable/disable maintenance mode for your application via mix tasks below

mix maintenance.enable
mix maintenance.disable

Or 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 mode

retry-after response header

503 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.

Example 503 responses (default)

When in maintenance mode, your application will respond 503 to all http requests. The default message is "application on scheduled maintenance.". Examples:

text/html response

<html>
  <body>Application on scheduled maintenance.</body>
</html>

application/json response

{"message": "Application on scheduled maintenance."}

text/plain response

Application on scheduled maintenance.

Custom 503 Message

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!"