From 2a1f17e3ede2054f31c35b5dc240f51d0d5509d1 Mon Sep 17 00:00:00 2001 From: floatingghost Date: Mon, 14 Nov 2022 15:07:26 +0000 Subject: [PATCH] and i yoink (#275) Co-authored-by: Mark Felder Co-authored-by: FloatingGhost Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/275 --- lib/pleroma/web/federator.ex | 6 ++- lib/pleroma/workers/backup_worker.ex | 2 +- lib/pleroma/workers/purge_expired_activity.ex | 2 +- lib/pleroma/workers/purge_expired_filter.ex | 2 +- lib/pleroma/workers/purge_expired_token.ex | 2 +- lib/pleroma/workers/worker_helper.ex | 2 +- .../pleroma/workers/publisher_worker_test.exs | 52 +++++++++++++++++++ 7 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 test/pleroma/workers/publisher_worker_test.exs diff --git a/lib/pleroma/web/federator.ex b/lib/pleroma/web/federator.ex index 770044de2..3a00424c6 100644 --- a/lib/pleroma/web/federator.ex +++ b/lib/pleroma/web/federator.ex @@ -48,7 +48,9 @@ def publish(%{id: "pleroma:fakeid"} = activity) do @impl true def publish(%{data: %{"object" => object}} = activity) when is_binary(object) do - PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil}) + PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil}, + priority: publish_priority(activity) + ) end @impl true @@ -63,7 +65,7 @@ def publish(%{data: %{"object" => object}} = activity) when is_map(object) or is ) end - defp publish_priority(%{type: "Delete"}), do: 3 + defp publish_priority(%{data: %{"type" => "Delete"}}), do: 3 defp publish_priority(_), do: 0 # Job Worker Callbacks diff --git a/lib/pleroma/workers/backup_worker.ex b/lib/pleroma/workers/backup_worker.ex index 4ab08706e..cf78f1cb9 100644 --- a/lib/pleroma/workers/backup_worker.ex +++ b/lib/pleroma/workers/backup_worker.ex @@ -16,7 +16,7 @@ def process(backup, admin_user_id \\ nil) do @impl Oban.Worker def timeout(_job) do - Pleroma.Config.get([:workers, :timeout, :backup]) || :timer.minutes(1) + Pleroma.Config.get([:workers, :timeout, :backup], :timer.minutes(1)) end def schedule_deletion(backup) do diff --git a/lib/pleroma/workers/purge_expired_activity.ex b/lib/pleroma/workers/purge_expired_activity.ex index ece84df37..652e1d6b1 100644 --- a/lib/pleroma/workers/purge_expired_activity.ex +++ b/lib/pleroma/workers/purge_expired_activity.ex @@ -29,7 +29,7 @@ def enqueue(args) do @impl Oban.Worker def timeout(_job) do - Pleroma.Config.get([:workers, :timeout, :activity_expiration]) || :timer.minutes(1) + Pleroma.Config.get([:workers, :timeout, :activity_expiration], :timer.minutes(1)) end @impl true diff --git a/lib/pleroma/workers/purge_expired_filter.ex b/lib/pleroma/workers/purge_expired_filter.ex index dd6a22a68..593380d13 100644 --- a/lib/pleroma/workers/purge_expired_filter.ex +++ b/lib/pleroma/workers/purge_expired_filter.ex @@ -26,7 +26,7 @@ def enqueue(args) do @impl Oban.Worker def timeout(_job) do - Pleroma.Config.get([:workers, :timeout, :filter_expiration]) || :timer.minutes(1) + Pleroma.Config.get([:workers, :timeout, :filter_expiration], :timer.minutes(1)) end @impl true diff --git a/lib/pleroma/workers/purge_expired_token.ex b/lib/pleroma/workers/purge_expired_token.ex index 1773aeff9..b4db84f4e 100644 --- a/lib/pleroma/workers/purge_expired_token.ex +++ b/lib/pleroma/workers/purge_expired_token.ex @@ -21,7 +21,7 @@ def enqueue(args) do @impl Oban.Worker def timeout(_job) do - Pleroma.Config.get([:workers, :timeout, :token_expiration]) || :timer.minutes(1) + Pleroma.Config.get([:workers, :timeout, :token_expiration], :timer.minutes(1)) end @impl true diff --git a/lib/pleroma/workers/worker_helper.ex b/lib/pleroma/workers/worker_helper.ex index 97c0e4e5c..4c0a55774 100644 --- a/lib/pleroma/workers/worker_helper.ex +++ b/lib/pleroma/workers/worker_helper.ex @@ -47,7 +47,7 @@ def enqueue(op, params, worker_args \\ []) do @impl Oban.Worker def timeout(_job) do queue_atom = String.to_atom(unquote(queue)) - Config.get([:workers, :timeout, queue_atom]) || :timer.minutes(1) + Config.get([:workers, :timeout, queue_atom], :timer.minutes(1)) end end end diff --git a/test/pleroma/workers/publisher_worker_test.exs b/test/pleroma/workers/publisher_worker_test.exs new file mode 100644 index 000000000..cf0ac0ccb --- /dev/null +++ b/test/pleroma/workers/publisher_worker_test.exs @@ -0,0 +1,52 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Workers.PublisherWorkerTest do + use Pleroma.DataCase, async: true + use Oban.Testing, repo: Pleroma.Repo + + import Pleroma.Factory + + alias Pleroma.Object + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Builder + alias Pleroma.Web.CommonAPI + alias Pleroma.Web.Federator + + describe "Oban job priority:" do + setup do + user = insert(:user) + + {:ok, post} = CommonAPI.post(user, %{status: "Regrettable post"}) + object = Object.normalize(post, fetch: false) + {:ok, delete_data, _meta} = Builder.delete(user, object.data["id"]) + {:ok, delete, _meta} = ActivityPub.persist(delete_data, local: true) + + %{ + post: post, + delete: delete + } + end + + test "Deletions are lower priority", %{delete: delete} do + assert {:ok, %Oban.Job{priority: 3}} = Federator.publish(delete) + end + + test "Creates are normal priority", %{post: post} do + assert {:ok, %Oban.Job{priority: 0}} = Federator.publish(post) + end + end + + describe "Oban job timeout" do + test "should have a timeout" do + clear_config([:workers, :timeout, :federator_outgoing], :timer.minutes(2)) + assert Pleroma.Workers.PublisherWorker.timeout(nil) == :timer.minutes(2) + end + + test "should use a default timeout if none specified" do + clear_config([:workers, :timeout, :federator_outgoing]) + assert Pleroma.Workers.PublisherWorker.timeout(nil) == :timer.seconds(10) + end + end +end