Skip to content

Commit

Permalink
delete trieve group in the background worker
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Oct 19, 2024
1 parent 7d470a3 commit 7b99366
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
3 changes: 2 additions & 1 deletion core/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
23 changes: 10 additions & 13 deletions core/lib/canary/index/trieve/changes/delete_group.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
16 changes: 16 additions & 0 deletions core/lib/canary/workers/delete_trieve_groups.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7b99366

Please sign in to comment.