Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
Shinichi69 authored Mar 23, 2021
2 parents 38aea4b + 2f6673c commit c8d4714
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 26 deletions.
140 changes: 140 additions & 0 deletions Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,108 @@

namespace Mageplaza\CronSchedule\Observer;

use DateTime as DateTimeInterface;
use DateTimeZone;
use Exception;
use Magento\Cron\Model\ConfigInterface;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Magento\Cron\Model\Schedule;
use Magento\Cron\Model\ScheduleFactory;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\Request;
use Magento\Framework\App\State;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Process\PhpExecutableFinderFactory;
use Magento\Framework\Profiler\Driver\Standard\StatFactory;
use Magento\Framework\ShellInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;
use Mageplaza\CronSchedule\Helper\Data;
use Psr\Log\LoggerInterface;

/**
* Class ProcessCronQueueObserver
* @package Mageplaza\CronSchedule\Observer
*/
class ProcessCronQueueObserver extends \Magento\Cron\Observer\ProcessCronQueueObserver
{
/**
* @var TimezoneInterface
*/
private $localeDate;

/**
* @var string
*/
private $locale;

/**
* ProcessCronQueueObserver constructor.
*
* @param ObjectManagerInterface $objectManager
* @param ScheduleFactory $scheduleFactory
* @param CacheInterface $cache
* @param ConfigInterface $config
* @param ScopeConfigInterface $scopeConfig
* @param Request $request
* @param ShellInterface $shell
* @param DateTime $dateTime
* @param PhpExecutableFinderFactory $phpExecutableFinderFactory
* @param LoggerInterface $logger
* @param State $state
* @param StatFactory $statFactory
* @param LockManagerInterface $lockManager
* @param ManagerInterface $eventManager
* @param DeadlockRetrierInterface $retrier
* @param TimezoneInterface $localeDate
* @param ResolverInterface $localeResolver
*/
public function __construct(
ObjectManagerInterface $objectManager,
ScheduleFactory $scheduleFactory,
CacheInterface $cache,
ConfigInterface $config,
ScopeConfigInterface $scopeConfig,
Request $request,
ShellInterface $shell,
DateTime $dateTime,
PhpExecutableFinderFactory $phpExecutableFinderFactory,
LoggerInterface $logger,
State $state,
StatFactory $statFactory,
LockManagerInterface $lockManager,
ManagerInterface $eventManager,
DeadlockRetrierInterface $retrier,
TimezoneInterface $localeDate,
ResolverInterface $localeResolver
) {
$this->localeDate = $localeDate;
$this->locale = $localeResolver->getLocale();

parent::__construct(
$objectManager,
$scheduleFactory,
$cache,
$config,
$scopeConfig,
$request,
$shell,
$dateTime,
$phpExecutableFinderFactory,
$logger,
$state,
$statFactory,
$lockManager,
$eventManager,
$retrier
);
}

/**
* @param string $jobCode
* @param string $cronExpression
Expand All @@ -46,4 +140,50 @@ protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exist

parent::saveSchedule($jobCode, $cronExpression, $timeInterval, $exists);
}

/**
* @param int $scheduledTime
* @param int $currentTime
* @param string[] $jobConfig
* @param Schedule $schedule
* @param string $groupId
*
* @throws Exception
*/
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
{
$jobCode = $schedule->getJobCode();
$scheduleLifetime = $this->getCronGroupConfigurationValueCustom($groupId, self::XML_PATH_SCHEDULE_LIFETIME);
$scheduleLifetime = $scheduleLifetime * self::SECONDS_IN_MINUTE;
$scheduledAt = $schedule->getScheduledAt();

$dataScheduledAt = $this->localeDate->date(
new DateTimeInterface($scheduledAt, new DateTimeZone('UTC')),
$this->locale,
true
);

if ($scheduledTime < $currentTime - $scheduleLifetime) {
$schedule->setStatus(Schedule::STATUS_MISSED);
throw new Exception(
sprintf('Cron Job %s is missed at %s', $jobCode, $dataScheduledAt->format('Y-m-d H:i:s'))
);
}

parent::_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId);
}

/**
* @param $groupId
* @param $path
*
* @return mixed
*/
private function getCronGroupConfigurationValueCustom($groupId, $path)
{
return $this->_scopeConfig->getValue(
'system/cron/' . $groupId . '/' . $path,
ScopeInterface::SCOPE_STORE
);
}
}
52 changes: 26 additions & 26 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"name": "mageplaza/module-cron-schedule",
"description": "Magento 2 Cron Schedule Extension",
"require": {
"mageplaza/module-core": "^1.4.5"
},
"type": "magento2-module",
"version": "1.0.1",
"license": "proprietary",
"authors": [
{
"name": "Mageplaza",
"email": "[email protected]",
"homepage": "https://www.mageplaza.com",
"role": "Technical Support"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Mageplaza\\CronSchedule\\": ""
}
}
}
{
"name": "mageplaza/module-cron-schedule",
"description": "Magento 2 Cron Schedule Extension",
"require": {
"mageplaza/module-core": "^1.4.5"
},
"type": "magento2-module",
"version": "4.0.0",
"license": "proprietary",
"authors": [
{
"name": "Mageplaza",
"email": "[email protected]",
"homepage": "https://www.mageplaza.com",
"role": "Technical Support"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Mageplaza\\CronSchedule\\": ""
}
}
}

0 comments on commit c8d4714

Please sign in to comment.