From 7b99366869a0ff728dbb352a0c3c7d16ff711747 Mon Sep 17 00:00:00 2001 From: yujonglee Date: Sat, 19 Oct 2024 21:34:12 +0900 Subject: [PATCH] delete trieve group in the background worker --- core/config/config.exs | 3 ++- .../index/trieve/changes/delete_group.ex | 23 ++++++++----------- .../canary/workers/delete_trieve_groups.ex | 16 +++++++++++++ 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 core/lib/canary/workers/delete_trieve_groups.ex diff --git a/core/config/config.exs b/core/config/config.exs index 7cadd7cd..60d04644 100644 --- a/core/config/config.exs +++ b/core/config/config.exs @@ -60,7 +60,8 @@ config :canary, Oban, default: 10, github_processor: 2, webpage_processor: 2, - email: 10 + email: 10, + trieve: 50 ], repo: Canary.Repo, plugins: [ diff --git a/core/lib/canary/index/trieve/changes/delete_group.ex b/core/lib/canary/index/trieve/changes/delete_group.ex index dd7f5f15..acace77d 100644 --- a/core/lib/canary/index/trieve/changes/delete_group.ex +++ b/core/lib/canary/index/trieve/changes/delete_group.ex @@ -1,6 +1,5 @@ defmodule Canary.Index.Trieve.Changes.DeleteGroup do use Ash.Resource.Change - alias Canary.Index.Trieve @impl true def init(opts) do @@ -16,17 +15,15 @@ defmodule Canary.Index.Trieve.Changes.DeleteGroup do @impl true def after_batch(changesets_and_results, opts, _context) do - changesets_and_results - |> Enum.map(fn {_changeset, record} -> - id = Map.get(record, opts[:tracking_id_attribute]) - {id, record} - end) - |> Enum.map(fn {id, record} -> - case Trieve.Client.delete_group(id) do - :ok -> {:ok, record} - {:error, %{"message" => "Not Found" <> _}} -> {:ok, record} - {:error, error} -> {:error, error} - end - end) + records = + changesets_and_results + |> Enum.map(fn {_changeset, record} -> record end) + + records + |> Enum.map(&%{"tracking_id" => Map.get(&1, opts[:tracking_id_attribute])}) + |> Enum.map(&Canary.Workers.DeleteTrieveGroup.new(&1)) + |> Oban.insert_all() + + Enum.map(records, &{:ok, &1}) end end diff --git a/core/lib/canary/workers/delete_trieve_groups.ex b/core/lib/canary/workers/delete_trieve_groups.ex new file mode 100644 index 00000000..6075d97d --- /dev/null +++ b/core/lib/canary/workers/delete_trieve_groups.ex @@ -0,0 +1,16 @@ +defmodule Canary.Workers.DeleteTrieveGroup do + use Oban.Worker, + queue: :trieve, + max_attempts: 3 + + alias Canary.Index.Trieve + + @impl true + def perform(%Oban.Job{args: %{"tracking_id" => id}}) do + case Trieve.Client.delete_group(id) do + :ok -> :ok + {:error, %{"message" => "Not Found" <> _}} -> :ok + {:error, error} -> {:error, error} + end + end +end