craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
219 stars 170 forks source link

[4.x]: Invalid owner id thrown on update search indexes job #3711

Open ryssbowh opened 4 days ago

ryssbowh commented 4 days ago

What happened?

Description

We have a commerce website that creates carts programmatically depending on user actions. A user may choose an order of type "A", not go through with it, and then order a type "B". When that happens we delete the current cart and recreate it as it contains different products & custom fields.

Then jobs are spawned to update the search indexes for the addresses of the now deleted cart and they fail as the order doesn't exist anymore. the error I get is :

[yii\base\InvalidConfigException] yii\base\InvalidConfigException: Invalid owner ID: 853069 in /var/www/html/vendor/craftcms/cms/src/elements/Address.php:312
Stack trace:
#0 /var/www/html/vendor/craftcms/commerce/src/Plugin.php(663): craft\elements\Address->getOwner()
#1 [internal function]: craft\commerce\Plugin->craft\commerce\{closure}(Object(craft\events\DefineBehaviorsEvent))
#2 /var/www/html/vendor/yiisoft/yii2/base/Event.php(312): call_user_func(Object(Closure), Object(craft\events\DefineBehaviorsEvent))
#3 /var/www/html/vendor/yiisoft/yii2/base/Component.php(650): yii\base\Event::trigger('craft\\elements\\...', 'defineBehaviors', Object(craft\events\DefineBehaviorsEvent))
#4 /var/www/html/vendor/craftcms/cms/src/base/Model.php(114): yii\base\Component->trigger('defineBehaviors', Object(craft\events\DefineBehaviorsEvent))
#5 /var/www/html/vendor/yiisoft/yii2/base/Component.php(747): craft\base\Model->behaviors()
#6 /var/www/html/vendor/yiisoft/yii2/base/Component.php(660): yii\base\Component->ensureBehaviors()
#7 /var/www/html/vendor/craftcms/cms/src/base/Element.php(4197): yii\base\Component->getBehavior('customFields')
#8 /var/www/html/vendor/craftcms/cms/src/base/Element.php(4163): craft\base\Element->setFieldValue('orderAdditional...', NULL)
#9 /var/www/html/vendor/yiisoft/yii2/base/Component.php(180): craft\base\Element->setFieldValues(Array)
#10 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2203): yii\base\Component->__set('fieldValues', Array)
#11 /var/www/html/vendor/craftcms/cms/src/helpers/App.php(1441): craft\base\Element->__set('fieldValues', Array)
#12 /var/www/html/vendor/craftcms/cms/src/base/Model.php(85): craft\helpers\App::configure(Object(craft\elements\Address), Array)
#13 /var/www/html/vendor/craftcms/cms/src/base/Element.php(2109): craft\base\Model->__construct(Array)
#14 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(2136): craft\base\Element->__construct(Array)
#15 /var/www/html/vendor/craftcms/cms/src/elements/db/AddressQuery.php(286): craft\elements\db\ElementQuery->createElement(Array)
#16 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(3260): craft\elements\db\AddressQuery->createElement(Array)
#17 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1606): craft\elements\db\ElementQuery->_createElements(Array)
#18 /var/www/html/vendor/yiisoft/yii2/db/Query.php(251): craft\elements\db\ElementQuery->populate(Array)
#19 /var/www/html/vendor/craftcms/cms/src/db/Query.php(252): yii\db\Query->all(NULL)
#20 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1649): craft\db\Query->all(NULL)
#21 /var/www/html/vendor/craftcms/cms/src/queue/jobs/UpdateSearchIndex.php(59): craft\elements\db\ElementQuery->all()
#22 /var/www/html/vendor/yiisoft/yii2-queue/src/Queue.php(243): craft\queue\jobs\UpdateSearchIndex->execute(Object(craft\queue\Queue))
#23 /var/www/html/vendor/yiisoft/yii2-queue/src/cli/Queue.php(147): yii\queue\Queue->handleMessage(1291128, 'O:34:"craft\\que...', 300, 1)
#24 /var/www/html/vendor/craftcms/cms/src/queue/Queue.php(191): yii\queue\cli\Queue->handleMessage(1291128, 'O:34:"craft\\que...', 300, 1)
#25 /var/www/html/vendor/craftcms/cms/src/queue/Queue.php(166): craft\queue\Queue->executeJob()
#26 [internal function]: craft\queue\Queue->craft\queue\{closure}(Object(Closure))
#27 /var/www/html/vendor/yiisoft/yii2-queue/src/cli/Queue.php(114): call_user_func(Object(Closure), Object(Closure))
#28 /var/www/html/vendor/craftcms/cms/src/queue/Queue.php(164): yii\queue\cli\Queue->runWorker(Object(Closure))
#29 /var/www/html/vendor/craftcms/cms/src/controllers/QueueController.php(82): craft\queue\Queue->run()
#30 [internal function]: craft\controllers\QueueController->actionRun()
#31 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#32 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#33 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('run', Array)
#34 /var/www/html/vendor/craftcms/cms/src/web/Application.php(341): yii\base\Module->runAction('queue/run', Array)
#35 /var/www/html/vendor/craftcms/cms/src/web/Application.php(640): craft\web\Application->runAction('queue/run', Array)
#36 /var/www/html/vendor/craftcms/cms/src/web/Application.php(303): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#37 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#38 /var/www/html/web/index.php(12): yii\base\Application->run()
#39 {main}

I've tried to bypass those jobs with something like this :

Event::on(Elements::class, Elements::EVENT_BEFORE_UPDATE_SEARCH_INDEX, function (Event $e) {
    if ($e->element instanceof Address) {
        try {
            $e->element->getOwner();
        } catch (InvalidConfigException $ex) {
            $e->isValid = false;
        }
    }
});

But that doesn't work, somehow the order is still set as an owner on the address and the job goes through.

Steps to reproduce

  1. delete a user cart and recreate it in the same request

Expected behavior

The jobs are either not spawned or go through

Actual behavior

Jobs are failing

Craft CMS version

4.12.4

Craft Commerce version

4.6.12

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

linear[bot] commented 4 days ago

PT-2199 [4.x]: Invalid owner id thrown on update search indexes job

nfourtythree commented 3 days ago

Hi @ryssbowh

Thank you for your message, this sounds like a bit of an odd issue.

A couple of quick follow-up questions about when you are deleting/recreating the cart:

Thanks

ryssbowh commented 3 days ago

Hi @nfourtythree

The order is soft deleted with Craft::$app->getElements()->deleteElementById($cart->id)

We don't copy anything on the new cart, either the users have primary addresses and their are automatically assigned to it or they don't but they would see an error later in the workflow

nfourtythree commented 3 days ago

Hi @ryssbowh

Thank you for the answers.

Looking at the stack trace it says "Invalid owner ID: 853069", is that the ID of the order that you just deleted?

Thanks

ryssbowh commented 2 days ago

Yes that's correct @nfourtythree

nfourtythree commented 2 days ago

Hi @ryssbowh

Unfortunately, we are unable to replicate this in any way.

Could you please send a database backup and your composer files to support@craftcms.com, referencing this issue so we can look into this further?

Thanks!