Backstage

Backstage is a simple Elixir background job processing library backed by PostgreSQL.

Usage

defmodule MyApp.Jobs.WelcomeNotification do
  use Backstage.Job, repo: MyApp.Repo

  def run(%{user_id: user_id}) do
    user = Repo.get!(MyApp.User, user_id)
    UserEmail.welcome(user) |> Mailer.deliver!
  end
end

iex> {:ok, job} = MyApp.Jobs.WelcomeNotification.new(%{user_id: user.id}) |> Repo.insert

Installation

If available in Hex, the package can be installed as:

  1. Add backstage to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:backstage, "~> 0.1.0"}]
end
```
  1. Ensure backstage is started before your application:
```elixir
def application do
  [applications: [:backstage]]
end
```
  1. Start the backstage supervisor:
```elixir
def start(_type, _args) do
  import Supervisor.Spec

  children = [
    supervisor(Backstage.Supervisor, [])
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end
```

TODO

  • Logging
  • #1 (comment)
  • Currently polling Postgres for new jobs every X ms. Could we use LISTEN/NOTIFY?
  • Think about the implications of gen_stage events buffering
  • Make it possible to destroy the job as part of a transaction in "userland"
  • Retries/backoff
  • Vacuum the failed jobs?
  • UI to list jobs, failures, retry, etc.
    • Separate package
  • scheduler
  • benchmarking