From 6f202a401babbc73e93fb09e697c4c844688f557 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Wed, 27 Nov 2019 09:26:37 +0300 Subject: [PATCH] moved ScheduledActivity to Oban Periodic jobs --- config/config.exs | 3 +- lib/pleroma/application.ex | 1 - .../cron/scheduled_activity_worker.ex} | 41 +++++-------------- .../workers/scheduled_activity_worker.ex | 12 ------ .../scheduled_activity_daemon_test.exs | 19 --------- test/scheduled_activity_test.exs | 28 +++++++++++++ 6 files changed, 40 insertions(+), 64 deletions(-) rename lib/pleroma/{daemons/scheduled_activity_daemon.ex => workers/cron/scheduled_activity_worker.ex} (51%) delete mode 100644 lib/pleroma/workers/scheduled_activity_worker.ex delete mode 100644 test/daemons/scheduled_activity_daemon_test.exs diff --git a/config/config.exs b/config/config.exs index 5fc92ca1b..f4f7fcce7 100644 --- a/config/config.exs +++ b/config/config.exs @@ -508,7 +508,8 @@ config :pleroma, Oban, ], crontab: [ {"0 0 * * *", Pleroma.Workers.Cron.ClearOauthTokenWorker}, - {"0 * * * *", Pleroma.Workers.Cron.StatsWorker} + {"0 * * * *", Pleroma.Workers.Cron.StatsWorker}, + {"* * * * *", Pleroma.Workers.Cron.ScheduledActivityWorker} ] config :pleroma, :workers, diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 69a1e4a50..71670d9fc 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -35,7 +35,6 @@ defmodule Pleroma.Application do Pleroma.Config.TransferTask, Pleroma.Emoji, Pleroma.Captcha, - Pleroma.Daemons.ScheduledActivityDaemon, Pleroma.Daemons.ActivityExpirationDaemon, Pleroma.Plugs.RateLimiter.Supervisor ] ++ diff --git a/lib/pleroma/daemons/scheduled_activity_daemon.ex b/lib/pleroma/workers/cron/scheduled_activity_worker.ex similarity index 51% rename from lib/pleroma/daemons/scheduled_activity_daemon.ex rename to lib/pleroma/workers/cron/scheduled_activity_worker.ex index aee5f723a..407ab687a 100644 --- a/lib/pleroma/daemons/scheduled_activity_daemon.ex +++ b/lib/pleroma/workers/cron/scheduled_activity_worker.ex @@ -2,37 +2,33 @@ # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.Daemons.ScheduledActivityDaemon do +defmodule Pleroma.Workers.Cron.ScheduledActivityWorker do @moduledoc """ - Sends scheduled activities to the job queue. + The worker to post scheduled actvities. """ + use Oban.Worker, queue: "scheduled_activities" alias Pleroma.Config alias Pleroma.ScheduledActivity alias Pleroma.User alias Pleroma.Web.CommonAPI - use GenServer require Logger @schedule_interval :timer.minutes(1) - def start_link(_) do - GenServer.start_link(__MODULE__, nil) - end - - def init(_) do + @impl Oban.Worker + def perform(_opts, _job) do if Config.get([ScheduledActivity, :enabled]) do - schedule_next() - {:ok, nil} - else - :ignore + @schedule_interval + |> ScheduledActivity.due_activities() + |> Enum.each(&post_activity/1) end end - def perform(:execute, scheduled_activity_id) do + def post_activity(scheduled_activity) do try do - {:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity_id) + {:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity) %User{} = user = User.get_cached_by_id(scheduled_activity.user_id) {:ok, _result} = CommonAPI.post(user, scheduled_activity.params) rescue @@ -42,21 +38,4 @@ defmodule Pleroma.Daemons.ScheduledActivityDaemon do ) end end - - def handle_info(:perform, state) do - ScheduledActivity.due_activities(@schedule_interval) - |> Enum.each(fn scheduled_activity -> - Pleroma.Workers.ScheduledActivityWorker.enqueue( - "execute", - %{"activity_id" => scheduled_activity.id} - ) - end) - - schedule_next() - {:noreply, state} - end - - defp schedule_next do - Process.send_after(self(), :perform, @schedule_interval) - end end diff --git a/lib/pleroma/workers/scheduled_activity_worker.ex b/lib/pleroma/workers/scheduled_activity_worker.ex deleted file mode 100644 index ca7d53af1..000000000 --- a/lib/pleroma/workers/scheduled_activity_worker.ex +++ /dev/null @@ -1,12 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Workers.ScheduledActivityWorker do - use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities" - - @impl Oban.Worker - def perform(%{"op" => "execute", "activity_id" => activity_id}, _job) do - Pleroma.Daemons.ScheduledActivityDaemon.perform(:execute, activity_id) - end -end diff --git a/test/daemons/scheduled_activity_daemon_test.exs b/test/daemons/scheduled_activity_daemon_test.exs deleted file mode 100644 index c8e464491..000000000 --- a/test/daemons/scheduled_activity_daemon_test.exs +++ /dev/null @@ -1,19 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.ScheduledActivityDaemonTest do - use Pleroma.DataCase - alias Pleroma.ScheduledActivity - import Pleroma.Factory - - test "creates a status from the scheduled activity" do - user = insert(:user) - scheduled_activity = insert(:scheduled_activity, user: user, params: %{status: "hi"}) - Pleroma.Daemons.ScheduledActivityDaemon.perform(:execute, scheduled_activity.id) - - refute Repo.get(ScheduledActivity, scheduled_activity.id) - activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id)) - assert Pleroma.Object.normalize(activity).data["content"] == "hi" - end -end diff --git a/test/scheduled_activity_test.exs b/test/scheduled_activity_test.exs index dcf12fb49..d3d05745f 100644 --- a/test/scheduled_activity_test.exs +++ b/test/scheduled_activity_test.exs @@ -8,6 +8,8 @@ defmodule Pleroma.ScheduledActivityTest do alias Pleroma.ScheduledActivity import Pleroma.Factory + clear_config([ScheduledActivity, :enabled]) + setup context do DataCase.ensure_local_uploader(context) end @@ -61,4 +63,30 @@ defmodule Pleroma.ScheduledActivityTest do assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}] end end + + test "creates a status from the scheduled activity" do + Pleroma.Config.put([ScheduledActivity, :enabled], true) + user = insert(:user) + + naive_datetime = + NaiveDateTime.add( + NaiveDateTime.utc_now(), + -:timer.minutes(2), + :millisecond + ) + + scheduled_activity = + insert( + :scheduled_activity, + scheduled_at: naive_datetime, + user: user, + params: %{status: "hi"} + ) + + Pleroma.Workers.Cron.ScheduledActivityWorker.perform(:opts, :pid) + + refute Repo.get(ScheduledActivity, scheduled_activity.id) + activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id)) + assert Pleroma.Object.normalize(activity).data["content"] == "hi" + end end