Packages
ash_oban
0.8.5
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0-rc.1
0.8.0-rc.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.6
0.2.5
0.2.4
0.2.3
0.2.3-rc.1
0.2.3-rc.0
0.2.2
0.2.1
0.2.0
0.1.14
0.1.13
0.1.12
0.1.11
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
The extension for integrating Ash resources with Oban.
Current section
Files
Jump to
Current section
Files
lib/changes/run_oban_trigger.ex
# SPDX-FileCopyrightText: 2023 ash_oban contributors <https://github.com/ash-project/ash_oban/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshOban.Changes.RunObanTrigger do
@moduledoc """
Runs an oban trigger by name.
"""
use Ash.Resource.Change
@impl true
def change(changeset, opts, context) do
trigger = AshOban.Info.oban_trigger(changeset.resource, opts[:trigger])
if !trigger do
raise "No such trigger #{opts[:trigger]} for resource #{inspect(changeset.resource)}"
end
Ash.Changeset.after_action(changeset, fn _changeset, result ->
AshOban.run_trigger(
result,
trigger,
Keyword.merge(
opts[:oban_job_opts] || [],
context
|> Ash.Context.to_opts()
|> Keyword.take([:actor, :tenant])
|> Keyword.update(:tenant, nil, &Ash.ToTenant.to_tenant(&1, changeset.resource))
)
)
{:ok, result}
end)
end
@impl true
def batch_change(changesets, _opts, _context) do
changesets
end
@impl true
def atomic(changeset, opts, context) do
{:ok, change(changeset, opts, context)}
end
@impl true
def after_batch([], _opts, _context) do
[]
end
def after_batch([{changeset, _} | _] = changesets_and_results, opts, context) do
trigger = AshOban.Info.oban_trigger(changeset.resource, opts[:trigger])
if !trigger do
raise "No such trigger #{opts[:trigger]} for resource #{inspect(changeset.resource)}"
end
results =
changesets_and_results
|> Enum.map(&elem(&1, 1))
results
|> AshOban.run_triggers(
trigger,
Keyword.merge(
opts[:oban_job_opts] || [],
context
|> Ash.Context.to_opts()
|> Keyword.take([:actor, :tenant])
|> Keyword.update(:tenant, nil, &Ash.ToTenant.to_tenant(&1, changeset.resource))
)
)
results
|> Enum.map(&{:ok, &1})
end
end