Skip to content

Commit

Permalink
Merge pull request #7213 from magento-trigger/ph-delivery
Browse files Browse the repository at this point in the history
[Platform Health] Updates for PHP8.1
  • Loading branch information
xmav committed Nov 12, 2021
2 parents 13d8ccd + dd83201 commit fea0bba
Show file tree
Hide file tree
Showing 8 changed files with 937 additions and 197 deletions.
43 changes: 28 additions & 15 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
*/
namespace Magento\Cron\Observer;

use Exception;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
use Magento\Cron\Model\Schedule;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\CronException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Profiler\Driver\Standard\Stat;
use Magento\Framework\Profiler\Driver\Standard\StatFactory;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Throwable;

/**
* The observer for processing cron jobs.
Expand Down Expand Up @@ -226,13 +230,15 @@ public function __construct(
* Generate tasks schedule
* Cleanup tasks schedule
*
* @param \Magento\Framework\Event\Observer $observer
* @param Observer $observer
*
* @return void
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
public function execute(Observer $observer)
{
$currentTime = $this->dateTime->gmtTimestamp();
$jobGroupsRoot = $this->_config->getJobs();
Expand Down Expand Up @@ -311,8 +317,9 @@ private function lockGroup(string $groupId, callable $callback): void
* @param string[] $jobConfig
* @param Schedule $schedule
* @param string $groupId
*
* @return void
* @throws \Exception
* @throws Exception|Throwable
*/
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
{
Expand All @@ -322,25 +329,29 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
if ($scheduledTime < $currentTime - $scheduleLifetime) {
$schedule->setStatus(Schedule::STATUS_MISSED);
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
throw new Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
}

if (!isset($jobConfig['instance'], $jobConfig['method'])) {
$schedule->setStatus(Schedule::STATUS_ERROR);
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception(sprintf('No callbacks found for cron job %s', $jobCode));
throw new Exception(sprintf('No callbacks found for cron job %s', $jobCode));
}
$model = $this->_objectManager->create($jobConfig['instance']);
$callback = [$model, $jobConfig['method']];
if (!is_callable($callback)) {
$schedule->setStatus(Schedule::STATUS_ERROR);
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception(
sprintf('Invalid callback: %s::%s can\'t be called', $jobConfig['instance'], $jobConfig['method'])
throw new Exception(
sprintf(
'Invalid callback: %s::%s can\'t be called',
$jobConfig['instance'],
$jobConfig['method']
)
);
}

$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()));
$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
$this->retrier->execute(
function () use ($schedule) {
$schedule->save();
Expand All @@ -355,7 +366,7 @@ function () use ($schedule) {
$this->logger->info(sprintf('Cron Job %s is run', $jobCode));
//phpcs:ignore Magento2.Functions.DiscouragedFunction
call_user_func_array($callback, [$schedule]);
} catch (\Throwable $e) {
} catch (Throwable $e) {
$schedule->setStatus(Schedule::STATUS_ERROR);
$this->logger->error(
sprintf(
Expand All @@ -380,8 +391,8 @@ function () use ($schedule) {
$schedule->setStatus(
Schedule::STATUS_SUCCESS
)->setFinishedAt(
strftime(
'%Y-%m-%d %H:%M:%S',
date(
'Y-m-d H:i:s',
$this->dateTime->gmtTimestamp()
)
);
Expand Down Expand Up @@ -607,14 +618,16 @@ protected function getConfigSchedule($jobConfig)
* @param string $cronExpression
* @param int $timeInterval
* @param array $exists
*
* @return void
* @throws Exception
*/
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
{
$currentTime = $this->dateTime->gmtTimestamp();
$timeAhead = $currentTime + $timeInterval;
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
$scheduledAt = strftime('%Y-%m-%d %H:%M:00', $time);
$scheduledAt = date('Y-m-d H:i:00', $time);
$alreadyScheduled = !empty($exists[$jobCode . '/' . $scheduledAt]);
$schedule = $this->createSchedule($jobCode, $cronExpression, $time);
$valid = $schedule->trySchedule();
Expand Down Expand Up @@ -648,8 +661,8 @@ protected function createSchedule($jobCode, $cronExpression, $time)
->setCronExpr($cronExpression)
->setJobCode($jobCode)
->setStatus(Schedule::STATUS_PENDING)
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
->setScheduledAt(date('Y-m-d H:i', $time));

return $schedule;
}
Expand Down
120 changes: 52 additions & 68 deletions app/code/Magento/ImportExport/Model/Import/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
*/
namespace Magento\ImportExport\Model\Import;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Stdlib\StringUtils;
use Magento\ImportExport\Model\Import;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\ImportExport\Model\ImportFactory;
use Magento\ImportExport\Model\ResourceModel\Helper;
use Magento\Store\Model\ScopeInterface;

/**
* Import entity abstract model
*
* phpcs:disable Magento2.Classes.AbstractApi
* @api
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
Expand All @@ -34,20 +39,24 @@ abstract class AbstractEntity
*/
const COLUMN_ACTION_VALUE_DELETE = 'delete';

/**#@+
* XML paths to parameters
/**
* Path to bunch size configuration
*/
const XML_PATH_BUNCH_SIZE = 'import/format_v2/bunch_size';

/**
* Path to page size configuration
*/
const XML_PATH_PAGE_SIZE = 'import/format_v2/page_size';

/**#@-*/

/**#@+
* Database constants
/**
* Size of varchar value
*/
const DB_MAX_VARCHAR_LENGTH = 256;

/**
* Size of text value
*/
const DB_MAX_TEXT_LENGTH = 65536;

const ERROR_CODE_SYSTEM_EXCEPTION = 'systemException';
Expand Down Expand Up @@ -84,9 +93,9 @@ abstract class AbstractEntity
. ", see acceptable values on settings specified for Admin",
];

/**#@-*/

/**#@-*/
/**
* @var AdapterInterface
*/
protected $_connection;

/**
Expand All @@ -97,9 +106,7 @@ abstract class AbstractEntity
protected $_dataValidated = false;

/**
* Valid column names
*
* @array
* @var array
*/
protected $validColumnNames = [];

Expand Down Expand Up @@ -132,7 +139,7 @@ abstract class AbstractEntity
/**
* Magento string lib
*
* @var \Magento\Framework\Stdlib\StringUtils
* @var StringUtils
*/
protected $string;

Expand Down Expand Up @@ -252,7 +259,7 @@ abstract class AbstractEntity
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $_scopeConfig;

Expand Down Expand Up @@ -285,54 +292,45 @@ abstract class AbstractEntity
private $serializer;

/**
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\ImportExport\Model\ImportFactory $importFactory
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
* @param \Magento\Framework\App\ResourceConnection $resource
* @param StringUtils $string
* @param ScopeConfigInterface $scopeConfig
* @param ImportFactory $importFactory
* @param Helper $resourceHelper
* @param ResourceConnection $resource
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @param array $data
* @param Json|null $serializer
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function __construct(
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\ImportExport\Model\ImportFactory $importFactory,
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
StringUtils $string,
ScopeConfigInterface $scopeConfig,
ImportFactory $importFactory,
Helper $resourceHelper,
ResourceConnection $resource,
ProcessingErrorAggregatorInterface $errorAggregator,
array $data = []
array $data = [],
Json $serializer = null
) {
$this->_scopeConfig = $scopeConfig;
$this->_dataSourceModel = isset(
$data['data_source_model']
) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel();
$this->_connection =
isset($data['connection']) ?
$data['connection'] :
$resource->getConnection();
$this->string = $string;
$this->_pageSize = isset(
$data['page_size']
) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int)$this->_scopeConfig->getValue(
$this->_scopeConfig = $scopeConfig;
$this->_dataSourceModel = $data['data_source_model'] ?? $importFactory->create()->getDataSourceModel();
$this->_maxDataSize = $data['max_data_size'] ?? $resourceHelper->getMaxDataSize();
$this->_connection = $data['connection'] ?? $resource->getConnection();
$this->errorAggregator = $errorAggregator;
$this->_pageSize = $data['page_size'] ?? ((int) $this->_scopeConfig->getValue(
static::XML_PATH_PAGE_SIZE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) : 0);
$this->_maxDataSize = isset(
$data['max_data_size']
) ? $data['max_data_size'] : $resourceHelper->getMaxDataSize();
$this->_bunchSize = isset(
$data['bunch_size']
) ? $data['bunch_size'] : (static::XML_PATH_BUNCH_SIZE ? (int)$this->_scopeConfig->getValue(
ScopeInterface::SCOPE_STORE
) ?: 0);
$this->_bunchSize = $data['bunch_size'] ?? ((int) $this->_scopeConfig->getValue(
static::XML_PATH_BUNCH_SIZE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) : 0);

$this->errorAggregator = $errorAggregator;
ScopeInterface::SCOPE_STORE
) ?: 0);

foreach ($this->errorMessageTemplates as $errorCode => $message) {
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
}
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}

/**
Expand Down Expand Up @@ -462,7 +460,7 @@ protected function _saveValidatedBunches()
foreach ($entityGroup as $key => $value) {
$bunchRows[$key] = $value;
}
$productDataSize = strlen($this->getSerializer()->serialize($bunchRows));
$productDataSize = strlen($this->serializer->serialize($bunchRows));

/* Check if the new bunch should be started */
$isBunchSizeExceeded = ($this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize);
Expand All @@ -473,7 +471,7 @@ protected function _saveValidatedBunches()
$entityGroup = [];
}

if (isset($entityGroup) && $this->validateRow($rowData, $source->key())) {
if (isset($entityGroup) && isset($rowData) && $this->validateRow($rowData, $source->key())) {
/* Add row to entity group */
$entityGroup[$source->key()] = $this->_prepareRowForDb($rowData);
} elseif (isset($entityGroup)) {
Expand All @@ -488,22 +486,6 @@ protected function _saveValidatedBunches()
return $this;
}

/**
* Get Serializer instance
*
* Workaround. Only way to implement dependency and not to break inherited child classes
*
* @return Json
* @deprecated 100.2.0
*/
private function getSerializer()
{
if (null === $this->serializer) {
$this->serializer = ObjectManager::getInstance()->get(Json::class);
}
return $this->serializer;
}

/**
* Add error with corresponding current data source row number.
*
Expand Down Expand Up @@ -553,7 +535,7 @@ public function addMessageTemplate($errorCode, $message)
/**
* Import behavior getter
*
* @param array $rowData
* @param array|null $rowData
* @return string
*/
public function getBehavior(array $rowData = null)
Expand All @@ -569,7 +551,9 @@ public function getBehavior(array $rowData = null)
if ($rowData !== null && $behavior == \Magento\ImportExport\Model\Import::BEHAVIOR_CUSTOM) {
// try analyze value in self::COLUMN_CUSTOM column and return behavior for given $rowData
if (array_key_exists(self::COLUMN_ACTION, $rowData)) {
if (strtolower($rowData[self::COLUMN_ACTION]) == self::COLUMN_ACTION_VALUE_DELETE) {
if ($rowData[self::COLUMN_ACTION]
&& strtolower($rowData[self::COLUMN_ACTION]) == self::COLUMN_ACTION_VALUE_DELETE
) {
$behavior = \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE;
} else {
// as per task description, if column value is different to self::COLUMN_CUSTOM_VALUE_DELETE,
Expand Down
Loading

0 comments on commit fea0bba

Please sign in to comment.