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 not working if using processor on same resource #5772

Closed
DurandA opened this issue Aug 23, 2023 · 3 comments
Closed

Messenger not working if using processor on same resource #5772

DurandA opened this issue Aug 23, 2023 · 3 comments

Comments

@DurandA
Copy link

DurandA commented Aug 23, 2023

API Platform version(s) affected: 3.1

Description
When a resource use both the messenger and a custom processor, either the messages are not created if using messenger=true, or the processor is not called if using messenger='input'.

How to reproduce

#[ORM\Entity]
#[ApiResource(
    operations: [
        new Post(
            processor: MyEntityProcessor::class,
            messenger: 'input',
            deserialize: false, 
        )
    ]
)]
class MyEntity
{
}
final class MyEntityProcessor implements ProcessorInterface
{
    public function __construct(private ProcessorInterface $persistProcessor)
    {
    }

    public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
    {
        $result = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
        return $result;
    }
}

services.yaml

services:
    App\State\MyEntityProcessor:
        bind:
            $persistProcessor: '@api_platform.doctrine.orm.state.item_provider'

messenger.yaml

framework:
    messenger:
        transports:
            async: 'doctrine://default'

        routing:
            'App\Entity\MyEntity': async

Additional Context
I need to generate thumbnails on MediaObjects after creation. I want to use Symfony Messenger to trigger this and do it asynchronously since it could take time some to process.

The processor is used to persists the entity. As described in Symfony Messenger Integration:

Note: when using messenger=true ApiResource attribute in a Doctrine entity, the Doctrine Processor is not called. If you want the Doctrine Processor to be called, you should decorate a built-in state processor and implement your own logic.

@DurandA DurandA changed the title Async messenger not working if using processor on same resource Messenger not working if using processor on same resource Aug 24, 2023
@soyuka
Copy link
Member

soyuka commented Aug 24, 2023

decorate the Messenger processor...

@soyuka soyuka closed this as completed Aug 24, 2023
@DurandA
Copy link
Author

DurandA commented Aug 24, 2023

@soyuka You mean something like this in the message handler?

final class MyEntityHandler implements MessageHandlerInterface
{

    public function __construct(private ProcessorInterface $persistProcessor)
    {
    }

    public function __invoke(MediaObject $mediaObject)
    {
        // call $this->persistProcessor->process()
    }
}

The problem with this solution is that the messenger will not be called immediately if it is asynchronous. I need a processor that is called immediately and a message handler that is called asynchronously since it could take several seconds to execute.

@darthf1
Copy link
Contributor

darthf1 commented Aug 26, 2023

Can you not dispatch an event through the message bus, after the entity is persisted? The event handler can dispatch the GenerateThumbnailCommand, or just execute your logic in the event handler.

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

No branches or pull requests

3 participants