-
Notifications
You must be signed in to change notification settings - Fork 1
Add foundation-shutdown #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
defunctl
wants to merge
5
commits into
main
Choose a base branch
from
feature/foundation-shutdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f0247d
Add foundation-shutdown
defunctl a053cc8
Add foundation-shutdown to AGENTS.md
defunctl db530be
Add decorators, add fastcgi_finish_request, litespeed_finish_request …
defunctl 0bbe799
Skip response finisher mocks when native functions exist
defunctl ef4fa94
Merge main into feature/foundation-shutdown
defunctl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Path-based git attributes | ||
| # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html | ||
|
|
||
| # Ignore paths when git creates an archive of this package | ||
| .gitattributes export-ignore | ||
| .gitignore export-ignore | ||
| .github export-ignore |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| name: Close Pull Request | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened] | ||
|
|
||
| jobs: | ||
| run: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: superbrothers/close-pull-request@v3 | ||
| with: | ||
| comment: "This is a read-only repository. Please submit your PR on the https://github.com/stellarwp/foundation repository.<br><br>Thanks!" | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| vendor/ | ||
| composer.lock |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace StellarWP\Foundation\Shutdown\Contracts; | ||
|
|
||
| /** | ||
| * Runs application shutdown work at a lifecycle boundary. | ||
| */ | ||
| interface ShutdownRunner extends Terminable | ||
| { | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace StellarWP\Foundation\Shutdown\Contracts; | ||
|
|
||
| /** | ||
| * Work that should run when an application reaches its termination boundary. | ||
| */ | ||
| interface Terminable | ||
| { | ||
| public function terminate(): void; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # Foundation Shutdown | ||
|
|
||
| > [!WARNING] | ||
| > **This is a read-only repository!** For pull requests or issues, see [stellarwp/foundation](https://github.com/stellarwp/foundation). | ||
|
|
||
| Run application termination work once, in a predictable order, without allowing | ||
| one failed task to prevent the remaining tasks from running. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```shell | ||
| composer require stellarwp/foundation-shutdown | ||
| ``` | ||
|
|
||
| ## Register the provider | ||
|
|
||
| Register `ShutdownProvider` through your application's normal Foundation provider | ||
| list: | ||
|
|
||
| ```php | ||
| use StellarWP\Foundation\Shutdown\ShutdownProvider; | ||
|
|
||
| private array $providers = [ | ||
| ShutdownProvider::class, | ||
| ]; | ||
| ``` | ||
|
|
||
| The provider has no custom constructor and uses the application's existing | ||
| Foundation container and configuration. Package installation alone has no side | ||
| effects; consumers may omit this provider and construct the public runner directly | ||
| or supply their own provider. | ||
|
|
||
| ## Create and contribute tasks | ||
|
|
||
| Termination work implements the small `Terminable` contract: | ||
|
|
||
| ```php | ||
| use StellarWP\Foundation\Shutdown\Contracts\Terminable; | ||
|
|
||
| final class FlushTelemetry implements Terminable | ||
| { | ||
| public function terminate(): void { | ||
| // Flush bounded application telemetry. | ||
| } | ||
| } | ||
|
|
||
| final class CloseRequestLog implements Terminable | ||
| { | ||
| public function terminate(): void { | ||
| // Close the request log after the response is sent. | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Contribute an application's termination work from one provider. Resolve the | ||
| concrete tasks lazily so all providers can finish registering before termination | ||
| services are constructed. Contributions must be registered before the runner is | ||
| resolved: | ||
|
|
||
| ```php | ||
| use lucatume\DI52\Container; | ||
| use StellarWP\Foundation\Container\Contracts\Provider; | ||
| use StellarWP\Foundation\Shutdown\ShutdownProvider as FoundationShutdownProvider; | ||
| use StellarWP\Foundation\Shutdown\ShutdownTask; | ||
|
|
||
| final class ApplicationShutdownProvider extends Provider | ||
| { | ||
| public function register(): void { | ||
| $this->container->singleton(CloseRequestLog::class); | ||
| $this->container->singleton(FlushTelemetry::class); | ||
|
|
||
| $this->container->mergeArrayVar( | ||
| FoundationShutdownProvider::TASKS, | ||
| static fn (Container $container): array => [ | ||
| new ShutdownTask($container->get(CloseRequestLog::class), 10), | ||
| new ShutdownTask($container->get(FlushTelemetry::class), 100), | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Register both providers through the application's provider list: | ||
|
|
||
| ```php | ||
| private array $providers = [ | ||
| FoundationShutdownProvider::class, | ||
| ApplicationShutdownProvider::class, | ||
| ]; | ||
| ``` | ||
|
|
||
| Lower priority values run first. Tasks with the same priority retain their | ||
| registration order. | ||
|
|
||
| ## WordPress shutdown | ||
|
|
||
| `ShutdownProvider` binds the `ShutdownRunner` contract to the ordered task runner, | ||
| decorated by `ResponseFinishingRunner`, and attaches it to WordPress's `shutdown` | ||
| action at the latest priority. When supported, it finishes the response with | ||
| `fastcgi_finish_request()` or `litespeed_finish_request()` before running the | ||
| contributed tasks. The PHP worker remains occupied until those tasks finish, so | ||
| long-running work still belongs in a proper background queue. | ||
|
|
||
| The runner is resolved lazily when the action fires, so features may contribute | ||
| tasks after the provider is registered. | ||
|
|
||
| With the default provider registered, applications may also invoke the configured | ||
| runner chain directly: | ||
|
|
||
| ```php | ||
| use StellarWP\Foundation\Shutdown\Contracts\ShutdownRunner; | ||
|
|
||
| $container->get(ShutdownRunner::class)->terminate(); | ||
| ``` | ||
|
|
||
| Applications that omit the default provider must bind the `ShutdownRunner` contract | ||
| in their own provider or construct the concrete | ||
| `StellarWP\Foundation\Shutdown\ShutdownRunner` with their desired tasks. | ||
|
|
||
| Each runner instance executes only once, including when termination is invoked | ||
| recursively. A `Throwable` from one task is isolated so later tasks still run. | ||
|
|
||
| ## Logging | ||
|
|
||
| `ShutdownRunner` accepts an optional PSR-3 logger. When the application binds a | ||
| `Psr\Log\LoggerInterface`—including through `foundation-log`—the container injects | ||
| it automatically. Applications without a logger require no additional setup. | ||
|
|
||
| The runner logs the task count and each task at `debug` level. Task failures are | ||
| logged at `error` level with the task class, priority, and actual exception so | ||
| compatible loggers retain its message and stack trace. Logger failures are isolated | ||
| so diagnostics cannot interrupt termination work. | ||
|
|
||
| Output-buffer management, hard task timeouts, and asynchronous execution beyond | ||
| the default WordPress shutdown action belong to the consuming application or a | ||
| dedicated framework integration. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace StellarWP\Foundation\Shutdown; | ||
|
|
||
| use StellarWP\Foundation\Shutdown\Contracts\ShutdownRunner; | ||
| use Throwable; | ||
|
|
||
| /** | ||
| * Finishes the current HTTP response before running termination work. | ||
| */ | ||
| final class ResponseFinishingRunner implements ShutdownRunner | ||
| { | ||
| private bool $terminated = false; | ||
|
|
||
| public function __construct( | ||
| private readonly ShutdownRunner $runner | ||
| ) { | ||
| } | ||
|
|
||
| public function terminate(): void { | ||
| if ($this->terminated) { | ||
| return; | ||
| } | ||
|
|
||
| $this->terminated = true; | ||
|
|
||
| foreach (['fastcgi_finish_request', 'litespeed_finish_request'] as $finishRequest) { | ||
| if (! function_exists($finishRequest)) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| if ($finishRequest()) { | ||
| break; | ||
| } | ||
| } catch (Throwable) { | ||
| // Response finishing is best-effort and must not block termination work. | ||
| } | ||
| } | ||
|
|
||
| $this->runner->terminate(); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace StellarWP\Foundation\Shutdown; | ||
|
|
||
| use lucatume\DI52\Container; | ||
| use StellarWP\Foundation\Container\ContainerAdapter; | ||
| use StellarWP\Foundation\Container\Contracts\Provider; | ||
| use StellarWP\Foundation\Shutdown\Contracts\ShutdownRunner as ShutdownRunnerContract; | ||
|
|
||
| /** | ||
| * Registers the default shutdown runner and task contribution point. | ||
| * | ||
| * @property-read ContainerAdapter $container | ||
| */ | ||
| final class ShutdownProvider extends Provider | ||
| { | ||
| public const string TASKS = self::class . '.tasks'; | ||
| private const string REGISTERED = self::class . '.registered'; | ||
|
|
||
| public function register(): void { | ||
| if ($this->container->has(self::REGISTERED)) { | ||
| return; | ||
| } | ||
|
|
||
| $this->container->singleton(self::REGISTERED, true); | ||
|
|
||
| $this->container->when(ShutdownRunner::class) | ||
| ->needs('$tasks') | ||
| ->give(static fn (Container $container): array => $container->getVar(self::TASKS, [])); | ||
|
|
||
| $this->container->singletonDecorators(ShutdownRunnerContract::class, [ | ||
| ResponseFinishingRunner::class, | ||
| ShutdownRunner::class, | ||
| ]); | ||
|
|
||
| add_action( | ||
| 'shutdown', | ||
| $this->container->callback(ShutdownRunnerContract::class, 'terminate'), | ||
| PHP_INT_MAX | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.