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

[4.x] Element query executed before Craft is fully initialized #249

Closed
ccchapman opened this issue Oct 17, 2022 · 5 comments
Closed

[4.x] Element query executed before Craft is fully initialized #249

ccchapman opened this issue Oct 17, 2022 · 5 comments

Comments

@ccchapman
Copy link

ccchapman commented Oct 17, 2022

A warning can be logged when the ScoutIndex::criteria calls $query->site().

config/scout.php

<?php

use craft\elements\Entry;
use craft\elements\db\EntryQuery;
use craft\helpers\App;
use rias\scout\ScoutIndex;

return [
    "indices" => [
        ScoutIndex::create(App::env("ALGOLIA_INDEX_NAME"))
            ->criteria(function (EntryQuery $query) {
                return $query->site("*");
            })
            ->elementType(Entry::class)
            ->transformer(function (Entry $entry) {
                return [
                    //
                ];
            }),
    ],
];

storage/logs/web.log

2022-10-17 18:25:34 [web.WARNING] Element query executed before Craft is fully initialized. in /vendor/craftcms/cms/src/elements/db/ElementQuery.php:1259
Stack trace:
#0 /vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare(Object(craft\db\pgsql\QueryBuilder))
#1 /vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build(Object(craft\elements\db\UserQuery))
#2 /vendor/yiisoft/yii2/db/Query.php(287): yii\db\Query->createCommand(Object(craft\db\Connection))
#3 /vendor/craftcms/cms/src/db/Query.php(274): yii\db\Query->one(NULL)
#4 /vendor/craftcms/cms/src/elements/db/ElementQuery.php(1558): craft\db\Query->one(NULL)
#5 /vendor/craftcms/cms/src/elements/User.php(532): craft\elements\db\ElementQuery->one()
#6 /vendor/yiisoft/yii2/web/User.php(601): craft\elements\User::findIdentity(1)
#7 /vendor/yiisoft/yii2/web/User.php(320): yii\web\User->getIdentityAndDurationFromCookie()
#8 /vendor/yiisoft/yii2/web/User.php(726): yii\web\User->loginByCookie()
#9 /vendor/craftcms/cms/src/web/User.php(500): yii\web\User->renewAuthStatus()
#10 /vendor/yiisoft/yii2/web/User.php(198): craft\web\User->renewAuthStatus()
#11 /vendor/yiisoft/yii2/web/User.php(366): yii\web\User->getIdentity()
#12 /vendor/craftcms/cms/src/web/User.php(204): yii\web\User->getIsGuest()
#13 /vendor/craftcms/cms/src/services/Sites.php(1237): craft\web\User->getIsGuest()
#14 /vendor/craftcms/cms/src/services/Sites.php(417): craft\services\Sites->_allSites(NULL)
#15 /vendor/craftcms/cms/src/elements/db/ElementQuery.php(882): craft\services\Sites->getAllSiteIds()
#16 /config/scout.php(81): craft\elements\db\ElementQuery->site('*')
#17 /vendor/studioespresso/craft-scout/src/ScoutIndex.php(56): craft\services\Config->{closure}(Object(craft\elements\db\EntryQuery))
#18 /config/scout.php(83): rias\scout\ScoutIndex->criteria(Object(Closure))
#19 /vendor/craftcms/cms/src/services/Config.php(289): include('...')
#20 /vendor/craftcms/cms/src/services/Config.php(281): craft\services\Config->_configFromFileInternal('...')
#21 /vendor/craftcms/cms/src/services/Plugins.php(934): craft\services\Config->getConfigFromFile('scout')
#22 /vendor/craftcms/cms/src/services/Plugins.php(228): craft\services\Plugins->createPlugin('scout', Array)
#23 /vendor/craftcms/cms/src/base/ApplicationTrait.php(1477): craft\services\Plugins->loadPlugins()
#24 /vendor/craftcms/cms/src/web/Application.php(105): craft\web\Application->_postInit()
#25 /vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#26 /vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct(Array)
#27 [internal function]: yii\base\Application->__construct(Array)
#28 /vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs(Array)
#29 /vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#30 /vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#31 /vendor/craftcms/cms/src/Craft.php(53): yii\BaseYii::createObject(Array, Array)
#32 /vendor/craftcms/cms/bootstrap/bootstrap.php(239): Craft::createObject(Array)
#33 /vendor/craftcms/cms/bootstrap/web.php(40): require('...')
#34 /web/index.php(11): require('...')
#35 {main} {"memory":27958128,"exception":"[object] (ErrorException(code: 0): Element query executed before Craft is fully initialized. at /vendor/craftcms/cms/src/elements/db/ElementQuery.php:1259)"} 

