upwork / phystrix

Phystrix is a latency and fault tolerance library for PHP, inspired by Netflix’s Hystrix
Apache License 2.0
1 stars 0 forks source link

Passing custom configuration parameters to a command #35

Closed jerryxh closed 5 years ago

jerryxh commented 5 years ago

Per the Phystrix README page, you should be able to add a custom, command-specific configuration (eg, 'MyCommand' => array('queryServices' => array( 'timeout' => 10) )) to the configuration array and access it in the run() method of the MyCommand class (which extends AbstractClass) as follows:

$timeout = $this->config->get('MyCommand')->get('queryServices')->get('timeout');

However, this is not possible because the following line in the initializeConfig() method of the AbstractClass class restricts the configuration to the "default" configuration:

$config = new Config($phystrixConfig->get('default')->toArray(), true);

This should either be fixed or rectified in the documentation.

lcf commented 5 years ago

Hi @jerryxh , sorry about the delay!

Here is the entire code of the initializeCommand method:

    public function initializeConfig(Config $phystrixConfig)
    {
        $commandKey = $this->getCommandKey();
        $config = new Config($phystrixConfig->get('default')->toArray(), true);
        if ($phystrixConfig->__isset($commandKey)) {
            $commandConfig = $phystrixConfig->get($commandKey);
            $config->merge($commandConfig);
        }
        $this->config = $config;
    }

as you can see, it will merge command-specific configuration with the default one IF command-specific configuration is available.

I hope that helps! If you can't get it working please re-open or file a new ticket with some more information about how to reproduce and I will take a look.

I can confirm that we use this feature and it definitely works!