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>

3
  • Please share the code which you have tried
    – Amit Bera
    May 23, 2019 at 10:33
  • added code examples May 23, 2019 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, 2019 at 13:36

1 Answer 1

3

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>
3
  • can be this used as well for event observer instance?
    – apedic
    Mar 9, 2021 at 16:01
  • 1
    @AntonioPedicini I did not try it, but I assume yes. For the same reason. Observer classes are instantiated via Object manager which knows about virtual types
    – Marius
    Mar 9, 2021 at 16:25
  • that worked thanks.
    – apedic
    Mar 10, 2021 at 12:01

Your Answer

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

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