2

I tried to use a virtual type in cronjob as instance but without any success. When I use a real class it works as expected. Is there any hint to get virtual types working in cronjobs?

crontab.xml:

<?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="mygroup">
        <job name="myjob" instance="Vendor\Package\MyClass" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Vendor\Package\Model\VirtualLoggerHandler" type="Magento\Framework\Logger\Handler\Base">
        <arguments>
            <argument name="fileName" xsi:type="string">/var/log/virtual.log</argument>
        </arguments>
    </virtualType>

    <virtualType name="Vendor\Package\Model\VirtualLogger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="name" xsi:type="string">virtual</argument>
            <argument name="handlers" xsi:type="array">
                <item name="debug" xsi:type="object">Vendor\Package\Model\VirtualLoggerHandler</item>
                <item name="error" xsi:type="object">Vendor\Package\Model\VirtualLoggerHandler</item>
                <item name="critical" xsi:type="object">Vendor\Package\Model\VirtualLoggerHandler</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="Vendor\Package\MyClass" type="Vendor\Package\BaseClass" shared="true">
        <arguments>
            <argument name="logger" xsi:type="object">Vendor\Package\Model\VirtualLogger</argument>
        </arguments>
    </virtualType>
</config>

  • Please share the code which you have tried – Amit Bera May 23 at 10:33
  • added code examples – Enrico Thies May 23 at 10:55
  • I know it's kind of old, but I wansn't able to get this working. I end up in virtual type hell. CLI commands as virtual types work flawlessly, crons not so much and I have no idea why. For some reason, the arguments that are also virtual types of the main cron virtual type don't get processed anymore. – Marius Sep 3 at 13:36
1

So I did a little more digging.
The result is that the virtual types work nicely for cron configuration. The problem for me what that I was configuring the virtual types wrong.

Here is my cron configuration

<?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 name="import_simple_products" instance="ImportSimpleProductsCron" method="execute">
            <schedule>10 3 * * *</schedule>
        </job>
    </group>
</config>

and the di.xml contains this:

<virtualType name="ImportSimpleProductsCron" type="[Vendor]\Import\Cron\Import">
    <arguments>
        <argument name="processFactory" xsi:type="object">SimpleProductsProcessFactory</argument><!-- also virtual type -->
    </arguments>
</virtualType>

<virtualType name="SimpleProductsProcessFactory" type="[Vendor]\Import\Model\Import\ProcessInterfaceFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">SimpleProductsProcess</argument><!-- also virtual type -->
    </arguments>
</virtualType>

<virtualType name="SimpleProductsProcess" type="[Vendor]\Import\Model\Import\Process">
    <arguments>
        <argument name="type" xsi:type="string">simple</argument>
    </arguments>
</virtualType>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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