Monday, June 14, 2010

Symfony config server

The other day at work, we implemented gearman for doing certain expensive data updates in the background. The workers are written in PHP and run as daemons, and our app calls them with PHP gearman client's function doBackground()

We also use Symfony, which in turn uses YAML for its configuration information. Our gearman workers need access to a bunch of config variables from symfony app.yml files (we have multiple applications/environments for our app).

It could be done by either using Syck within the PHP workers, or by sourcing Symfony framework and using sfConfig::get() calls. However, this doesn't work very well if gearman servers and workers live on a separate set of machines, and so in my continuing effort to learn more Python I wrote a simple config server.
Its very simple - all it does is daemonize itself and listen on a socket you specify on command line reading app.yml parameters from a symfony path you specify, so in our case each web server runs the config server, and PHP workers connect via TCP workers to read the configs needed.

To get a config value, all you have to do is open a TCP socket to the config server and issue something like:
All data is returned in JSON format.

You can also switch environments and apps by issuing commands like:
By default, the app is frontend and env is prod.

To start:
python -l -p 1025 -s my_symfony _app

[root@hydra ~] # telnet localhost 1025
Connected to localhost.
Escape character is '^]'.
{"secret": "xxx", "apikey": "yyy", "type": "web", "canvasurl": "\/myapp\/", "appid": 1234}

Another way of doing this is of course to pass config parameters to the workers themselves when calling them from clients, but that would be too boring : )

I tested it with Symfony 1.0 and I'm pretty sure it would work with any 1.x release.
If you actually use something so obscure and find bugs in it, let me know or just fork and fix!

No comments:

Post a Comment