Closed ericdrosas87 closed 7 months ago
Accordingly to @timkelty this should not be happening with each page request, though that is indeed what I am seeing.
@ericdrosas87 after going things over with @brandonkelly, I was mistaken and this is indeed expected. That said, these are extremely lightweight queries and unlikely to contribute meaningfully to performance.
Yii's asset bundles include a hash, and when load-balancing is in the mix, Craft has no way of knowing how to map the hash to an asset bundle source path, hence the existence of the resourcepaths
.
We've considering moving this to be stored exclusively with the cache component, but that wouldn't necessarily be any faster, as in load-balanced envs, this usually means a network request of some sort (sounds like Redis in your case).
Configuring \craft\web\AssetManager::$cacheSourcePaths = false
will prevent these writes, but will also cause CP assets to break in a multi-server/load-balanced env.
I appreciate you looking into this @timkelty , I have a few follow-up questions:
Configuring \craft\web\AssetManager::$cacheSourcePaths = false will prevent these writes, but will also cause CP assets to break in a multi-server/load-balanced env.
So the control panel will still function as expected, but assets will be missing? Will Craft still serve up the graphQL /api
endpoint data as expected?
Can you foresee any issues with only setting \craft\web\AssetManager::$cacheSourcePaths = false
for the read-only regional stack we are setting up? By "read-only" I mean we don't anticipate content creators to use the Craft CMS in the region we are currently standing up, we just need it to serve up data from the read replica databases.
So the control panel will still function as expected, but assets will be missing? Will Craft still serve up the graphQL /api endpoint data as expected?
CSS and JS for the control panel may be missing depending on which server handles those requests, and if they have the
assets published to the cpresources
directory.
The gql endpoint would be unaffected.
Can you foresee any issues with only setting \craft\web\AssetManager::$cacheSourcePaths = false for the read-only regional stack we are setting up?
Asset bundles are primarily used by the control panel, however, some plugins register them to inject into front end Twig templates. Those should be the only cases to be concerned with. Sounds like maybe you're running headless w/ GQL, so that may not even be a concern.
Sounds like maybe you're running headless w/ GQL, so that may not even be a concern.
Yep we're running headless. We will give that a shot and I'll report back if we see any issues, thank you again!
Sounds good!
Sorry to dig this back up from the dead, but I would just like to confirm that the following code snippet will also work?:
'components' => [
'assetManager' => function() {
# Get default config:
$config = craft\helpers\App::assetManagerConfig();
# Set custom property:
$config['cacheSourcePaths'] = (bool)App::env('DISABLE_SOURCE_PATHS') ?: true;
# Create + return component:
return Craft::createObject($config);
},
// rest of configs
Specifically asking if setting the AssetManager config will work this way as well as the way you suggested (\craft\web\AssetManager::$cacheSourcePaths = false
)
@ericdrosas87 the closure looks right, but (bool)App::env('DISABLE_SOURCE_PATHS') ?: true
will always evaluate to true
.
Try replacing it with:
$config['cacheSourcePaths'] = App::parseBooleanEnv('$DISABLE_SOURCE_PATHS') ?? true;
@timkelty Good catch! Thank you!
What happened?
Description
Upon logging into the Craft control panel, approximately 20 DB writes are made to the database by the AssetManager. Accordingly to @timkelty this should not be happening with each page request, though that is indeed what I am seeing.
We are hosting this in Google Cloud's Kubernetes Engine with Redis and the Google Cloud Storage plugin for assets and asset variants.
Apache info:
Server version: Apache/2.4.57 (Debian) Server built: 2023-04-13T03:26:51
Steps to reproduce
Alternatively for local dev:
Craft::info()
log to thehash()
function incraft\web\AssetManager
to see the number of the upserts being performed with each request in thestdout
log.tail -f
the logsThese same upserts are seen with each page request and sometimes additional upserts are seen in the logs.
Expected behavior
Minimal if any DB writes per page request
Actual behavior
The same paths/hashes are passed to the upsert with almost every page request in the control panel.
Craft CMS version
4.5.4
PHP version
8.1
Operating system and version
Linux 5.15.65+
Database type and version
PostgreSQL 13.7
Image driver and version
Imagick 3.7.0 (ImageMagick 6.9.11-60)
Installed plugins and versions
-