Skip to content

fix 37878: fix sales_clean_orders #37999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@
*/
namespace Magento\Cron\Observer;

use Laminas\Http\PhpEnvironment\Request as Environment;
use Exception;
use Laminas\Http\PhpEnvironment\Request as Environment;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
use Magento\Cron\Model\Schedule;
@@ -184,6 +184,11 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
private $retrier;

/**
* @var array
*/
private $ignoreMismatches = [];

/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Cron\Model\ScheduleFactory $scheduleFactory
@@ -201,6 +206,7 @@ class ProcessCronQueueObserver implements ObserverInterface
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param DeadlockRetrierInterface $retrier
* @param Environment $environment
* @param array $ignoreMismatches
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
@@ -219,7 +225,8 @@ public function __construct(
\Magento\Framework\Lock\LockManagerInterface $lockManager,
\Magento\Framework\Event\ManagerInterface $eventManager,
DeadlockRetrierInterface $retrier,
Environment $environment
Environment $environment,
array $ignoreMismatches = []
) {
$this->_objectManager = $objectManager;
$this->_scheduleFactory = $scheduleFactory;
@@ -237,6 +244,7 @@ public function __construct(
$this->lockManager = $lockManager;
$this->eventManager = $eventManager;
$this->retrier = $retrier;
$this->ignoreMismatches = $ignoreMismatches;
}

/**
@@ -789,6 +797,9 @@ private function getCronExpression($jobConfig)
private function cleanupScheduleMismatches()
{
foreach ($this->invalid as $jobCode => $scheduledAtList) {
if (in_array($jobCode, $this->ignoreMismatches)) {
continue;
}
$this->cleanup(
[
'status = ?' => Schedule::STATUS_PENDING,
102 changes: 102 additions & 0 deletions app/code/Magento/Sales/Observer/SetCronPendingPaymentOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Observer;

use Magento\Cron\Model\Schedule;
use Magento\Cron\Model\ScheduleFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Sales\Model\Order;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;

class SetCronPendingPaymentOrder implements ObserverInterface
{
public const XML_PATH_ORDER_DELETE_PENDING = 'sales/orders/delete_pending_after';

/**
* @var ScheduleFactory
*/
protected $scheduleFactory;

/**
* @var DateTime
*/
protected $dateTime;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var bool
*/
protected $isStart = false;

/**
* @param ScheduleFactory $scheduleFactory
* @param DateTime $dateTime
* @param LoggerInterface $logger
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScheduleFactory $scheduleFactory,
DateTime $dateTime,
LoggerInterface $logger,
ScopeConfigInterface $scopeConfig
) {
$this->scheduleFactory = $scheduleFactory;
$this->dateTime = $dateTime;
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
}

/**
* Set cron for order pending payment
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this|void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->isStart) {
return $this;
}

$order = $observer->getEvent()->getOrder();
if (!$order->getId() || $order->getStatus() !== Order::STATE_PENDING_PAYMENT) {
return $this;
}

$lifetime = $this->scopeConfig->getValue(
self::XML_PATH_ORDER_DELETE_PENDING,
ScopeInterface::SCOPE_WEBSITES,
$order->getStore()->getWebsite()->getId()
);
$currentTime = $this->dateTime->gmtTimestamp();
try {
$schedule = $this->scheduleFactory->create()
->setJobCode('sales_clean_orders')
->setStatus(Schedule::STATUS_PENDING)
->setCreatedAt(date('Y-m-d H:i:s', $currentTime))
->setScheduledAt(date('Y-m-d H:i', $currentTime + $lifetime * 60));

$schedule->save();
$this->isStart = true;
} catch (\Exception $e) {
$this->logger->critical($e);
}
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/Sales/etc/di.xml
Original file line number Diff line number Diff line change
@@ -1038,4 +1038,11 @@
</argument>
</arguments>
</type>
<type name="Magento\Cron\Observer\ProcessCronQueueObserver">
<arguments>
<argument name="ignoreMismatches" xsi:type="array">
<item name="sales_clean_orders" xsi:type="string">sales_clean_orders</item>
</argument>
</arguments>
</type>
</config>
3 changes: 3 additions & 0 deletions app/code/Magento/Sales/etc/events.xml
Original file line number Diff line number Diff line change
@@ -56,4 +56,7 @@
name="sales_assign_order_to_customer"
instance="Magento\Sales\Observer\AssignOrderToCustomerObserver" />
</event>
<event name="sales_order_save_after">
<observer name="sales_order_pending_payment_cron" instance="Magento\Sales\Observer\SetCronPendingPaymentOrder" />
</event>
</config>
1 change: 1 addition & 0 deletions app/code/Magento/Sales/etc/module.xml
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
<module name="Magento_Customer"/>
<module name="Magento_Payment"/>
<module name="Magento_SalesSequence"/>
<module name="Magento_Cron"/>
</sequence>
</module>
</config>