Repository navigation
(feat) Request-scoped lifecycle and context propagation - #368
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Problem - Request-scoped lifecycle and context propagation
Doppar already drops resolved request-scoped services when a request terminates, but that cleanup is implicit and HTTP-only. It sweeps every service bound after boot, so the lifetime of a service depends on when it was registered, and console commands and workers get no equivalent. There was also no object that described the running request, command or job, so logs from one execution could not be tied together and metadata such as a tenant had nowhere to live.
What this adds
An immutable
ExecutionContext(id, type, metadata, parent, start time), a stack of active contexts owned by the container, and a newexecutionbinding scope.singleton,executionortransient.bindExecution()andbindWithScope()declare it,scopeOf()reads it back. An execution-scoped instance is shared inside one context, is never visible to the next request, command or job, and is dropped when its context ends.contextBeginning()andcontextEnding()(facade:Execution::beginning()andExecution::ending()), optionally filtered by context type. Ending listeners run before the scoped instances are dropped.dispatch()opens arequestcontext withmethod,pathandrequest_id(fromX-Request-Id). It is suspended after the response is sent and resumed interminate(), so terminating callbacks anddefer()callbacks still see it. It ends last, after request cleanup, and also ends if the handler or the error page throws.consolecontext carrying the command name, wrapped around its defer scope.LoggerServiceadds anexecutionkey (id, type, parent id, metadata) to each record's context. It is read from the active context at log time, so no logger state exists to reset. A caller-suppliedexecutionkey is never overwritten.Executionfacade overExecutionService, bound asexecution, plus anexecution()helper likedefer()andsignal().Backward compatibility
The only signature changes are optional trailing parameters:
Application::terminate()andDispatchResult::__construct()each gain a nullable?ExecutionContext $context = null.Documentation
Docs are in the docs repo under
versions/4.x: a newexecution-context.md, plus updates tocontainer.md,logging.md,request-lifecycle.md,helpers.md,facades.md. DocsChecklist