Ethan3600 / magento2-CronjobManager

Cron Job Manager for Magento 2
https://packagist.org/packages/ethanyehuda/magento2-cronjobmanager
Open Software License 3.0
340 stars 73 forks source link

"Required parameter 'theme_dir' was not passed" when I dispatch now a cronjob in backend #191

Closed CanBeer closed 1 year ago

CanBeer commented 1 year ago

Describe the bug

We have a sync_import_order cron which allows us to create the shipment and capture the order. Therefore, there is an invoice and shipment email send.

The cron starts as follows:

  public function execute(): void
     {
         $this->_appState->emulateAreaCode(
             \Magento\Framework\App\Area::AREA_FRONTEND,
             [$this, 'import']
         );
     }

I have no problem, if I run the cron in cli like this: php8.1 bin/magento cronmanager:runjob sync_import_order

On the other hand, if I launch it from the back office with the button "Dispatch now", I got this error:


2023-03-01T20:38:52+00:00 ERR (3): /app/xhz6bqjk4g6zk_stg/var/import/sap/gb/EDESADV_importev.xml Import error: InvalidArgumentException: Required parameter 'theme_dir' was not passed in /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/Fallback/Rule/Simple.php:59
Stack trace:
#0 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/Fallback/Rule/Theme.php(78): Magento\Framework\View\Design\Fallback\Rule\Simple->getPatternDirs(Array)
#1 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/Fallback/Rule/Composite.php(48): Magento\Framework\View\Design\Fallback\Rule\Theme->getPatternDirs(Array)
#2 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/Fallback/Rule/ModularSwitch.php(50): Magento\Framework\View\Design\Fallback\Rule\Composite->getPatternDirs(Array)
#3 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/FileResolution/Fallback/Resolver/Simple.php(118): Magento\Framework\View\Design\Fallback\Rule\ModularSwitch->getPatternDirs(Array)
#4 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/FileResolution/Fallback/Resolver/Simple.php(61): Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple->resolveFile(Object(Magento\Framework\View\Design\Fallback\Rule\ModularSwitch), 'info/pdf/defaul...', Array)
#5 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/FileResolution/Fallback/File.php(42): Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple->resolve('template', 'info/pdf/defaul...', 'frontend', Object(Magento\Theme\Model\Theme), NULL, 'Magento_Payment')
#6 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Design/FileResolution/Fallback/TemplateFile.php(82): Magento\Framework\View\Design\FileResolution\Fallback\File->getFile('frontend', Object(Magento\Theme\Model\Theme), 'info/pdf/defaul...', 'Magento_Payment')
#7 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/FileSystem.php(126): Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile->getFile('frontend', Object(Magento\Theme\Model\Theme), 'info/pdf/defaul...', 'Magento_Payment')
#8 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/Template/File/Resolver.php(60): Magento\Framework\View\FileSystem->getTemplateFileName('Magento_Payment...', Array)
#9 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/Template.php(214): Magento\Framework\View\Element\Template\File\Resolver->getTemplateFileName('Magento_Payment...', Array)
#10 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/Template.php(293): Magento\Framework\View\Element\Template->getTemplateFile()
#11 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/AbstractBlock.php(1095): Magento\Framework\View\Element\Template->_toHtml()
#12 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/AbstractBlock.php(1099): Magento\Framework\View\Element\AbstractBlock->Magento\Framework\View\Element\{closure}()
#13 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/View/Element/AbstractBlock.php(660): Magento\Framework\View\Element\AbstractBlock->_loadCache()
#14 /app/xhz6bqjk4g6zk_stg/vendor/magento/module-payment/Block/Info.php(62): Magento\Framework\View\Element\AbstractBlock->toHtml()
#15 /app/xhz6bqjk4g6zk_stg/app/code/Reflet/Sales/Model/Order/Pdf/Invoice.php(784): Magento\Payment\Block\Info->toPdf()
#16 /app/xhz6bqjk4g6zk_stg/app/code/Reflet/Sales/Model/Order/Pdf/Invoice.php(182): Reflet\Sales\Model\Order\Pdf\Invoice->insertOrder(Object(Zend_Pdf_Page), Object(Reflet\GlobalPaymentsHpp\Model\Order\Interceptor), true)
#17 /app/xhz6bqjk4g6zk_stg/app/code/Reflet/Sales/Observer/AddInvoiceEmailAttachment.php(86): Reflet\Sales\Model\Order\Pdf\Invoice->getPdf(Array)
#18 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Event/Invoker/InvokerDefault.php(88): Reflet\Sales\Observer\AddInvoiceEmailAttachment->execute(Object(Magento\Framework\Event\Observer))
#19 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Event/Invoker/InvokerDefault.php(74): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Reflet\Sales\Observer\AddInvoiceEmailAttachment), Object(Magento\Framework\Event\Observer))
#20 /app/xhz6bqjk4g6zk_stg/vendor/magento/module-staging/Model/Event/Manager.php(97): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))
#21 /app/xhz6bqjk4g6zk_stg/generated/code/Magento/Staging/Model/Event/Manager/Proxy.php(95): Magento\Staging\Model\Event\Manager->dispatch('email_invoice_s...', Array)
#22 /app/xhz6bqjk4g6zk_stg/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php(159): Magento\Staging\Model\Event\Manager\Proxy->dispatch('email_invoice_s...', Array)
#23 /app/xhz6bqjk4g6zk_stg/app/code/Reflet/Sync/Model/Import/Xml/Sales/Order.php(562): Magento\Sales\Model\Order\Email\Sender\InvoiceSender->send(Object(Magento\Sales\Model\Order\Invoice\Interceptor))
#24 /app/xhz6bqjk4g6zk_stg/app/code/Reflet/Sync/Model/Import/Xml/Sales/Order.php(234): Reflet\Sync\Model\Import\Xml\Sales\Order->processRowWorld(Object(SimpleXMLElement))
#25 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(58): Reflet\Sync\Model\Import\Xml\Sales\Order->processRow(Object(SimpleXMLElement))
#26 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(138): Reflet\Sync\Model\Import\Xml\Sales\Order\Interceptor->___callParent('processRow', Array)
#27 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(153): Reflet\Sync\Model\Import\Xml\Sales\Order\Interceptor->Magento\Framework\Interception\{closure}(Object(SimpleXMLElement))
#28 /app/xhz6bqjk4g6zk_stg/generated/code/Reflet/Sync/Model/Import/Xml/Sales/Order/Interceptor.php(23): Reflet\Sync\Model\Import\Xml\Sales\Order\Interceptor->___callPlugins('processRow', Array, Array)
#29 /app/xhz6bqjk4g6zk_stg/vendor/reflet/magento-module-main-sync/Model/Import/Sync.php(229): Reflet\Sync\Model\Import\Xml\Sales\Order\Interceptor->processRow(Object(SimpleXMLElement))
#30 /app/xhz6bqjk4g6zk_stg/vendor/reflet/magento-module-main-sync/Cron/ImportOrder.php(94): Reflet\MainSync\Model\Import\Sync->import(16)
#31 [internal function]: Reflet\MainSync\Cron\ImportOrder->import()
#32 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/State.php(187): call_user_func_array(Array, Array)
#33 /app/xhz6bqjk4g6zk_stg/vendor/reflet/magento-module-main-sync/Cron/ImportOrder.php(81): Magento\Framework\App\State->emulateAreaCode('frontend', Array)
#34 [internal function]: Reflet\MainSync\Cron\ImportOrder->execute(Object(Magento\Cron\Model\Schedule\Interceptor))
#35 /app/xhz6bqjk4g6zk_stg/vendor/ethanyehuda/magento2-cronjobmanager/Helper/Processor.php(121): call_user_func_array(Array, Array)
#36 /app/xhz6bqjk4g6zk_stg/vendor/ethanyehuda/magento2-cronjobmanager/Model/ScheduleManagement.php(64): EthanYehuda\CronjobManager\Helper\Processor->runScheduledJob(Array, Object(Magento\Cron\Model\Schedule\Interceptor))
#37 /app/xhz6bqjk4g6zk_stg/vendor/ethanyehuda/magento2-cronjobmanager/Model/Manager.php(94): EthanYehuda\CronjobManager\Model\ScheduleManagement->execute(97584755)
#38 /app/xhz6bqjk4g6zk_stg/vendor/ethanyehuda/magento2-cronjobmanager/Controller/Adminhtml/Manage/Job/MassDispatch.php(67): EthanYehuda\CronjobManager\Model\Manager->dispatchSchedule('97584755', Object(Magento\Cron\Model\Schedule\Interceptor))
#39 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(58): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch->execute()
#40 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(138): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->___callParent('execute', Array)
#41 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(153): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->Magento\Framework\Interception\{closure}()
#42 /app/xhz6bqjk4g6zk_stg/generated/code/EthanYehuda/CronjobManager/Controller/Adminhtml/Manage/Job/MassDispatch/Interceptor.php(23): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->___callPlugins('execute', Array, Array)
#43 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/Action/Action.php(111): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->execute()
#44 /app/xhz6bqjk4g6zk_stg/vendor/magento/module-backend/App/AbstractAction.php(151): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#45 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#46 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(138): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->___callParent('dispatch', Array)
#47 /app/xhz6bqjk4g6zk_stg/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(145): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#48 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#49 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(153): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#50 /app/xhz6bqjk4g6zk_stg/generated/code/EthanYehuda/CronjobManager/Controller/Adminhtml/Manage/Job/MassDispatch/Interceptor.php(32): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->___callPlugins('dispatch', Array, Array)
#51 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/FrontController.php(245): EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#52 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/FrontController.php(212): Magento\Framework\App\FrontController->getActionResponse(Object(EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor), Object(Magento\Framework\App\Request\Http))
#53 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/FrontController.php(147): Magento\Framework\App\FrontController->processRequest(Object(Magento\Framework\App\Request\Http), Object(EthanYehuda\CronjobManager\Controller\Adminhtml\Manage\Job\MassDispatch\Interceptor))
#54 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#55 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#56 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#57 /app/xhz6bqjk4g6zk_stg/generated/code/Magento/Framework/App/FrontController/Interceptor.php(23): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#58 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/Http.php(116): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#59 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Http->launch()
#60 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\Http\Interceptor->___callParent('launch', Array)
#61 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\Http\Interceptor->Magento\Framework\Interception\{closure}()
#62 /app/xhz6bqjk4g6zk_stg/generated/code/Magento/Framework/App/Http/Interceptor.php(23): Magento\Framework\App\Http\Interceptor->___callPlugins('launch', Array, Array)
#63 /app/xhz6bqjk4g6zk_stg/vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http\Interceptor->launch()
#64 /app/xhz6bqjk4g6zk_stg/pub/index.php(30): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#65 {main}

During the generation of the PDF invoice attachment, Magento try to load the Magento_Payment::info/pdf/default.phtml frontend template, but initially the area is ADMINHTML, and he can't load it in the backend theme.

       /** @var \Magento\Payment\Block\Info $paymentInfoBlock */
            $paymentInfoBlock = $this->_paymentData->getInfoBlock($order->getPayment());
            $paymentInfoBlock->setData('is_secure_mode', true);
            $paymentInfo = $paymentInfoBlock->toPdf();

image

Have you ever noticed this problem? I don't know if it's a Magento problem or related to your module.

Expected behavior

We need to have the same behaviour as the cli.

Environment (please complete the following information):

fredden commented 1 year ago

I don't recommend using the 'dispatch now' nor 'run cron' features. These both run the scheduled tasks in the wrong context, which is the cause of the bug you're describing here.

If you want to trigger a job to run "now", I recommend using the 'schedule job' feature instead.

CanBeer commented 1 year ago

@fredden Thanks for the workaround.

fredden commented 1 year ago

Version 2.0.0 has now been released, which includes #193 which removes the problem feature.