Skip to content
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

Set process title when running cron job #34186

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a2f9e4b
Set process title when running cron job
fredden Sep 28, 2021
0ac321b
Preserve (most of the) original command line title
fredden Sep 30, 2021
92d9706
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Sep 30, 2021
3613f80
Correct docblock
fredden Sep 30, 2021
55bf25e
Include PHP_BINARY in process title
fredden Oct 1, 2021
073b517
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Oct 1, 2021
34c1375
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Oct 4, 2021
5969ba7
Fix unit test
fredden Oct 6, 2021
2fa4ff5
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Oct 6, 2021
a5684a6
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Oct 25, 2021
07cabe4
Reduce property visibility
fredden Oct 25, 2021
d5a38f6
Merged 2.4-develop and resolved conflicts
engcom-Charlie Nov 23, 2021
9241acd
Merge remote-tracking branch 'magento2/2.4-develop' into cron-process…
engcom-Charlie Nov 23, 2021
c8856f6
Merge remote-tracking branch 'upstream/2.4-develop' into cron-process…
fredden Nov 27, 2021
4cca4c7
Recover from failed merge conflict resolution
fredden Nov 27, 2021
ae72fdd
Extract new feature to its own method
fredden Nov 27, 2021
c584355
Set required visibility on consts
fredden Nov 27, 2021
afda08b
Merge remote-tracking branch 'magento2/2.4-develop' into cron-process…
engcom-Charlie Dec 24, 2021
278509f
Merge branch 'cron-process-title' of github.com:fredden/magento2 into…
engcom-Charlie Dec 24, 2021
c888e66
Merge remote-tracking branch 'magento2/2.4-develop' into cron-process…
engcom-Charlie Jan 5, 2022
9a71f4e
Remove use of superglobal
fredden Jan 6, 2022
a1dec0c
Merge branch '2.4-develop' into cron-process-title
andrewbess Nov 9, 2022
e80a06f
Merge branch '2.4-develop' into cron-process-title
fredden Nov 11, 2022
87ef2f9
Merge branch '2.4-develop' into cron-process-title
engcom-Echo Feb 16, 2023
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
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 @@ -351,6 +365,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 @@ -929,4 +945,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