[#1149] Refactoring: GenServer workers renamed to daemons, use Oban.Worker moved to helper.

This commit is contained in:
Ivan Tashkinov 2019-09-02 14:57:40 +03:00
parent dd017c65a4
commit b49085c156
19 changed files with 20 additions and 65 deletions

View file

@ -54,7 +54,7 @@
scheduled_jobs = scheduled_jobs =
with digest_config <- Application.get_env(:pleroma, :email_notifications)[:digest], with digest_config <- Application.get_env(:pleroma, :email_notifications)[:digest],
true <- digest_config[:active] do true <- digest_config[:active] do
[{digest_config[:schedule], {Pleroma.DigestEmailWorker, :perform, []}}] [{digest_config[:schedule], {Pleroma.Daemons.DigestEmailDaemon, :perform, []}}]
else else
_ -> [] _ -> []
end end

View file

@ -36,8 +36,8 @@ def start(_type, _args) do
Pleroma.Emoji, Pleroma.Emoji,
Pleroma.Captcha, Pleroma.Captcha,
Pleroma.FlakeId, Pleroma.FlakeId,
Pleroma.ScheduledActivityWorker, Pleroma.Daemons.ScheduledActivityDaemon,
Pleroma.ActivityExpirationWorker Pleroma.Daemons.ActivityExpirationDaemon
] ++ ] ++
cachex_children() ++ cachex_children() ++
hackney_pool_children() ++ hackney_pool_children() ++

View file

@ -2,7 +2,7 @@
# Copyright © 2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ActivityExpirationWorker do defmodule Pleroma.Daemons.ActivityExpirationDaemon do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.ActivityExpiration alias Pleroma.ActivityExpiration
alias Pleroma.Config alias Pleroma.Config

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.DigestEmailWorker do defmodule Pleroma.Daemons.DigestEmailDaemon do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Workers.DigestEmailsWorker alias Pleroma.Workers.DigestEmailsWorker

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ScheduledActivityWorker do defmodule Pleroma.Daemons.ScheduledActivityDaemon do
@moduledoc """ @moduledoc """
Sends scheduled activities to the job queue. Sends scheduled activities to the job queue.
""" """

View file

@ -3,11 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.ActivityExpirationWorker do defmodule Pleroma.Workers.ActivityExpirationWorker do
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "activity_expiration",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "activity_expiration" use Pleroma.Workers.WorkerHelper, queue: "activity_expiration"
@impl Oban.Worker @impl Oban.Worker
@ -18,6 +13,6 @@ def perform(
}, },
_job _job
) do ) do
Pleroma.ActivityExpirationWorker.perform(:execute, activity_expiration_id) Pleroma.Daemons.ActivityExpirationDaemon.perform(:execute, activity_expiration_id)
end end
end end

View file

@ -8,11 +8,6 @@ defmodule Pleroma.Workers.BackgroundWorker do
alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy
alias Pleroma.Web.OAuth.Token.CleanWorker alias Pleroma.Web.OAuth.Token.CleanWorker
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "background",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "background" use Pleroma.Workers.WorkerHelper, queue: "background"
@impl Oban.Worker @impl Oban.Worker

View file

@ -5,17 +5,12 @@
defmodule Pleroma.Workers.DigestEmailsWorker do defmodule Pleroma.Workers.DigestEmailsWorker do
alias Pleroma.User alias Pleroma.User
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "digest_emails",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "digest_emails" use Pleroma.Workers.WorkerHelper, queue: "digest_emails"
@impl Oban.Worker @impl Oban.Worker
def perform(%{"op" => "digest_email", "user_id" => user_id}, _job) do def perform(%{"op" => "digest_email", "user_id" => user_id}, _job) do
user_id user_id
|> User.get_cached_by_id() |> User.get_cached_by_id()
|> Pleroma.DigestEmailWorker.perform() |> Pleroma.Daemons.DigestEmailDaemon.perform()
end end
end end

View file

@ -3,11 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.MailerWorker do defmodule Pleroma.Workers.MailerWorker do
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "mailer",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "mailer" use Pleroma.Workers.WorkerHelper, queue: "mailer"
@impl Oban.Worker @impl Oban.Worker

View file

@ -6,11 +6,6 @@ defmodule Pleroma.Workers.PublisherWorker do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Web.Federator alias Pleroma.Web.Federator
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "federator_outgoing",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "federator_outgoing" use Pleroma.Workers.WorkerHelper, queue: "federator_outgoing"
def backoff(attempt) when is_integer(attempt) do def backoff(attempt) when is_integer(attempt) do

View file

@ -5,11 +5,6 @@
defmodule Pleroma.Workers.ReceiverWorker do defmodule Pleroma.Workers.ReceiverWorker do
alias Pleroma.Web.Federator alias Pleroma.Web.Federator
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "federator_incoming",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "federator_incoming" use Pleroma.Workers.WorkerHelper, queue: "federator_incoming"
@impl Oban.Worker @impl Oban.Worker

