Browse Source

feat: replace que with oban :)

pull/26/head
Inhji Y. 1 year ago
parent
commit
e70702627e
  1. 9
      apps/bookmarks/lib/bookmarks/worker.ex
  2. 2
      apps/bookmarks/mix.exs
  3. 13
      apps/db/priv/repo/migrations/20200420190150_add_oban_jobs_table.exs
  4. 18
      apps/tomie/lib/tomie/application.ex
  5. 1
      apps/tomie/mix.exs
  6. 4
      apps/tomie_web/lib/tomie_web/live/bookmark_live/new.ex
  7. 13
      apps/tomie_web/lib/tomie_web/worker.ex
  8. 1
      apps/tomie_web/mix.exs
  9. 5
      config/config.exs
  10. 5
      config/test.exs
  11. 1
      mix.lock

9
apps/bookmarks/lib/bookmarks/worker.ex

@ -1,8 +1,11 @@
defmodule Bookmarks.Worker do
use Que.Worker, concurrency: 4
use Oban.Worker, queue: :default
def perform(%Bookmarks.Bookmark{source: source} = bookmark) do
with {:ok, html} <- Scraper.get_html(source),
@impl Oban.Worker
def perform(%{"id" => id}, _job) do
with bookmark <- Bookmarks.get_bookmark!(id),
source <- Map.get(bookmark, :source),
{:ok, html} <- Scraper.get_html(source),
{:ok, result} <- Scraper.parse(html),
{:ok, updated_bookmark} <- Bookmarks.update_bookmark(bookmark, %{title: result.title}) do
{:ok, updated_bookmark} =

2
apps/bookmarks/mix.exs

@ -27,7 +27,7 @@ defmodule Bookmarks.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:que, "~> 0.10.1"},
{:oban, "~> 1.2"},
{:earmark, "~> 1.4"},
{:ecto_sql, "~> 3.1"},
{:phoenix_pubsub, "~> 2.0"},

13
apps/db/priv/repo/migrations/20200420190150_add_oban_jobs_table.exs

@ -0,0 +1,13 @@
defmodule Db.Repo.Migrations.AddObanJobsTable do
use Ecto.Migration
def up do
Oban.Migrations.up()
end
# We specify `version: 1` in `down`, ensuring that we'll roll all the way back down if
# necessary, regardless of which version we've migrated `up` to.
def down do
Oban.Migrations.down(version: 1)
end
end

18
apps/tomie/lib/tomie/application.ex

@ -6,8 +6,24 @@ defmodule Tomie.Application do
use Application
def start(_type, _args) do
children = []
children = [
{Oban, oban_config()}
]
Supervisor.start_link(children, strategy: :one_for_one, name: Tomie.Supervisor)
end
defp oban_config do
opts = Application.get_env(:tomie, Oban)
# Prevent running queues or scheduling jobs from an iex console.
# if Code.ensure_loaded?(IEx) and IEx.started?() do
# opts
# |> Keyword.put(:crontab, false)
# |> Keyword.put(:queues, false)
# else
# opts
# end
opts
end
end

1
apps/tomie/mix.exs

@ -37,6 +37,7 @@ defmodule Tomie.MixProject do
# Type `mix help deps` for examples and options.
defp deps do
[
{:oban, "~> 1.2"},
{:ecto_sql, "~> 3.1"},
{:jason, "~> 1.0"},
{:pow, "~> 1.0.19"},

4
apps/tomie_web/lib/tomie_web/live/bookmark_live/new.ex

@ -34,7 +34,9 @@ defmodule TomieWeb.BookmarkLive.New do
case Bookmarks.create_bookmark(params) do
{:ok, bookmark} ->
Bookmarks.set_tags(tags, bookmark)
TomieWeb.Worker.run(bookmark)
Bookmarks.Worker.new(%{id: bookmark.id})
|> Oban.insert()
{:noreply,
push_redirect(socket, to: Routes.live_path(socket, BookmarkLive.Show, bookmark))}

13
apps/tomie_web/lib/tomie_web/worker.ex

@ -1,13 +0,0 @@
defmodule TomieWeb.Worker do
def run(entity) do
disable_workers = Application.get_env(:tomie_web, :disable_workers, false)
unless disable_workers do
do_run(entity)
end
end
defp do_run(%Bookmarks.Bookmark{} = bookmark) do
Que.add(Bookmarks.Worker, bookmark)
end
end

1
apps/tomie_web/mix.exs

@ -51,7 +51,6 @@ defmodule TomieWeb.MixProject do
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.1"},
{:pow, "~> 1.0.19"},
{:que, "~> 0.10.1"},
{:excoveralls, "~> 0.12.3", only: :test},
{:timex, "~> 3.5"},
{:telemetry_poller, "~> 0.4"},

5
config/config.exs

@ -16,6 +16,11 @@ config :db,
config :tomie,
user_agent: "Tomie/0.x (https://inhji.de)"
config :tomie, Oban,
repo: Db.Repo,
prune: {:maxlen, 10_000},
queues: [default: 10]
config :tomie_web,
ecto_repos: [Db.Repo],
generators: [context_app: :tomie]

5
config/test.exs

@ -1,5 +1,10 @@
use Mix.Config
config :tomie, Oban,
crontab: false,
queues: false,
prune: :disabled
# Configure your database
config :db, Db.Repo,
username: "postgres",

1
mix.lock

@ -35,6 +35,7 @@
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mock": {:hex, :mock, "0.3.4", "c5862eb3b8c64237f45f586cf00c9d892ba07bb48305a43319d428ce3c2897dd", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "e6d886252f1a41f4ba06ecf2b4c8d38760b34b1c08a11c28f7397b2e03995964"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
"oban": {:hex, :oban, "1.2.0", "7cca94d341be43d220571e28f69131c4afc21095b25257397f50973d3fc59b07", [:mix], [{:ecto_sql, "~> 3.1", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ba5f8b3f7d76967b3e23cf8014f6a13e4ccb33431e4808f036709a7f822362ee"},
"open_graph_extended": {:git, "https://git.inhji.de/inhji/open_graph.git", "bed84f89524ec5ba12ab85018ff6f166cfff7a3d", []},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"phoenix": {:hex, :phoenix, "1.5.0-rc.0", "383ba0e00fc78c05fe9a78505a16f868123eb287ad1e0fce22ce9ec7a32cacef", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.1", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1c37b18d7dca8b4e45884274c65f1479c8e80c840099cad065b4259667af9366"},

Loading…
Cancel
Save