Skip to content

Commit 907114e

Browse files
committed
Merge remote-tracking branch '34186/cron-process-title' into comm_78764_31355
2 parents 8dfe175 + 87ef2f9 commit 907114e

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Magento\Cron\Observer;
1111

12+
use Laminas\Http\PhpEnvironment\Request as Environment;
1213
use Exception;
1314
use Magento\Cron\Model\DeadlockRetrierInterface;
1415
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
@@ -133,6 +134,16 @@ class ProcessCronQueueObserver implements ObserverInterface
133134
*/
134135
protected $dateTime;
135136

137+
/**
138+
* @var Environment
139+
*/
140+
private Environment $environment;
141+
142+
/**
143+
* @var string
144+
*/
145+
private string $originalProcessTitle;
146+
136147
/**
137148
* @var \Symfony\Component\Process\PhpExecutableFinder
138149
*/
@@ -189,6 +200,7 @@ class ProcessCronQueueObserver implements ObserverInterface
189200
* @param \Magento\Framework\Lock\LockManagerInterface $lockManager
190201
* @param \Magento\Framework\Event\ManagerInterface $eventManager
191202
* @param DeadlockRetrierInterface $retrier
203+
* @param Environment $environment
192204
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
193205
*/
194206
public function __construct(
@@ -206,7 +218,8 @@ public function __construct(
206218
StatFactory $statFactory,
207219
\Magento\Framework\Lock\LockManagerInterface $lockManager,
208220
\Magento\Framework\Event\ManagerInterface $eventManager,
209-
DeadlockRetrierInterface $retrier
221+
DeadlockRetrierInterface $retrier,
222+
Environment $environment
210223
) {
211224
$this->_objectManager = $objectManager;
212225
$this->_scheduleFactory = $scheduleFactory;
@@ -216,6 +229,7 @@ public function __construct(
216229
$this->_request = $request;
217230
$this->_shell = $shell;
218231
$this->dateTime = $dateTime;
232+
$this->environment = $environment;
219233
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
220234
$this->logger = $logger;
221235
$this->state = $state;
@@ -354,6 +368,8 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
354368
);
355369
}
356370

371+
$this->setProcessTitle($jobCode, $groupId);
372+
357373
$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
358374
$this->retrier->execute(
359375
function () use ($schedule) {
@@ -944,4 +960,24 @@ function () use ($scheduleResource, $where) {
944960
$scheduleResource->getConnection()
945961
);
946962
}
963+
964+
/**
965+
* Set the process title to include the job code and group
966+
*
967+
* @param string $jobCode
968+
* @param string $groupId
969+
*/
970+
private function setProcessTitle(string $jobCode, string $groupId): void
971+
{
972+
if (!isset($this->originalProcessTitle)) {
973+
$this->originalProcessTitle = PHP_BINARY . ' ' . implode(' ', $this->environment->getServer('argv'));
974+
}
975+
976+
if (strpos($this->originalProcessTitle, " --group=$groupId ") !== false) {
977+
// Group is already shown, so no need to include here in duplicate
978+
cli_set_process_title($this->originalProcessTitle . " # job: $jobCode");
979+
} else {
980+
cli_set_process_title($this->originalProcessTitle . " # group: $groupId, job: $jobCode");
981+
}
982+
}
947983
}

app/code/Magento/Cron/Test/Unit/Observer/ProcessCronQueueObserverTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Cron\Test\Unit\Observer;
99

1010
use Exception;
11+
use Laminas\Http\PhpEnvironment\Request as Environment;
1112
use Magento\Cron\Model\Config;
1213
use Magento\Cron\Model\DeadlockRetrierInterface;
1314
use Magento\Cron\Model\ResourceModel\Schedule as ScheduleResourceModel;
@@ -20,8 +21,8 @@
2021
use Magento\Framework\App\Config\ScopeConfigInterface;
2122
use Magento\Framework\App\Console\Request as ConsoleRequest;
2223
use Magento\Framework\App\ObjectManager;
23-
use Magento\Framework\App\State;
2424
use Magento\Framework\App\State as AppState;
25+
use Magento\Framework\App\State;
2526
use Magento\Framework\DataObject;
2627
use Magento\Framework\DB\Adapter\AdapterInterface;
2728
use Magento\Framework\Event\ManagerInterface;
@@ -219,6 +220,14 @@ protected function setUp(): void
219220

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

223+
$environmentMock = $this->getMockBuilder(Environment::class)
224+
->disableOriginalConstructor()
225+
->getMock();
226+
$environmentMock->expects($this->any())
227+
->method('getServer')
228+
->with('argv')
229+
->willReturn([]);
230+
222231
$this->cronQueueObserver = new ProcessCronQueueObserver(
223232
$this->objectManagerMock,
224233
$this->scheduleFactoryMock,
@@ -234,7 +243,8 @@ protected function setUp(): void
234243
$this->statFactory,
235244
$this->lockManagerMock,
236245
$this->eventManager,
237-
$this->retrierMock
246+
$this->retrierMock,
247+
$environmentMock
238248
);
239249
}
240250

0 commit comments

Comments
 (0)