Skip to content

Commit

Permalink
Merge remote-tracking branch '34186/cron-process-title' into comm_787…
Browse files Browse the repository at this point in the history
…64_31355
  • Loading branch information
Indraniks committed Feb 17, 2023
2 parents 8dfe175 + 87ef2f9 commit 907114e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
38 changes: 37 additions & 1 deletion app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Magento\Cron\Observer;

use Laminas\Http\PhpEnvironment\Request as Environment;
use Exception;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
Expand Down Expand Up @@ -133,6 +134,16 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
protected $dateTime;

/**
* @var Environment
*/
private Environment $environment;

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

/**
* @var \Symfony\Component\Process\PhpExecutableFinder
*/
Expand Down Expand Up @@ -189,6 +200,7 @@ class ProcessCronQueueObserver implements ObserverInterface
* @param \Magento\Framework\Lock\LockManagerInterface $lockManager
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param DeadlockRetrierInterface $retrier
* @param Environment $environment
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -206,7 +218,8 @@ public function __construct(
StatFactory $statFactory,
\Magento\Framework\Lock\LockManagerInterface $lockManager,
\Magento\Framework\Event\ManagerInterface $eventManager,
DeadlockRetrierInterface $retrier
DeadlockRetrierInterface $retrier,
Environment $environment
) {
$this->_objectManager = $objectManager;
$this->_scheduleFactory = $scheduleFactory;
Expand All @@ -216,6 +229,7 @@ public function __construct(
$this->_request = $request;
$this->_shell = $shell;
$this->dateTime = $dateTime;
$this->environment = $environment;
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
$this->logger = $logger;
$this->state = $state;
Expand Down Expand Up @@ -354,6 +368,8 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
);
}

$this->setProcessTitle($jobCode, $groupId);

$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
$this->retrier->execute(
function () use ($schedule) {
Expand Down Expand Up @@ -944,4 +960,24 @@ function () use ($scheduleResource, $where) {
$scheduleResource->getConnection()
);
}

/**
* Set the process title to include the job code and group
*
* @param string $jobCode
* @param string $groupId
*/
private function setProcessTitle(string $jobCode, string $groupId): void
{
if (!isset($this->originalProcessTitle)) {
$this->originalProcessTitle = PHP_BINARY . ' ' . implode(' ', $this->environment->getServer('argv'));
}

if (strpos($this->originalProcessTitle, " --group=$groupId ") !== false) {
// Group is already shown, so no need to include here in duplicate
cli_set_process_title($this->originalProcessTitle . " # job: $jobCode");
} else {
cli_set_process_title($this->originalProcessTitle . " # group: $groupId, job: $jobCode");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Cron\Test\Unit\Observer;

use Exception;
use Laminas\Http\PhpEnvironment\Request as Environment;
use Magento\Cron\Model\Config;
use Magento\Cron\Model\DeadlockRetrierInterface;
use Magento\Cron\Model\ResourceModel\Schedule as ScheduleResourceModel;
Expand All @@ -20,8 +21,8 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\Request as ConsoleRequest;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\App\State as AppState;
use Magento\Framework\App\State;
use Magento\Framework\DataObject;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
Expand Down Expand Up @@ -219,6 +220,14 @@ protected function setUp(): void

$this->retrierMock = $this->getMockForAbstractClass(DeadlockRetrierInterface::class);

$environmentMock = $this->getMockBuilder(Environment::class)
->disableOriginalConstructor()
->getMock();
$environmentMock->expects($this->any())
->method('getServer')
->with('argv')
->willReturn([]);

$this->cronQueueObserver = new ProcessCronQueueObserver(
$this->objectManagerMock,
$this->scheduleFactoryMock,
Expand All @@ -234,7 +243,8 @@ protected function setUp(): void
$this->statFactory,
$this->lockManagerMock,
$this->eventManager,
$this->retrierMock
$this->retrierMock,
$environmentMock
);
}

Expand Down

0 comments on commit 907114e

Please sign in to comment.