View file

@ -3,15 +3,10 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.ScheduledActivityWorker do defmodule Pleroma.Workers.ScheduledActivityWorker do
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "scheduled_activities",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities" use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities"
@impl Oban.Worker @impl Oban.Worker
def perform(%{"op" => "execute", "activity_id" => activity_id}, _job) do def perform(%{"op" => "execute", "activity_id" => activity_id}, _job) do
Pleroma.ScheduledActivityWorker.perform(:execute, activity_id) Pleroma.Daemons.ScheduledActivityDaemon.perform(:execute, activity_id)
end end
end end

View file

@ -7,11 +7,6 @@ defmodule Pleroma.Workers.SubscriberWorker do
alias Pleroma.Web.Federator alias Pleroma.Web.Federator
alias Pleroma.Web.Websub alias Pleroma.Web.Websub
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "federator_outgoing",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "federator_outgoing" use Pleroma.Workers.WorkerHelper, queue: "federator_outgoing"
@impl Oban.Worker @impl Oban.Worker

View file

@ -5,11 +5,6 @@
defmodule Pleroma.Workers.TransmogrifierWorker do defmodule Pleroma.Workers.TransmogrifierWorker do
alias Pleroma.User alias Pleroma.User
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "transmogrifier",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "transmogrifier" use Pleroma.Workers.WorkerHelper, queue: "transmogrifier"
@impl Oban.Worker @impl Oban.Worker

View file

@ -6,11 +6,6 @@ defmodule Pleroma.Workers.WebPusherWorker do
alias Pleroma.Notification alias Pleroma.Notification
alias Pleroma.Repo alias Pleroma.Repo
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: "web_push",
max_attempts: 1
use Pleroma.Workers.WorkerHelper, queue: "web_push" use Pleroma.Workers.WorkerHelper, queue: "web_push"
@impl Oban.Worker @impl Oban.Worker

View file

@ -27,6 +27,11 @@ defmacro __using__(opts) do
queue = Keyword.fetch!(opts, :queue) queue = Keyword.fetch!(opts, :queue)
quote do quote do
# Note: `max_attempts` is intended to be overridden in `new/2` call
use Oban.Worker,
queue: unquote(queue),
max_attempts: 1
def enqueue(op, params, worker_args \\ []) do def enqueue(op, params, worker_args \\ []) do
params = Map.merge(%{"op" => op}, params) params = Map.merge(%{"op" => op}, params)
queue_atom = String.to_atom(unquote(queue)) queue_atom = String.to_atom(unquote(queue))

View file

@ -10,7 +10,7 @@ defmodule Pleroma.ActivityExpirationWorkerTest do
test "deletes an activity" do test "deletes an activity" do
activity = insert(:note_activity) activity = insert(:note_activity)
expiration = insert(:expiration_in_the_past, %{activity_id: activity.id}) expiration = insert(:expiration_in_the_past, %{activity_id: activity.id})
Pleroma.ActivityExpirationWorker.perform(:execute, expiration.id) Pleroma.Daemons.ActivityExpirationDaemon.perform(:execute, expiration.id)
refute Repo.get(Activity, activity.id) refute Repo.get(Activity, activity.id)
end end

View file

@ -2,11 +2,11 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.DigestEmailWorkerTest do defmodule Pleroma.DigestEmailDaemonTest do
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.DigestEmailWorker alias Pleroma.Daemons.DigestEmailDaemon
alias Pleroma.Tests.ObanHelpers alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -23,7 +23,7 @@ test "it sends digest emails" do
User.switch_email_notifications(user2, "digest", true) User.switch_email_notifications(user2, "digest", true)
CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"}) CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"})
DigestEmailWorker.perform() DigestEmailDaemon.perform()
ObanHelpers.perform_all() ObanHelpers.perform_all()
# Performing job(s) enqueued at previous step # Performing job(s) enqueued at previous step
ObanHelpers.perform_all() ObanHelpers.perform_all()

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ScheduledActivityWorkerTest do defmodule Pleroma.ScheduledActivityDaemonTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.ScheduledActivity alias Pleroma.ScheduledActivity
import Pleroma.Factory import Pleroma.Factory
@ -10,7 +10,7 @@ defmodule Pleroma.ScheduledActivityWorkerTest do
test "creates a status from the scheduled activity" do test "creates a status from the scheduled activity" do
user = insert(:user) user = insert(:user)
scheduled_activity = insert(:scheduled_activity, user: user, params: %{status: "hi"}) scheduled_activity = insert(:scheduled_activity, user: user, params: %{status: "hi"})
Pleroma.ScheduledActivityWorker.perform(:execute, scheduled_activity.id) Pleroma.Daemons.ScheduledActivityDaemon.perform(:execute, scheduled_activity.id)
refute Repo.get(ScheduledActivity, scheduled_activity.id) refute Repo.get(ScheduledActivity, scheduled_activity.id)
activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id)) activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id))