Skip to content
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

Alias health in lieu of a separate binding #129

Merged
merged 1 commit into from
Oct 21, 2022
Merged

Alias health in lieu of a separate binding #129

merged 1 commit into from
Oct 21, 2022

Conversation

mabdullahsari
Copy link
Contributor

Hey 👋

This PR addresses an issue that arises when using Laravel's Container Events.

Given the following excerpt from an actual ServiceProvider:

public function register(): void
{
    $this->app->afterResolving(Health::class, $this->registerChecks(...));
}

private function registerChecks(Health $health)
{
    $health->checks([
        CacheCheck::new(),
        // ommitted for brevity...
    ]);
}

Triggering the health:schedule-check-heartbeat command makes the application crash with the following exception:

Spatie\Health\Exceptions\DuplicateCheckNamesFound

Digging a bit deeper reveals that the command eventually makes use of the Health Facade on L17.

The problem with this, is that the Facade makes use of the health binding instead of the singleton Health::class binding. A kinda hidden and notorious behaviour of the Facade class is that it creates a completely separate service context, ie. non-singleton bindings will be treated as singletons when making use of a Facade.


TL;DR By aliasing health to Health, we can ensure the same instance is used across all possible use cases.

(I have also removed the Closure defined on L39 because the IoC container will be able to auto-wire that without any problems.)

Thanks!

@freekmurze freekmurze merged commit 896efcb into spatie:main Oct 21, 2022
@freekmurze
Copy link
Member

Thank you!

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.

2 participants