Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Cannot force configuration to read multiplier from env #62370

@evolic

Description

@evolic

Symfony version(s) affected

6.4.28,7.3.6

Description

Today I wanted to set two new environment variables:

MESSENGER_MAX_RETRIES=3
MESSENGER_MULTIPLIER=2

Unfortunately I am not able to set multiplier in my async transport to use the second value.

I am getting error:

In NumericNode.php line 45:
                                                                                                                                            
  The value 0 is too small for path "framework.messenger.transports.async.retry_strategy.multiplier". Should be greater than or equal to 1

So the problem is that value is set to zero, although I set it as positive number e.g. 2.

How to reproduce

  1. Configure .env
MESSENGER_MAX_RETRIES=3
MESSENGER_MULTIPLIER=2
  1. Configure config/services.yaml
    messenger_multiplier: 5
#    messenger_multiplier: '%env(float:MESSENGER_MULTIPLIER)%'
  1. Configure async transport in config/packages/messenger.yaml
framework:
    messenger:
        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                # default configuration
                retry_strategy:
                    max_retries: '%env(int:MESSENGER_MAX_RETRIES)%'
                    # milliseconds delay
                    delay: 1000
                    # causes the delay to be higher before each retry
                    # e.g. 1 second delay, 2 seconds, 4 seconds
                    multiplier: '%messenger_multiplier%'
                    max_delay: 0
                    # override all of this with a service that
                    # implements Symfony\Component\Messenger\Retry\RetryStrategyInterface
                    # service: null
                failure_transport: failed_default

            # failed: 'doctrine://default?queue_name=failed'
            sync: 'sync://'

            failed_default:
                dsn: '%env(MESSENGER_FAILED_DSN)%'

        failure_transport: failed_default


        routing:
            # Route your messages to the transports
            'App\Message\SmsNotification': async
  1. Add message maker command src/Command/SendSmsCommand.php
<?php

declare(strict_types=1);

namespace App\Command;

use App\Message\SmsNotification;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsCommand(name: 'app:send-sms', description: 'Hello PhpStorm')]
class SendSmsCommand extends Command
{
    public function __construct(
        private MessageBusInterface $messageBus,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $message = new SmsNotification('Hello PhpStorm!');

        $this->messageBus->dispatch($message);

        return Command::SUCCESS;
    }
}
  1. Add some message class src/Message/SmsNotification.php
<?php

declare(strict_types=1);

namespace App\Message;

class SmsNotification
{
    public function __construct(
        private string $content,
    ) {
    }

    public function getContent(): string
    {
        return $this->content;
    }
}
  1. And its handler src/MessageHandler/SmsNotificationHandler.php:
<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\SmsNotification;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class SmsNotificationHandler
{
    public function __invoke(SmsNotification $message)
    {
        throw new \RuntimeException('This is a demo message handler.');

        echo 'Processing: ' . $message->getContent(), PHP_EOL;
    }
}

Possible Solution

I cannot say what causes the problem.

I wrote some additional code to check if I can inject both environment variables into some constructor and the only thing that is not working is Symfony Messenger.

I don't know if it is my mistake or there is some bug.

Additional Context

I was trying to set multiplier in few ways:

  • by '%env(float:MESSENGER_MULTIPLIER)%' - not working
  • by fixed value e.g. 3 - works fine
  • by using '%messenger_multiplier%' - works fine only if messenger_multiplier is set to fixed value e.g. 3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions