MishkaCmsCore.Runtime.Resources.Changes.SendToOban (Mishka CMS Core v0.0.1)

Copy Markdown View Source

Triggers background compilation jobs for runtime resources.

This change automatically enqueues Oban jobs whenever runtime resources (Pages, Layouts, Components, etc.) are created, updated, or destroyed. The jobs handle asynchronous compilation of these resources into executable Elixir modules, ensuring the runtime system reflects the latest changes without blocking the main request/response cycle.

The job carries the record's attributes (including site_id) and the action; the worker resolves the site from site_id at run time. Action types are differentiated, with special handling for unarchive. Enqueueing is skipped when an update changes nothing (context[:changed?] == false).

Jobs are unique per record (resource:id). Changing a record again while its job is still queued (available or scheduled) replaces that job's args with the latest, so only the newest version is ever compiled — never a stale snapshot.

In test environments, job enqueueing is disabled to prevent background processing during tests, making test execution more predictable and faster. The change uses after_action hooks to ensure jobs are only enqueued after successful database commits, preventing compilation of resources that fail validation or persistence.

Summary

Functions

Provides atomic operation support for the change.

Builds the Oban job args for a runtime change.

Applies the change to enqueue compilation jobs after successful actions.

Functions

atomic(changeset, opts, context)

Provides atomic operation support for the change.

Delegates to the regular change function for compatibility.

build_job_args(changeset, record)

@spec build_job_args(Ash.Changeset.t(), Ash.Resource.Record.t()) :: map()

Builds the Oban job args for a runtime change.

Serializes the record's attributes (which always include site_id) plus the action. The worker resolves the site name from site_id at run time, so the :site relationship is never touched here — loading it would re-read the multitenant source resource and demand a tenant.

It also carries actor_id when somebody is acting. The activity log reads it (MishkaCmsCore.Activity.Sources.Oban) so a job that failed names the person whose save enqueued it rather than "System" — the same actor AshPaperTrail stamps on the version of that very save, so the two rows agree. A job nobody triggered (a cron, a boot) has no actor and the key is absent.

change(changeset, opts, context)

Applies the change to enqueue compilation jobs after successful actions.

Parameters

  • changeset - The Ash changeset for the resource being modified
  • _opts - Change options (unused)
  • _context - Change context (unused)

Returns

Modified changeset with after_action hook for job enqueueing