moved ScheduledActivity to Oban Periodic jobs
authorMaksim Pechnikov <parallel588@gmail.com>
Wed, 27 Nov 2019 06:26:37 +0000 (09:26 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Wed, 27 Nov 2019 06:26:37 +0000 (09:26 +0300)
config/config.exs
lib/pleroma/application.ex
lib/pleroma/workers/cron/scheduled_activity_worker.ex [moved from lib/pleroma/daemons/scheduled_activity_daemon.ex with 51% similarity]
lib/pleroma/workers/scheduled_activity_worker.ex [deleted file]
test/daemons/scheduled_activity_daemon_test.exs [deleted file]
test/scheduled_activity_test.exs

index 5fc92ca1b02f3e7b679ca6463c56b447fee0729f..f4f7fcce7ead2c634f5c10367ea73f888fd72c66 100644 (file)
@@ -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,
index 69a1e4a50cad332ad9e0340bd7ccef3626e889bf..71670d9fc0c2783a3a8f76a8c6a69ab72c3e217c 100644 (file)
@@ -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
       ] ++
similarity index 51%
rename from lib/pleroma/daemons/scheduled_activity_daemon.ex
rename to lib/pleroma/workers/cron/scheduled_activity_worker.ex
index aee5f723a05fdc953b8afb93ca30041f1340e699..407ab687a5e4dd7a102da07b5ecfe5a727efc284 100644 (file)
@@ -2,37 +2,33 @@
 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
 # 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 (file)
index ca7d53a..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# 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 (file)
index c8e4644..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# 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
index dcf12fb490eca71ffa8f3324800199cff9e9fed2..d3d05745f4c218703d53a6fd297a6312ef7e1310 100644 (file)
@@ -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