Skip to content

No class "Key" in Lock/DeduplicateStamp #60510

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

Open
tacman opened this issue May 22, 2025 · 5 comments · May be fixed by #60512
Open

No class "Key" in Lock/DeduplicateStamp #60510

tacman opened this issue May 22, 2025 · 5 comments · May be fixed by #60512

Comments

@tacman
Copy link
Contributor

tacman commented May 22, 2025

Symfony version(s) affected

7.3.0-BETA2

Description

symfony/lock is not required in messenger, so if you use it you get an error.

How to reproduce

Install messenger and try to use the DeduplicateStamp stamp

Possible Solution

Probably check that the class exists and if not prompt with a note to run

 composer require symfony/lock

Additional Context

No response

@tacman
Copy link
Contributor Author

tacman commented May 22, 2025

Installing symfony/lock sets the .env var to flock

LOCK_DSN=flock

but that should be enough to serialize, right?

symfony new --webapp lock-bug --version=next && cd lock-bug
echo "DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db" > .env.local
composer req symfony/lock
mkdir src/Message -p
cat > src/Message/TestMessage.php <<'END'
<?php

namespace App\Message;

use Symfony\Component\Messenger\Attribute\AsMessage;

#[AsMessage('async')]
final class TestMessage
{
}
END

mkdir -p src/Command
cat > src/Command/DispatchCommand.php <<'END'
<?php

namespace App\Command;

use App\Message\TestMessage;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DeduplicateStamp;

#[AsCommand('app:dispatch', 'Dispatch with a dedup stamp')]
class DispatchCommand
{
        public function __construct(
        private readonly MessageBusInterface $messageBus,
    )
        {
        }


        public function __invoke(SymfonyStyle $io): int
        {
        $this->messageBus->dispatch(new TestMessage(), [
            new DeduplicateStamp('the-lock-resource')
        ]);
                return Command::SUCCESS;
        }
}

END

bin/console cache:clear
bin/console app:dispatch

Image

It occurs to me that the string 'the-lock-resource' might be a class and not a key to a class, like the cache component. I've never used the lock component before, so I was just using the example in the blog post at https://symfony.com/blog/new-in-symfony-7-3-messenger-improvements

Probably the real solution is to call it correctly.

Still, a message about installing symfony/lock would be helpful.

@norkunas
Copy link
Contributor

#60330

@xabbuh
Copy link
Member

xabbuh commented May 22, 2025

I think we can increase DX by requiring the key to be passed as a Key instance (see #60512). This way it's obvious when using the DeduplicateStamp that you need the Lock component.

@stof
Copy link
Member

stof commented May 22, 2025

@xabbuh not sure we should do that. If you reuse the same Key object to dispatch multiple messages, the 2 stamps will be considered identical rather than being duplicates (as they share the same Key).
There's a good reason why the default API of the Lock component handles the Key internally, exposing the Key directly only for cases where we need to be able to serialize the Key to transmit it (which is what the DeduplicateStamp needs to do internally).

What we should do instead is check for the class existing in the constructor and throw a meaningful exception.

@xabbuh
Copy link
Member

xabbuh commented May 22, 2025

@stof got it, PR updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants