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

[messenger][DX] restart consumer when code changed in debug mode #29451

Open
mathroc opened this issue Dec 4, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@mathroc
Copy link
Contributor

commented Dec 4, 2018

Hi,

when working, it would be nice if the consumer would restart automatically when code (or configuration, etc.) has been updated

I've tried to write a middleware that would check if the cache is fresh and stop the command if not (in my case docker-compose would restart it) but I don't know how to check for that

here is what I tried:

<?php declare(strict_types=1);

namespace App\Messenger;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

class CodeChangedMiddleware implements MiddlewareInterface
{
    /** @var Kernel */
    private $kernel;

    public function __construct(KernelInterface $kernel)
    {
        assert($kernel instanceof Kernel);
        $this->kernel = $kernel;
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        if (!$this->cacheIsFresh()) {
            throw new \RuntimeException("Restarting consumer to update cache");
        }

        return $stack->next()->handle($envelope, $stack);
    }

    private function cacheIsFresh(): bool
    {
        // trying to mimick what's happening at the beginning of Kernel::initializeContainer
        $ro = new \ReflectionObject($this->kernel);
        $getContainerClass = $ro->getMethod("getContainerClass");
        $getContainerClass->setAccessible(true);

        $class = $getContainerClass->invoke($this->kernel);

        $warmupDirProperty = $ro->getParentClass()->getProperty("warmupDir");
        $warmupDirProperty->setAccessible(true);
        $cacheDir = $warmupDirProperty->getValue($this->kernel) ?: $this->kernel->getCacheDir();

        $debugProperty = $ro->getProperty("debug");
        $debugProperty->setAccessible(true);
        $cache = new ConfigCache("$cacheDir/$class.php", $debugProperty->getValue($this->kernel));

        return $cache->isFresh();
    }
}

but this cache is always fresh

Do you think this is something worth integrating into symfony ? (maybe directly into the consumer command instead)

and, any idea why the middleware I wrote does not detect code changes ?

fabpot added a commit that referenced this issue Mar 31, 2019

feature #30754 [Messenger] New messenger:stop-workers Command (weaver…
…ryan)

This PR was squashed before being merged into the 4.3-dev branch (closes #30754).

Discussion
----------

[Messenger] New messenger:stop-workers Command

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | Kinda of #29451
| License       | MIT
| Doc PR        | symfony/symfony-docs#11236

o/ me again.

This requires and is built on top of #30708

When you deploy, all workers need to be stopped and restarted. That's not currently possible, unless you manually track the pids and send a SIGTERM signal. We can make that much easier :).

Now run:

```
bin/console messenger:stop-workers
```

And it will signal to all workers (even if they're distributed on other servers) that they should stop, once they finish processing their current message. This is done via a key in `cache.app`.

Cheers!

Commits
-------

5897162 [Messenger] New messenger:stop-workers Command
@Simperfit

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2019

@weaverryan is this fixed ?

@weaverryan

This comment has been minimized.

Copy link
Member

commented Apr 11, 2019

It is not, but is interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.