使用Yii的CAssetsManager时,在AppFog上处理多个实例的更好方法

If you are running an Yii-app on Appfog with mulitple instances, and you are using CAssetsManager for your static files, you will (at least I did) experience issues with broken links to static files.

I concluded that the reason is that each instance on AppFog will have a different paths to each asset. Here's an example.

Instance 1: /mnt/var/vcap.local/dea/apps/54dc7546ac14/app/protected/assets/logo.png

Instance 2: /mnt/var/vcap.local/dea/apps/54dc7546ac14/app/protected/assets/logo.png

Instance 3: /mnt/var/vcap.local/dea/apps/54dc7546ac14/app/protected/assets/logo.png

When Yii publishes these assets they will get different paths, since each path is based on a hash that is based on dirname() returned by the function generatePath(). I know about the option hashByName, but setting it to true will share the published path among different extensions.

Therefore the public path to the logo.png will have three different possibilities:

Instance 1: https://www.example.com/assets/fb4gf4ac45/logo.png

Instance 2: https://www.example.com/assets/ab4ed4f394/logo.png

Instance 3: https://www.example.com/assets/fde07233bc/logo.png

The implication will be that when requesting www.example.com a arbitary instance will handle the request. Let's say it is instance 1, which will give a response with a link that refers to https://www.example.com/assets/fb4gf4ac45/logo.png. The link will trigger a second request, and this time another arbitary instance will handle the request (instance 2).

The problem is that the instance 2 cannot find assets/fb4gf4ac45/logo.png, since the file on this instance is located at assets/ab4ed4f394/logo.png.

This can be handled by using the S3AssetsManager, but it will still upload assets for each instance to S3, and that makes the site slow every time the codebase is updated. It also results in A LOT OF assets paths on S3.

THE QUESTION:

So, even though I do have a work around I wonder if there is a better way to handle multiple instances on Appfog without changing the Yii-source code.

You've got a couple of options. Aaron Francis' solution here is pretty nifty, but requires a fair number of moving parts: http://aaronfrancis.com/blog/2013/6/19/yii-and-the-asset-pipeline-part-2

It's also the most complete solution I've run into.

The second option is Jadon Hancock's available here. I've ended up adapting this to fit a project I'm working on. Not as elaborate or complete, but it was good enough for what I needed to start with. I may revisit Aaron's solution in the future. https://gist.github.com/jasonhancock/5073938