dongxianghui3709 2013-09-28 18:16
浏览 26

使用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.

  • 写回答

1条回答 默认 最新

  • doudu2404 2013-09-29 03:28
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示