Skip to content

Commit

Permalink
Oban improvements
Browse files Browse the repository at this point in the history
- Only install in prod and dev
- Upgrade to 2.10 (RC for now)
- Use new resolver 'forbidden' feature
- Remove no-longer-needed plugins
- Only load ObanWeb code when installed
  • Loading branch information
jerodsanto committed Sep 25, 2023
1 parent 76cae6b commit af98b6d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
6 changes: 1 addition & 5 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ config :changelog,
config :changelog, Oban,
repo: Changelog.Repo,
queues: [audio_updater: 10, scheduled: 5, email: 1, feeds: 1],
plugins: [
Oban.Plugins.Pruner,
Oban.Plugins.Gossip,
Oban.Web.Plugins.Stats
]
plugins: [Oban.Plugins.Pruner]

config :changelog, Changelog.Mailer, adapter: Bamboo.LocalAdapter

Expand Down
17 changes: 0 additions & 17 deletions lib/changelog_web/oban_resolver.ex

This file was deleted.

42 changes: 42 additions & 0 deletions lib/changelog_web/oban_web.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule ChangelogWeb.ObanWeb do
@moduledoc """
Simple macro to conditionally load Oban.Web only if already loaded. This
allows us to include it only in the production release and hence make it a lot
easier on potential open source contributors, avoiding the problem of sharing
the oban key and/or them hacking the code to get it working
Thanks to the team at Glific for showing us the way on this.
"""

defmacro __using__(_) do
if Code.ensure_loaded?(Oban.Web.Router) do
defmodule Resolver do
@behaviour Oban.Web.Resolver

@impl true
def resolve_user(conn) do
conn.assigns.current_user
end

@impl true
def resolve_access(user) do
if Changelog.Policies.AdminsOnly.index(user) do
:all
else
{:forbidden, "/in"}
end
end
end

quote do
import Oban.Web.Router

scope "/admin" do
pipe_through [:browser, :admin]

oban_dashboard("/oban", resolver: ChangelogWeb.ObanWeb.Resolver)
end
end
end
end
end
5 changes: 1 addition & 4 deletions lib/changelog_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule ChangelogWeb.Router do
use ChangelogWeb, :router

import Oban.Web.Router
use ChangelogWeb.ObanWeb

alias ChangelogWeb.Plug

Expand Down Expand Up @@ -62,8 +61,6 @@ defmodule ChangelogWeb.Router do
scope "/admin", ChangelogWeb.Admin, as: :admin do
pipe_through [:browser, :admin]

oban_dashboard "/oban", resolver: ChangelogWeb.ObanResolver

get "/", PageController, :index
get "/downloads", PageController, :downloads
get "/fresh_requests", PageController, :fresh_requests
Expand Down
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule Changelog.Mixfile do
use Mix.Project

# Change this to [:prod] if you don't have a web license available
@oban_envs [:dev, :prod]

Code.compile_file("config/secret_or_env.exs")

def project do
Expand Down Expand Up @@ -44,7 +47,7 @@ defmodule Changelog.Mixfile do
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:plug_cowboy, "~> 2.5"},
{:oban, "~> 2.15"},
{:oban_web, "~> 2.9", repo: "oban"},
{:oban_web, "~> 2.10.0-rc.2", repo: "oban", only: @oban_envs},
{:timex, "~> 3.0"},
{:scrivener_ecto, "~> 2.0"},
{:scrivener_html, "~> 1.8", github: "jerodsanto/scrivener_html", branch: "phx-1-7"},
Expand Down
Loading

0 comments on commit af98b6d

Please sign in to comment.