Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssetManager problem in cloud platform #8

Closed
kamarton opened this issue Aug 28, 2019 · 11 comments
Closed

AssetManager problem in cloud platform #8

kamarton opened this issue Aug 28, 2019 · 11 comments
Labels
type:docs Documentation
Milestone

Comments

@kamarton
Copy link

What steps will reproduce the problem?

Yii2 asset manager specified to any file storage and file serving. There is a small problem with this in a cloud platform.

  server A          server B            server C   ....     server X
     |                 |                   |                   |
     \---------------------------------------------------------/
                                |
                               LB
                                |
                          client browser

Yii2 schema:

  1. client make request
  2. server A
  3. asset managerer copy or symlink files on server A
  4. response
  5. client make new request with asset urls
  6. server B, C (LB select random servers)
  7. not found asset files (onlye copy files server A)

Idea:

  • It would be nice to have an easy-to-implement solution to not direct file serving.
  • serving asset contents
    • from action (read files from direct path, without copy to asset folder)
    • support CDN PUT or another storage (redis, database)

What is the expected result?

files serving any servers

What do you get instead?

not found asset files (onlye copy files server A)

Additional info

yii2 solution (with my source codes):

  1. asset manager put base directory to databse
  2. asset serving action
  3. get base directory from database
  4. read and serving content from base directory + url path (with file checks and more)
Q A
Version 2.x
PHP version -
Operating system -
@SamMousa
Copy link

I have built this: https://github.com/SAM-IT/yii2-static-assets

Things that are reusable for this use case include:

  • ReadOnlyAssetManager, a manager that calculates the URL but doesn't do any publishing.
  • AssetHelper, a helper class that allows you to find asset bundles in your code base and publish them using an asset manager of your choice.

https://github.com/SAM-IT/yii2-static-assets/blob/master/src/helpers/AssetHelper.php#L241

This could be used to publish to a directory on S3 during build.

@samdark samdark transferred this issue from yiisoft/yii-web Aug 28, 2019
@mikk150
Copy link

mikk150 commented Oct 1, 2019

I am using https://gitlab.com/mikk150/yii2-asset-manager-flysystem

I had same issue, but even kind of worse, because I do not know(or care) about servers or how many of them I have(auto scaling applications based on load, by purchasing cheapest one from amazon, azure or digitalocean by bidding).. This is uploading them to CDN and CDN deals with it's thing

@terabytesoftw
Copy link
Member

terabytesoftw commented Dec 5, 2019

@kamarton @mikk150 @SamMousa @samdark @rustamwin @roxblnfk I have been working with assets, and that problem with flysystems is effectively eliminated, since it works with relative routes and part of it does not work with symlinks, now just working with the local adapter would be completely resolved, they think it is worth implementing that you can work with two adapters at the same time, example:

$mountManager->copy('local://sourcePath/file.ext', 'publish://basePath/file.ext');

Where local can be the local adapter and publish could be local, aws, digital ocean, google cloud storage, or any other adapter.

On the other hand, the other version problem can be solved by adding at the end of the hash 'v0' -> default version, and it can be customized in the assetBundle 'v1' ... 'v99', example hashv0 or (hashv1 ... hashv99).

@SamMousa
Copy link

SamMousa commented Dec 5, 2019

I'm not sure how this is relevant @terabytesoftw, I'm assuming the flysystem asset manager already publishes data to any flysystem filesystem; how would having support for multiple help?

Note that there are already composite adapters that will allow you to write to multiple filesystems (local & remote) and then read with fallback (local first, remote otherwise):
https://flysystem.thephpleague.com/docs/adapter/replicate/
https://github.com/Litipk/flysystem-fallback-adapter

I'm not sure how having a mount manager that copies between paths is relevant here.
(I think I'm missing some context)

@samdark samdark transferred this issue from yiisoft/view Dec 19, 2019
@samdark samdark added this to the 1.0.0-alpha1 milestone Dec 27, 2019
@samdark
Copy link
Member

samdark commented Sep 28, 2020

This is valid and serious problem that could be solved in multiple ways @SamMousa and @mikk150 suggested:

  1. By sharing assets by writing them somewhere else like CDN or S3 bucket using https://github.com/yiisoft/yii-filesystem
  2. By pre-collecting assets and publishing these during deployment. In this case we need to ensure that all servers have assets at the same path.
  3. By pre-collecting assets and publishing these during development. Ready assets are then deployed to the servers.

Related #24 may greatly simplify asset list assembly for 2 and 3.

@SamMousa
Copy link

I think for the future we should do pre publishing. Moving to containers and immutable deploy artifacts is always safer. Even when wanting to deploy assets to a cdn doing so in advance is better.

@kamarton
Copy link
Author

This is valid and serious problem that could be solved in multiple ways @SamMousa and @mikk150 suggested:

I like the idea.

By pre-collecting assets and publishing these during deployment. In this case we need to ensure that all servers have assets at the same path.

An interesting question here is whether it is always known in advance or not the path. Forces the deploy to receive a different route (path) from the previous state in each case.

For example, if the asset files of one package have not changed for 2 years, it will destroy the CDN function in an intensive development status by e.g. forces downloads to mobile devices up to several times a day.
This may not be a problem in the process, but developers need a guide in this area, otherwise they may run into this problem more easily.

@SamMousa
Copy link

For example, if the asset files of one package have not changed for 2 years, it will destroy the CDN function in an intensive development status by e.g. forces downloads to mobile devices up to several times a day.
This may not be a problem in the process, but developers need a guide in this area, otherwise they may run into this problem more easily.

You could still use the file hash as input for path generation, doing it in advance is no different from doing it at runtime..

@samdark
Copy link
Member

samdark commented Oct 14, 2020

I've recently wrote down about how to solve it in Yii 2: https://rmcreative.ru/blog/post/yii-2-za-balansirovschikom-nagruzki It is in Russian but may be relevant.

@samdark samdark added type:docs Documentation and removed status:under discussion type:feature New feature labels Mar 31, 2021
@samdark
Copy link
Member

samdark commented Mar 31, 2021

Seems to be possible with current master. Need to document it.

@samdark
Copy link
Member

samdark commented Apr 20, 2021

This one is now solved. An example added to readme.

@samdark samdark closed this as completed Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

5 participants