Use oban for search indexing
authorEkaterina Vaartis <vaartis@kotobank.ch>
Sat, 22 Jan 2022 12:23:11 +0000 (15:23 +0300)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Wed, 29 Jun 2022 19:49:45 +0000 (20:49 +0100)
config/config.exs
lib/pleroma/search.ex
lib/pleroma/workers/search_indexing_worker.ex [new file with mode: 0644]

index c49e16fe236cdf569296250a1fcfef6446ae2733..05beb9ba075e1656e5ec914f0103dd84f45742ef 100644 (file)
@@ -568,7 +568,8 @@ config :pleroma, Oban,
     remote_fetcher: 2,
     attachments_cleanup: 1,
     new_users_digest: 1,
-    mute_expire: 5
+    mute_expire: 5,
+    search_indexing: 1
   ],
   plugins: [Oban.Plugins.Pruner],
   crontab: [
index ae0b28c54e52bd71ff156bfed5c8c2816eb574d0..af858fc46965198f67fafed29fc59e24f2b2c1d0 100644 (file)
@@ -1,19 +1,12 @@
 defmodule Pleroma.Search do
-  def add_to_index(activity) do
-    search_module = Pleroma.Config.get([Pleroma.Search, :module])
+  alias Pleroma.Workers.SearchIndexingWorker
 
-    ConcurrentLimiter.limit(Pleroma.Search, fn ->
-      Task.start(fn -> search_module.add_to_index(activity) end)
-    end)
+  def add_to_index(activity) do
+    SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity.id})
   end
 
   def remove_from_index(object) do
-    # Also delete from search index
-    search_module = Pleroma.Config.get([Pleroma.Search, :module])
-
-    ConcurrentLimiter.limit(Pleroma.Search, fn ->
-      Task.start(fn -> search_module.remove_from_index(object) end)
-    end)
+    SearchIndexingWorker.enqueue("remove_from_index", %{"object" => object.id})
   end
 
   def search(query, options) do
diff --git a/lib/pleroma/workers/search_indexing_worker.ex b/lib/pleroma/workers/search_indexing_worker.ex
new file mode 100644 (file)
index 0000000..43b7bad
--- /dev/null
@@ -0,0 +1,21 @@
+defmodule Pleroma.Workers.SearchIndexingWorker do
+  use Pleroma.Workers.WorkerHelper, queue: "search_indexing"
+
+  @impl Oban.Worker
+
+  def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
+    activity = Pleroma.Activity.get_by_id_with_object(activity_id)
+
+    search_module = Pleroma.Config.get([Pleroma.Search, :module])
+
+    search_module.add_to_index(activity)
+  end
+
+  def perform(%Job{args: %{"op" => "remove_from_index", "object" => object_id}}) do
+    object = Pleroma.Object.get_by_id(object_id)
+
+    search_module = Pleroma.Config.get([Pleroma.Search, :module])
+
+    search_module.remove_from_index(object)
+  end
+end