Refs

@janhenckens
Copy link
Member

What sort of request of action are you doing when seeing this error @ccchapman?

@ccchapman
Copy link
Author

@janhenckens I believe it was for every single web request.

janhenckens added a commit that referenced this issue Jan 9, 2023
Signed-off-by: Jan Henckens <jan@henckens.be>
@hatyi
Copy link

hatyi commented Feb 7, 2023

Hello I think we are experiencing this issue as well, is there any chance for a fix for this anytime soon?
We've just upgraded to Craft 4. Can provide any info necessary, I've just reached the conclusion that Scout is causing the errors.
Technically we get a pretty long log entry in our web.log file related to this ElementQuery error in almost every second.

Here is stack trace of one request:

[{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/elements/db/ElementQuery.php","line":1242,"function":"warning","class":"yii\\BaseYii","type":"::"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/db/Query.php","line":275,"function":"one","class":"yii\\db\\Query","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/elements/db/ElementQuery.php","line":1506,"function":"one","class":"craft\\db\\Query","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/elements/User.php","line":527,"function":"one","class":"craft\\elements\\db\\ElementQuery","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/web/User.php","line":489,"function":"renewAuthStatus","class":"yii\\web\\User","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/web/User.php","line":204,"function":"getIsGuest","class":"yii\\web\\User","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Sites.php","line":1237,"function":"getIsGuest","class":"craft\\web\\User","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Sites.php","line":417,"function":"_allSites","class":"craft\\services\\Sites","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/elements/db/ElementQuery.php","line":880,"function":"getAllSiteIds","class":"craft\\services\\Sites","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/config/scout.php","line":26,"function":"site","class":"craft\\elements\\db\\ElementQuery","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/studioespresso/craft-scout/src/ScoutIndex.php","line":56,"function":"{closure}","class":"craft\\services\\Config","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/config/scout.php","line":29,"function":"criteria","class":"rias\\scout\\ScoutIndex","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Config.php","line":289,"function":"include"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Config.php","line":281,"function":"_configFromFileInternal","class":"craft\\services\\Config","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Plugins.php","line":934,"function":"getConfigFromFile","class":"craft\\services\\Config","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/services/Plugins.php","line":228,"function":"createPlugin","class":"craft\\services\\Plugins","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/base/ApplicationTrait.php","line":1494,"function":"loadPlugins","class":"craft\\services\\Plugins","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/web/Application.php","line":105,"function":"_postInit","class":"craft\\web\\Application","type":"->"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/src/Craft.php","line":53,"function":"createObject","class":"yii\\BaseYii","type":"::"},{"file":"/home/748786.cloudwaysapps.com/xnpnrkejxu/public_html/vendor/craftcms/cms/bootstrap/bootstrap.php","line":252,"function":"createObject","class":"Craft","type":"::"}],"memory":8090352}

@humandevs
Copy link

humandevs commented Feb 15, 2023

You should be able to get the fix for now by using the develop branch, as:

"studioespresso/craft-scout": "develop as 3.0.0",

It would be nice to have this change tagged as a beta version as the logs are quite noisy. Also the develop branch has the requirement as Craft 4.3, which I'm not sure is actually required. I'm working on upgrading to Craft 4 now as well, to get some of the deadlock fixes.

@janhenckens
Copy link
Member

Hey everyone, sorry about the delay, I had the fix ready on develop but totally forgot to make a release for it. I just tagged 3.1.0 which includes this fix.

The fix referenced here was added in Craft 4.3.5 so I'm bumping the minimum version for this release to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants