Skip to content

[6.x] Store per-job state in queue workers and the MCP stdio server - #20799

Open
riasvdv wants to merge 3 commits into
6.xfrom
rias/long-running-workers
Open

riasvdv wants to merge 3 commits into
6.xfrom
rias/long-running-workers

Conversation

@riasvdv

@riasvdv riasvdv commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Three places stored per-request state in app()->terminating(). queue:work and the MCP stdio server don't terminate between jobs or calls, so that state was written late or lost. 5.x ran each queue job in its own process, so its after-request hooks ran when the job ended.

 change tracking after applying a draft
-  app()->terminating(upsert rows)
+  DB::afterCommit(upsert rows)        // also skipped on rollback
 bulk op triggers, deprecation warnings
   app()->terminating(store)
+  JobProcessed / JobExceptionOccurred (non-sync jobs): store
+  MCP stdio response: store
  • Change-tracking rows were written only when the worker exited, and also when applying the draft rolled back. After commit keeps them outside the save transaction, as Global Variables cause downtime while creating a new Draft #10662 needs.
  • A batched resave ends its bulk op in a later job, which can run on another worker and replay without the triggers still held in memory.
  • Deprecator is request-scoped, and the worker forgets scoped instances before each job, so warnings from every job but the last were dropped.

Evidence

  • Before: no change-tracking rows after commit until app()->terminate(); no bulk op or deprecation rows after a queued job finished.
    After: UpdateCanonicalElementTest, BulkOpDeferralsTest and DeprecatorTest cover each, and fail on the old code.

riasvdv and others added 3 commits November 17, 2026 15:08
Applying a draft deferred the canonical element's change-tracking upserts
to app termination. Queue workers and the MCP stdio server don't
terminate between jobs or calls, so the rows were only written when the
process exited, and they were written even when applying the draft
rolled back. DB::afterCommit() keeps the upserts outside the save
transaction, as #10662 needs, and drops them on rollback.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e8d1-9981-7090-9108-69125aec9ad4
Triggers collected during a queue job stayed in the worker's memory
until it exited. A batched resave ends its bulk op in a later job, which
can run on another worker and replay without them. Pending triggers are
now stored when each non-sync job finishes or throws, and after each MCP
stdio response.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e8d1-9981-7090-9108-69125aec9ad4
The Deprecator is request-scoped, and a queue worker forgets scoped
instances before each job, so warnings logged by every job but the last
were dropped before the worker terminated. They're now stored when each
non-sync job finishes or throws, and after each MCP stdio response.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e8d1-9981-7090-9108-69125aec9ad4
@github-actions

Copy link
Copy Markdown

📚 Storybook previews

@craftcms/ui — open Storybook

No changed components detected in this Storybook.

resources/js — open Storybook

No changed components detected in this Storybook.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant