Skip to content

Ensure all handlers are explicitly closed on kernel shutdown#377

Open
andy-educake wants to merge 3 commits intosymfony:4.xfrom
andy-educake:376-add-handler-manager-to-close-handlers
Open

Ensure all handlers are explicitly closed on kernel shutdown#377
andy-educake wants to merge 3 commits intosymfony:4.xfrom
andy-educake:376-add-handler-manager-to-close-handlers

Conversation

@andy-educake
Copy link
Copy Markdown

No description provided.

@andy-educake andy-educake force-pushed the 376-add-handler-manager-to-close-handlers branch from 6e7a558 to 5c10161 Compare November 24, 2020 10:38
Copy link
Copy Markdown
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this PR, but I'm not sure about it. If we do that, we should do the same thing for all others bundle that does IO.

@nicolas-grekas WDYT?

Comment thread MonologBundle.php Outdated
Comment thread Services/HandlerManager.php Outdated
Comment thread Services/HandlerManager.php Outdated
Comment thread Services/HandlerManager.php Outdated
Comment thread Resources/config/monolog.xml Outdated
Comment thread Services/HandlerManager.php Outdated
@andy-educake
Copy link
Copy Markdown
Author

Thanks for the comments @lyrixx Have dealt with all of them I think. I notice that Travis has failed for PHP 5.6 and PHP 7.0. Possibly because I used the iterable type hint on the constructor param. Should I use the Iterator interface instead for bc?

*/
private $handlers;

public function __construct(\IteratorAggregate $handlers)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct(\IteratorAggregate $handlers)
public function __construct(iterable $handlers)

The type IteratorAggregate is a bit more specific that it needs to be, isn‘t it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bundle supports PHP 5.6+ currently

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but Traversable should be used instead of IteratorAggregate

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should drop support for SF 3.X, and bump PHP to 7.1
IMHO, we must do that before this PR

</service>

<service id="monolog.handler_manager" class="Symfony\Bundle\MonologBundle\HandlerLifecycleManager" public="true">
<argument type="tagged_iterator" tag="monolog.handler" />
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using type="tagged_iterator", you should use a compiler pass to create the IteratorArgument (or drop the tag entirely and manage everything in the DI extension as I don't think we need to support handlers added elsewhere). This way, you can use weak references in the IteratorArgument, which will skip services which haven't been instantiated yet

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm familiar enough with the Container to change this right now but given #377 (comment) I should probably pause on this PR now anyway?

@stof
Copy link
Copy Markdown
Member

stof commented Nov 27, 2020

Regarding a generic solution in symfony, I think we need something similar to the ServiceResetter, but which is always called by the kernel on shutdown, for services which need to close some resources for their shutdown. This way, MonologBundle could hook the close method of handlers in there, DoctrineBundle could hook connections, etc...


use Monolog\Handler\HandlerInterface;

class HandlerLifecycleManager
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be tagged as @internal. We really don't want to expose it in the BC promise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class HandlerLifecycleManager
/**
* @internal
*/
class HandlerLifecycleManager

Copy link
Copy Markdown
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it still necessary?

public function close()
{
foreach ($this->handlers as $handler) {
$handler->close();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will instantiate all the handlers, even those that have not been used.

Comment thread MonologBundle.php
Comment on lines +60 to +62
$handlerManager = $this->container->get('monolog.handler_manager');

$handlerManager->close();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$handlerManager = $this->container->get('monolog.handler_manager');
$handlerManager->close();
$this->container->get('monolog.handler_manager')->close();


use Monolog\Handler\HandlerInterface;

class HandlerLifecycleManager
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class HandlerLifecycleManager
/**
* @internal
*/
class HandlerLifecycleManager

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.

5 participants