Skip to content

Commit

Permalink
improvement: add lock_for_update? to the oban trigger DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 4, 2024
1 parent f4fc471 commit 1c58336
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ spark_locals_without_parens = [
action_input: 1,
debug?: 1,
domain: 1,
lock_for_update?: 1,
log_errors?: 1,
log_final_error?: 1,
max_attempts: 1,
Expand Down
1 change: 1 addition & 0 deletions documentation/dsls/DSL-AshOban.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ end
| [`action_input`](#oban-triggers-trigger-action_input){: #oban-triggers-trigger-action_input } | `map` | | Static inputs to supply to the update/destroy action when it is called. Any metadata produced by `read_metadata` will overwrite these values. |
| [`scheduler_queue`](#oban-triggers-trigger-scheduler_queue){: #oban-triggers-trigger-scheduler_queue } | `atom` | | The queue to place the scheduler job in. The same queue as job is used by default (but with a priority of 1 so schedulers run first). |
| [`debug?`](#oban-triggers-trigger-debug?){: #oban-triggers-trigger-debug? } | `boolean` | `false` | If set to `true`, detailed debug logging will be enabled for this trigger. You can also set `config :ash_oban, debug_all_triggers?: true` to enable debug logging for all triggers. |
| [`lock_for_update?`](#oban-triggers-trigger-lock_for_update?){: #oban-triggers-trigger-lock_for_update? } | `boolean` | `true` | If `true`, a transaction will be started before looking up the record, and it will be locked for update. Typically you should leave this on unless you have before/after/around transaction hooks. |
| [`scheduler_cron`](#oban-triggers-trigger-scheduler_cron){: #oban-triggers-trigger-scheduler_cron } | `String.t \| false` | `"* * * * *"` | A crontab configuration for when the job should run. Defaults to once per minute ("* * * * *"). Use `false` to disable the scheduler entirely. |
| [`stream_batch_size`](#oban-triggers-trigger-stream_batch_size){: #oban-triggers-trigger-stream_batch_size } | `pos_integer` | | The batch size to pass when streaming records from using `Ash.stream!/2`. No batch size is passed if none is provided here, so the default is used. |
| [`queue`](#oban-triggers-trigger-queue){: #oban-triggers-trigger-queue } | `atom` | | The queue to place the worker job in. The trigger name is used by default. |
Expand Down
10 changes: 9 additions & 1 deletion lib/ash_oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule AshOban do
queue: atom,
scheduler_cron: String.t(),
scheduler_queue: atom,
lock_for_update?: boolean(),
action_input: map(),
max_attempts: pos_integer(),
record_limit: pos_integer(),
Expand All @@ -38,6 +39,7 @@ defmodule AshOban do
:read_action,
:action_input,
:worker_read_action,
:lock_for_update?,
:queue,
:debug?,
:read_metadata,
Expand Down Expand Up @@ -101,7 +103,13 @@ defmodule AshOban do
type: :boolean,
default: false,
doc:
"If set to `true`, detailed debug logging will be enabled for this trigger. You can also set `config :ash_oban, debug_all_triggers?: true` to enable debug logging for all triggers."
"If set to `true`, detailed debug logging will be enabled for this trigger. You can also set `config :ash_oban, debug_all_triggers?: true` to enable debug logging for all triggers. If the action has `transaction?: false` this is automatically false."
],
lock_for_update?: [
type: :boolean,
default: true,
doc:
"If `true`, a transaction will be started before looking up the record, and it will be locked for update. Typically you should leave this on unless you have before/after/around transaction hooks."
],
scheduler_cron: [
type: {:or, [:string, {:literal, false}]},
Expand Down
4 changes: 2 additions & 2 deletions lib/transformers/define_schedulers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ defmodule AshOban.Transformers.DefineSchedulers do

on_error_transaction? =
can_transact? && trigger.on_error &&
Ash.Resource.Info.action(dsl, trigger.on_error).transaction?
Ash.Resource.Info.action(dsl, trigger.on_error).transaction? && trigger.lock_for_update?

trigger_action = Ash.Resource.Info.action(dsl, trigger.action)

work_transaction? =
can_transact? && trigger_action.transaction?
can_transact? && trigger_action.transaction? && trigger.lock_for_update?

atomic? = Map.get(trigger_action, :require_atomic?, false)

Expand Down

0 comments on commit 1c58336

Please sign in to comment.