⬆️ ⬇️

Convert Zend config from ini to yaml. Underwater rocks

As a preface I will say that I always liked yaml. It so happened that I mostly work with the Zend Framework But unfortunately ZF did not support yaml for a long time. Then I added a simple class that was a wrapper for the symfony of the sfYaml component and began to use yaml quietly in my projects.



Finally I added Zend_Config_Writer_Yaml to ZF 1.11.12 and I decided to convert configs from ini to yaml





Without thinking twice, I found a ready-made script on the Internet. Looked like this.

')

$inputfile = APPLICATION_PATH . '/configs/application.ini'; $outputfile = APPLICATION_PATH. '/configs/application.yml'; $config = new Zend_Config_Ini($inputfile, null, array('allowModifications' => false, 'skipExtends'=> true)); $writer = new Zend_Config_Writer_Yaml(); $writer->write($outputfile, $config, true, true); 


Taxes, it looks simple, only ZF classes are used, no amateur performance is already good. I tried to convert. It turned out all right. Launched tests - everything works. Well, now let's do the same on the battle server.



After a while we began to receive an error from mysql. General error: 1205 Lock wait timeout exceeded; I try to execute a simple query and run into a lock. Well we look SHOW PROCESSLIST and we see nothing. It should be noted that the problem concerned only InnoDB tables, and 90-95% of us are MyISAM. Googled a little about this - try to do show engine innodb status. I see only not started threads i. There is no transaction that is running that could block other requests.

I try to pull admins - I learned nothing of value from them. They suggested adding indexes ... well, eprst.



Okay, you have to do something. We started using Gearman not so long ago - respectively, there are workers who are essentially demons and open long connections. Began to sin on them - let think restart them. And Lok helped out. But the reason is still not clear. Added a commit commit after completing each task - everything seems to be quiet.



The next day, I check the soap - the same story. Periodically similar errors arrive.



Here I just want to ask who has already guessed what happened?



In the morning, managers complain that the client cannot update the information. I check - there really is a request, but the data does not change. Immediately I recall yesterday's COMMITs forced. Well, what the hell is not joking, let's try to do

show variables like “% autocommit%”;

I get ON all right, the same php and ... drumming OFF. (up to this point, I confessed to the admins with all the bad words, but it turns out there is no fault myself)



In the end, everything turned out to be trite and simple.



Tax ... well, and how this could be all because it worked, we look in the configs and find a small detail.



resources.multidb.dbname.adapter = “pdo_mysql”

resources.multidb.dbname.host = “localhost”

resources.multidb.dbname.username = “user”

resources.multidb.dbname.password = “pass”

resources.multidb.dbname.dbname = “dbname”

resources.multidb.dbname.driver_options.1002 = “SET NAMES utf8;”



converted



 resources: 
   multidb: 
     dbname: 
       adapter: pdo_mysql
       host: localhost
       username: user
       password: pass
       dbname: dbname
       driver_options: 
         - SET NAMES utf8;




As you can see, when converting, 1002 turned into actually 0. Then, if we look a little deeper, we see

in Zend_Db

// PDO constant values ​​result

const ATTR_AUTOCOMMIT = 0;

...



And finally, the hero of the occasion - Zend_Config_Writer_Yaml

 /** * Service function for encoding YAML * * @param int $indent Current indent level * @param array $data Data to encode * @return string */ protected static function _encodeYaml($indent, $data) { reset($data); $result = ""; $numeric = is_numeric(key($data)); // look here foreach($data as $key => $value) { if(is_array($value)) { $encoded = "\n".self::_encodeYaml($indent+1, $value); } else { $encoded = (string)$value."\n"; } $result .= str_repeat(" ", $indent).($numeric?"- ":"$key: ").$encoded; // and look here } return $result; } 




I want to add that if I use SymfonyComponents / YAML / sfYaml.php, which I mentioned at the beginning of the article (I wrote a simple wrapper for it for easy use in ZF) and add to our example

$ writer-> setYamlEncoder (array ('App_Yaml', 'dump'));

then everything is converted correctly.



So friends be careful when it comes to configs.



Do not judge strictly - the first post on Habré.

Source: https://habr.com/ru/post/148574/



All Articles