Skip to content

Commit 6ab55f7

Browse files
committed
chore: support shared job context
1 parent 76c06f7 commit 6ab55f7

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

documentation/dsls/DSL-AshOban.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959
| [`domain`](#oban-domain){: #oban-domain } | `module` | | The Domain to use when calling actions on this resource. Defaults to the resource's domain. |
6060
| [`list_tenants`](#oban-list_tenants){: #oban-list_tenants } | `list(any) \| (-> any) \| module` | `[nil]` | A list of tenants or a function behaviour that returns a list of tenants a trigger should be run for. Can be overwritten on the trigger level. |
6161
| [`shared_context?`](#oban-shared_context?){: #oban-shared_context? } | `boolean` | `false` | Deprecated: Use `shared_context` instead. `shared_context? true` is equivalent to `shared_context :all`. |
62-
| [`shared_context`](#oban-shared_context){: #oban-shared_context } | `:all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. Shared context propagates to related actions called via `manage_relationship` and can be passed to other action invocations using the context as scope, making it easier to detect AshOban execution in nested actions. Valid keys: - `:ash_oban?` - the `ash_oban?: true` private flag - `:job` - the `ash_oban: %{job: job}` context data Use `:all` to share all keys (equivalent to the old `shared_context? true`). We recommend `shared_context [:job]` for most use cases. Can be overridden per trigger or scheduled action. |
62+
| [`shared_context`](#oban-shared_context){: #oban-shared_context } | `:all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. Shared context propagates to related actions called via `manage_relationship` and can be passed to other action invocations using the context as scope, making it easier to detect AshOban execution in nested actions. Valid keys: - `:ash_oban?` - the `ash_oban?: true` private flag - `:job` - the `ash_oban: %{job: job}` context data Use `:all` to share all keys (equivalent to the old `shared_context? true`). We recommend `shared_context [:job]` for most use cases. Can be overridden per trigger or scheduled action. If not specified at any level, falls back to `Application.get_env(:ash_oban, :shared_context)`. |
6363
| [`use_tenant_from_record?`](#oban-use_tenant_from_record?){: #oban-use_tenant_from_record? } | `boolean` | `false` | Default value for `use_tenant_from_record?` for all triggers in this resource. When set to `true`, tenants will be extracted from each record's tenant attribute and used when workers process those records. This allows schedulers to use multitenancy `:allow_global` read actions to find records across all tenants, while worker actions still run with the correct tenant context for each record. Can be overridden per trigger. |
6464

6565

@@ -150,7 +150,7 @@ end
150150
| [`on_error`](#oban-triggers-trigger-on_error){: #oban-triggers-trigger-on_error } | `atom` | | An update action to call after the last attempt has failed. See the getting started guide for more. |
151151
| [`on_error_fails_job?`](#oban-triggers-trigger-on_error_fails_job?){: #oban-triggers-trigger-on_error_fails_job? } | `boolean` | `false` | Determines if the oban job will be failed on the last attempt when there is an on_error handler that is called. If there is no on_error, then the action is always marked as failed on the last attempt. |
152152
| [`shared_context?`](#oban-triggers-trigger-shared_context?){: #oban-triggers-trigger-shared_context? } | `boolean` | | Deprecated: Use `shared_context` instead. `shared_context? true` is equivalent to `shared_context :all`. |
153-
| [`shared_context`](#oban-triggers-trigger-shared_context){: #oban-triggers-trigger-shared_context } | `:all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. Shared context propagates to related actions called via `manage_relationship` and can be passed to other action invocations using the context as scope, making it easier to detect AshOban execution in nested actions. Valid keys: - `:ash_oban?` - the `ash_oban?: true` private flag - `:job` - the `ash_oban: %{job: job}` context data Use `:all` to share all keys (equivalent to the old `shared_context? true`). We recommend `shared_context [:job]` for most use cases. If not specified, inherits the global `shared_context` setting from the `oban` section. |
153+
| [`shared_context`](#oban-triggers-trigger-shared_context){: #oban-triggers-trigger-shared_context } | `:all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. Shared context propagates to related actions called via `manage_relationship` and can be passed to other action invocations using the context as scope, making it easier to detect AshOban execution in nested actions. Valid keys: - `:ash_oban?` - the `ash_oban?: true` private flag - `:job` - the `ash_oban: %{job: job}` context data Use `:all` to share all keys (equivalent to the old `shared_context? true`). We recommend `shared_context [:job]` for most use cases. If not specified, inherits the global `shared_context` setting from the `oban` section, then falls back to `Application.get_env(:ash_oban, :shared_context)`. |
154154
| [`use_tenant_from_record?`](#oban-triggers-trigger-use_tenant_from_record?){: #oban-triggers-trigger-use_tenant_from_record? } | `boolean` | `false` | If set to `true`, the tenant will be extracted from each record's tenant attribute and used when the worker processes that record. This allows the scheduler to use a multitenancy `:allow_global` read action to find records across all tenants, while the worker action still runs with the correct tenant context for each individual record. If not specified, inherits the global `use_tenant_from_record?` setting from the `oban` section. |
155155
| [`tags`](#oban-triggers-trigger-tags){: #oban-triggers-trigger-tags } | `list(String.t)` | `[]` | A list of tags to add to the Oban job. Tags are merged with any tags set in `worker_opts`. |
156156
| [`worker_opts`](#oban-triggers-trigger-worker_opts){: #oban-triggers-trigger-worker_opts } | `keyword` | `[]` | Options to set on the worker. ATTENTION: this may overwrite options set by ash_oban, make sure you know what you are doing. See [Oban.Worker](https://hexdocs.pm/oban/Oban.Worker.html#module-defining-workers) for options and [Oban.Pro.Worker](https://oban.pro/docs/pro/Oban.Pro.Worker.html) for oban pro |
@@ -275,7 +275,7 @@ schedule name, cron
275275
| [`tags`](#oban-scheduled_actions-schedule-tags){: #oban-scheduled_actions-schedule-tags } | `list(String.t)` | `[]` | A list of tags to add to the Oban job. |
276276
| [`debug?`](#oban-scheduled_actions-schedule-debug?){: #oban-scheduled_actions-schedule-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. |
277277
| [`shared_context?`](#oban-scheduled_actions-schedule-shared_context?){: #oban-scheduled_actions-schedule-shared_context? } | `boolean` | | Deprecated: Use `shared_context` instead. `shared_context? true` is equivalent to `shared_context :all`. |
278-
| [`shared_context`](#oban-scheduled_actions-schedule-shared_context){: #oban-scheduled_actions-schedule-shared_context } | `:all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. See the trigger-level `shared_context` option for details. If not specified, inherits the global `shared_context` setting from the `oban` section. |
278+
| [`shared_context`](#oban-scheduled_actions-schedule-shared_context){: #oban-scheduled_actions-schedule-shared_context } | `nil \| :all \| list(:ash_oban? \| :job)` | | Determines which context keys are placed in shared context instead of regular context. See the trigger-level `shared_context` option for details. If not specified, inherits the global `shared_context` setting from the `oban` section, then falls back to `Application.get_env(:ash_oban, :shared_context)`. |
279279

280280

281281

documentation/tutorials/getting-started-with-ash-oban.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,33 @@ policies do
191191
end
192192
```
193193

194+
## Shared Context
195+
196+
By default, context set by AshOban (like `ash_oban?: true` and the `%Oban.Job{}` struct) is placed in the regular action context. This means it is **not** propagated to nested actions called via `manage_relationship` or other nested action invocations.
197+
198+
If you need AshOban context to propagate to nested actions (e.g. so that policy bypasses work in related actions), use the `shared_context` option. This places the specified keys into Ash's shared context, which is automatically propagated to all nested actions.
199+
200+
```elixir
201+
# Recommended: share only the job
202+
shared_context [:job]
203+
204+
# Share all AshOban context keys (ash_oban? and job)
205+
shared_context :all
206+
```
207+
208+
`shared_context` can be set at three levels, with each inheriting from the next if not specified:
209+
210+
1. **Per trigger or scheduled action** — set `shared_context` directly on the trigger/schedule
211+
2. **Per resource** — set `shared_context` in the `oban` section of the resource DSL
212+
3. **Application config** — set `config :ash_oban, shared_context: [:job]` in your app config
213+
214+
This makes it easy to configure shared context globally:
215+
216+
```elixir
217+
# in config.exs
218+
config :ash_oban, shared_context: [:job]
219+
```
220+
194221
## Persisting the actor along with a job
195222

196223
Create a module that is responsible for translating the current user to a value that will be JSON encoded, and for turning that encoded value back into an actor.

lib/ash_oban.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ defmodule AshOban do
410410
411411
We recommend `shared_context [:job]` for most use cases.
412412
413-
If not specified, inherits the global `shared_context` setting from the `oban` section.
413+
If not specified, inherits the global `shared_context` setting from the `oban` section,
414+
then falls back to `Application.get_env(:ash_oban, :shared_context)`.
414415
"""
415416
],
416417
use_tenant_from_record?: [
@@ -620,7 +621,8 @@ defmodule AshOban do
620621
doc: """
621622
Determines which context keys are placed in shared context instead of regular context.
622623
See the trigger-level `shared_context` option for details.
623-
If not specified, inherits the global `shared_context` setting from the `oban` section.
624+
If not specified, inherits the global `shared_context` setting from the `oban` section,
625+
then falls back to `Application.get_env(:ash_oban, :shared_context)`.
624626
"""
625627
]
626628
]
@@ -713,6 +715,8 @@ defmodule AshOban do
713715
We recommend `shared_context [:job]` for most use cases.
714716
715717
Can be overridden per trigger or scheduled action.
718+
719+
If not specified at any level, falls back to `Application.get_env(:ash_oban, :shared_context)`.
716720
"""
717721
],
718722
use_tenant_from_record?: [

lib/transformers/set_defaults.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ defmodule AshOban.Transformers.SetDefaults do
209209
:all
210210

211211
true ->
212-
nil
212+
Application.get_env(:ash_oban, :shared_context)
213213
end
214214
end
215215

0 commit comments

Comments
 (0)