An Elixir plug returns a service unavailable response during maintenance
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
lib
test
.gitignore
.travis.yml
README.md
mix.exs
mix.lock

README.md

Build Status hex.pm version hex.pm

PlugMaintenance

An Elixir plug that returns a service unavailable response during maintenance.

Installation

  1. Add plug_maintenance to your list of dependencies in mix.exs:
def deps do
  [{:plug_maintenance, "~> 0.1.0"}]
end
  1. Ensure plug_maintenance is started before your application:
def application do
  [applications: [:plug_maintenance]]
end

Usage

  • Add the Maintenance plug to the router.
pipeline :api do
  plug :accepts, ["json"]
  plug Maintenance
end
scope "/" do
  pipe_through :api
  get "/v1/foo/bar, FooController, :bar
end
  • By default, it checks whether the environment variable MAINTENANCE is true or whether there is a file named maintenance and returns a service unavailable message with json, but if you want to customize it, please give your module for checking the maintenance status as an option.
plug Maintenance, monitor: {MyApp.MaintenanceMonitor, :check, []}, renderer: {MyApp.MaintenanceRenderer, :render, []}

Custom monitor can be written like this.

defmodule MyApp.MaintenanceMonitor do
  def check(conn, _opts) do
    # Returning true will result in maintenance mode
    # Write the logic for your environment
    Redix.command(~w(GET maintenance)) == {:ok, "true"}
  end
end

For Phoenix, renderer can be written like this.

defmodule MyApp.MaintenanceRenderer do
  use MyApp.Web, :view
  import Plug.Conn

  def render(conn, _opts) do
    conn
    |> put_status(:service_unavailable)
    |> put_view(__MODULE__)
    |> render("index.html", layout: {MyApp.LayoutView, "maintenance.html"})
    |> halt
  end
end
  • If you want to limit the range to controller or action, add it to controller as follows.
plug Maintenance
plug Maintenance when action in [:index, :show:, :update]
plug Maintenance, [monitor: {MyMonitor, :check, []}, renderer: {MyRenderer, :render, []}] when action in [:index, :show:, :update]