Skip to content

Commit

Permalink
move cancel_fetch to source resource
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Sep 25, 2024
1 parent 9fd4d10 commit b85e120
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
44 changes: 43 additions & 1 deletion core/lib/canary/sources/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Canary.Sources.Source do
domain: Canary.Sources,
data_layer: AshPostgres.DataLayer

alias Canary.Sources.Document
require Ash.Query
require Ecto.Query

alias Canary.Sources.Document

attributes do
uuid_primary_key :id
Expand Down Expand Up @@ -103,9 +105,49 @@ defmodule Canary.Sources.Source do
change {Ash.Resource.Change.CascadeDestroy, relationship: :events, action: :destroy}
end

update :cancel_fetch do
require_atomic? false

change set_attribute(:state, :idle)

change fn changeset, _ctx ->
%{id: source_id, config: %Ash.Union{type: type}} = changeset.data

worker =
case type do
:webpage -> Canary.Workers.WebpageProcessor
:github_issue -> Canary.Workers.GithubIssueProcessor
:github_discussion -> Canary.Workers.GithubDiscussionProcessor
end
|> to_string()
|> String.trim_leading("Elixir.")

changeset
|> Ash.Changeset.after_action(fn _, record ->
result =
Oban.Job
|> Ecto.Query.where(worker: ^worker)
|> Ecto.Query.where([j], json_extract_path(j.args, ["source_id"]) == ^source_id)
|> Oban.cancel_all_jobs()

case result do
{:ok, _} ->
{:ok, record}

{:error, error} ->
IO.inspect(error)
{:ok, record}
end
end)
end
end

update :fetch do
require_atomic? false

change set_attribute(:state, :running)
change set_attribute(:last_fetched_at, &DateTime.utc_now/0)

change fn changeset, _ctx ->
%{id: source_id, config: config} = changeset.data

Expand Down
25 changes: 11 additions & 14 deletions core/lib/canary_web/live/source_live/detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ defmodule CanaryWeb.SourceLive.Detail do
use CanaryWeb, :live_component
alias PrimerLive.Component, as: Primer

require Ecto.Query

@impl true
def render(assigns) do
~H"""
Expand Down Expand Up @@ -351,20 +349,19 @@ defmodule CanaryWeb.SourceLive.Detail do

@impl true
def handle_event("cancel", _, socket) do
worker =
case socket.assigns.source.config.type do
:webpage -> Canary.Workers.WebpageProcessor
:github_issue -> Canary.Workers.GithubIssueProcessor
:github_discussion -> Canary.Workers.GithubDiscussionProcessor
end
|> to_string()
|> String.trim_leading("Elixir.")
result =
socket.assigns.source
|> Ash.Changeset.for_update(:cancel_fetch, %{})
|> Ash.update()

Oban.Job
|> Ecto.Query.where(worker: ^worker)
|> Oban.cancel_all_jobs()
case result do
{:ok, _} ->
{:noreply, socket}

{:noreply, socket}
{:error, error} ->
IO.inspect(error)
{:noreply, socket}
end
end

defp transform_params(params) do
Expand Down

0 comments on commit b85e120

Please sign in to comment.