1

My cronjob is defined as follows:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job instance="Vendor\Module\Model\Status" method="update" name="vendor_module_status_update">
            <schedule>0 */2 * * *</schedule>
        </job>
    </group>
</config>

Class Vendor\Module\Model\Status exists and manual test below works fine:

<?php

declare(strict_types=1);

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Http as Http;
use Magento\Framework\AppInterface as AppInterface;

require '/path/to/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

class TestApp extends Http implements AppInterface
{
    public function launch()
    {
        $update = $this->_objectManager->get(Vendor\Module\Model\Status::class);
        $update->update();

        echo get_class($update);

        return $this->_response;
    }

    public function catchException(Bootstrap $bootstrap, Exception $exception): bool
    {
        return false;
    }
}

/** @var Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

However for some reason execution via cronjob throws:

Class Vendor\Module\Model\Status does not exist

Any idea what could be a reason?

0

1 Answer 1

1
  • Your class works with Object Manager but does not work when cron running because Vendor_Module module didn't enable yet. Try to check if Vendor_Module is in app/etc/config.php file, if so make sure Vendor_Module does not disable. If you didn't install that module, run setup:upgrade to install and enable the module. Then re-compile code and check the result.

  • If you tried the above check but the issue still persists, maybe the Status class has an incorrect file path or incorrect namespace.

The file path should be app/code/Vendor/Module/Model/Status.php

The namespace should be Vendor\Module\Model;

You can check Magento document for reference: https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/crons/custom-cron-tutorial.html

And it is worth adding Vendor\Module\Model\Status to a __construct() of a controller to check if the Class exists and works or not.

14
  • I don't think this is an issue because $this->_objectManager->get(Vendor\Module\Model\Status::class) is finding it w/o any issues. Commented Oct 17, 2022 at 18:59
  • Also you missed "code" directory in the path Commented Oct 17, 2022 at 19:08
  • Maybe case sensitive issue in the file path, or namespace. I've added "code" directory which was missed in the file path.
    – Tu Van
    Commented Oct 18, 2022 at 1:56
  • Checked it multiple times. Also will not explain why object manager works. Commented Oct 18, 2022 at 8:12
  • 1
    Do you think maybe Vendor_Module module didn't enable yet? Try to check if Vendor_Module in app/etc/config.php file to make sure Vendor_Module does not disable and if you didn't install that module, run setup:upgrade to install and enable the module. Then re-compile code and check the result
    – Tu Van
    Commented Oct 18, 2022 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.