Skip to content

Commit

Permalink
Allow using worker_read_action for locking before processing the job (
Browse files Browse the repository at this point in the history
  • Loading branch information
smt116 authored Jul 20, 2023
1 parent c13793d commit 3876b9c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/transformers/define_schedulers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,18 @@ defmodule AshOban.Transformers.DefineSchedulers do

can_lock? = Ash.DataLayer.data_layer_can?(dsl, {:lock, :for_update})

read_action =
trigger.worker_read_action || trigger.read_action ||
Ash.Resource.Info.primary_action!(resource, :read).name

get_and_lock =
if can_lock? do
quote do
Ash.Changeset.before_action(changeset, fn changeset ->
query()
|> Ash.Query.do_filter(primary_key)
|> Ash.Query.set_context(%{private: %{ash_oban?: true}})
|> Ash.Query.for_read(unquote(trigger.read_action))
|> Ash.Query.for_read(unquote(read_action))
|> Ash.Query.lock(:for_update)
|> unquote(api).read_one()
|> case do
Expand All @@ -235,7 +239,7 @@ defmodule AshOban.Transformers.DefineSchedulers do
query()
|> Ash.Query.do_filter(primary_key)
|> Ash.Query.set_context(%{private: %{ash_oban?: true}})
|> Ash.Query.for_read(unquote(trigger.read_action))
|> Ash.Query.for_read(unquote(read_action))
|> unquote(api).read_one()
|> case do
{:ok, nil} ->
Expand Down Expand Up @@ -279,7 +283,7 @@ defmodule AshOban.Transformers.DefineSchedulers do

handle_error = handle_error(trigger, resource, api)

work = work(trigger, worker, pro?, resource, api)
work = work(trigger, worker, pro?, read_action, api)

Module.create(
worker_module_name,
Expand Down Expand Up @@ -371,18 +375,14 @@ defmodule AshOban.Transformers.DefineSchedulers do
end
end

defp work(trigger, worker, pro?, resource, api) do
defp work(trigger, worker, pro?, read_action, api) do
function_name =
if pro? do
:process
else
:perform
end

read_action =
trigger.worker_read_action || trigger.read_action ||
Ash.Resource.Info.primary_action!(resource, :read).name

if trigger.state != :active do
quote location: :keep do
@impl unquote(worker)
Expand Down

0 comments on commit 3876b9c

Please sign in to comment.