Skip to content

Commit a3ca437

Browse files
author
Anton Evers
committed
Clean up the cron observer before adding edits
1 parent ec99590 commit a3ca437

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

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

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,42 @@ class ProcessCronQueueObserver implements ObserverInterface
5959
/**
6060
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection
6161
*/
62-
protected $_pendingSchedules;
62+
protected $pendingSchedules;
6363

6464
/**
6565
* @var \Magento\Cron\Model\ConfigInterface
6666
*/
67-
protected $_config;
67+
protected $config;
6868

6969
/**
7070
* @var \Magento\Framework\App\ObjectManager
7171
*/
72-
protected $_objectManager;
72+
protected $objectManager;
7373

7474
/**
7575
* @var \Magento\Framework\App\CacheInterface
7676
*/
77-
protected $_cache;
77+
protected $cache;
7878

7979
/**
8080
* @var \Magento\Framework\App\Config\ScopeConfigInterface
8181
*/
82-
protected $_scopeConfig;
82+
protected $scopeConfig;
8383

8484
/**
8585
* @var ScheduleFactory
8686
*/
87-
protected $_scheduleFactory;
87+
protected $scheduleFactory;
8888

8989
/**
9090
* @var \Magento\Framework\App\Console\Request
9191
*/
92-
protected $_request;
92+
protected $request;
9393

9494
/**
9595
* @var \Magento\Framework\ShellInterface
9696
*/
97-
protected $_shell;
97+
protected $shell;
9898

9999
/**
100100
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
@@ -143,13 +143,13 @@ public function __construct(
143143
\Psr\Log\LoggerInterface $logger,
144144
\Magento\Framework\App\State $state
145145
) {
146-
$this->_objectManager = $objectManager;
147-
$this->_scheduleFactory = $scheduleFactory;
148-
$this->_cache = $cache;
149-
$this->_config = $config;
150-
$this->_scopeConfig = $scopeConfig;
151-
$this->_request = $request;
152-
$this->_shell = $shell;
146+
$this->objectManager = $objectManager;
147+
$this->scheduleFactory = $scheduleFactory;
148+
$this->cache = $cache;
149+
$this->config = $config;
150+
$this->scopeConfig = $scopeConfig;
151+
$this->request = $request;
152+
$this->shell = $shell;
153153
$this->timezone = $timezone;
154154
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
155155
$this->logger = $logger;
@@ -169,25 +169,25 @@ public function __construct(
169169
*/
170170
public function execute(\Magento\Framework\Event\Observer $observer)
171171
{
172-
$pendingJobs = $this->_getPendingSchedules();
172+
$pendingJobs = $this->getPendingSchedules();
173173
$currentTime = $this->timezone->scopeTimeStamp();
174-
$jobGroupsRoot = $this->_config->getJobs();
174+
$jobGroupsRoot = $this->config->getJobs();
175175

176176
$phpPath = $this->phpExecutableFinder->find() ?: 'php';
177177

178178
foreach ($jobGroupsRoot as $groupId => $jobsRoot) {
179-
if ($this->_request->getParam('group') !== null
180-
&& $this->_request->getParam('group') !== '\'' . ($groupId) . '\''
181-
&& $this->_request->getParam('group') !== $groupId) {
179+
if ($this->request->getParam('group') !== null
180+
&& $this->request->getParam('group') !== '\'' . ($groupId) . '\''
181+
&& $this->request->getParam('group') !== $groupId) {
182182
continue;
183183
}
184-
if (($this->_request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1') && (
185-
$this->_scopeConfig->getValue(
184+
if (($this->request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1') && (
185+
$this->scopeConfig->getValue(
186186
'system/cron/' . $groupId . '/use_separate_process',
187187
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
188188
) == 1
189189
)) {
190-
$this->_shell->execute(
190+
$this->shell->execute(
191191
$phpPath . ' %s cron:run --group=' . $groupId . ' --' . Cli::INPUT_KEY_BOOTSTRAP . '='
192192
. self::STANDALONE_PROCESS_STARTED . '=1',
193193
[
@@ -210,7 +210,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
210210

211211
try {
212212
if ($schedule->tryLockJob()) {
213-
$this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId);
213+
$this->runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId);
214214
}
215215
} catch (\Exception $e) {
216216
$schedule->setMessages($e->getMessage());
@@ -233,8 +233,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
233233
$schedule->save();
234234
}
235235

236-
$this->_generate($groupId);
237-
$this->_cleanup($groupId);
236+
$this->generate($groupId);
237+
$this->cleanup($groupId);
238238
}
239239
}
240240

@@ -249,9 +249,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
249249
* @return void
250250
* @throws \Exception
251251
*/
252-
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
252+
protected function runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
253253
{
254-
$scheduleLifetime = (int)$this->_scopeConfig->getValue(
254+
$scheduleLifetime = (int)$this->scopeConfig->getValue(
255255
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_LIFETIME,
256256
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
257257
);
@@ -265,7 +265,7 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
265265
$schedule->setStatus(Schedule::STATUS_ERROR);
266266
throw new \Exception('No callbacks found');
267267
}
268-
$model = $this->_objectManager->create($jobConfig['instance']);
268+
$model = $this->objectManager->create($jobConfig['instance']);
269269
$callback = [$model, $jobConfig['method']];
270270
if (!is_callable($callback)) {
271271
$schedule->setStatus(Schedule::STATUS_ERROR);
@@ -294,15 +294,15 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
294294
*
295295
* @return \Magento\Cron\Model\ResourceModel\Schedule\Collection
296296
*/
297-
protected function _getPendingSchedules()
297+
protected function getPendingSchedules()
298298
{
299-
if (!$this->_pendingSchedules) {
300-
$this->_pendingSchedules = $this->_scheduleFactory->create()->getCollection()->addFieldToFilter(
299+
if (!$this->pendingSchedules) {
300+
$this->pendingSchedules = $this->scheduleFactory->create()->getCollection()->addFieldToFilter(
301301
'status',
302302
Schedule::STATUS_PENDING
303303
)->load();
304304
}
305-
return $this->_pendingSchedules;
305+
return $this->pendingSchedules;
306306
}
307307

308308
/**
@@ -311,13 +311,13 @@ protected function _getPendingSchedules()
311311
* @param string $groupId
312312
* @return $this
313313
*/
314-
protected function _generate($groupId)
314+
protected function generate($groupId)
315315
{
316316
/**
317317
* check if schedule generation is needed
318318
*/
319-
$lastRun = (int)$this->_cache->load(self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $groupId);
320-
$rawSchedulePeriod = (int)$this->_scopeConfig->getValue(
319+
$lastRun = (int)$this->cache->load(self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $groupId);
320+
$rawSchedulePeriod = (int)$this->scopeConfig->getValue(
321321
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_GENERATE_EVERY,
322322
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
323323
);
@@ -326,7 +326,7 @@ protected function _generate($groupId)
326326
return $this;
327327
}
328328

329-
$schedules = $this->_getPendingSchedules();
329+
$schedules = $this->getPendingSchedules();
330330
$exists = [];
331331
/** @var Schedule $schedule */
332332
foreach ($schedules as $schedule) {
@@ -336,13 +336,13 @@ protected function _generate($groupId)
336336
/**
337337
* generate global crontab jobs
338338
*/
339-
$jobs = $this->_config->getJobs();
340-
$this->_generateJobs($jobs[$groupId], $exists, $groupId);
339+
$jobs = $this->config->getJobs();
340+
$this->generateJobs($jobs[$groupId], $exists, $groupId);
341341

342342
/**
343343
* save time schedules generation was ran with no expiration
344344
*/
345-
$this->_cache->save(
345+
$this->cache->save(
346346
$this->timezone->scopeTimeStamp(),
347347
self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $groupId,
348348
['crontab'],
@@ -360,7 +360,7 @@ protected function _generate($groupId)
360360
* @param string $groupId
361361
* @return $this
362362
*/
363-
protected function _generateJobs($jobs, $exists, $groupId)
363+
protected function generateJobs($jobs, $exists, $groupId)
364364
{
365365
foreach ($jobs as $jobCode => $jobConfig) {
366366
$cronExpression = null;
@@ -390,11 +390,11 @@ protected function _generateJobs($jobs, $exists, $groupId)
390390
* @param string $groupId
391391
* @return $this
392392
*/
393-
protected function _cleanup($groupId)
393+
protected function cleanup($groupId)
394394
{
395395
// check if history cleanup is needed
396-
$lastCleanup = (int)$this->_cache->load(self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId);
397-
$historyCleanUp = (int)$this->_scopeConfig->getValue(
396+
$lastCleanup = (int)$this->cache->load(self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId);
397+
$historyCleanUp = (int)$this->scopeConfig->getValue(
398398
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_CLEANUP_EVERY,
399399
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
400400
);
@@ -403,7 +403,7 @@ protected function _cleanup($groupId)
403403
}
404404

405405
// check how long the record should stay unprocessed before marked as MISSED
406-
$scheduleLifetime = (int)$this->_scopeConfig->getValue(
406+
$scheduleLifetime = (int)$this->scopeConfig->getValue(
407407
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_LIFETIME,
408408
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
409409
);
@@ -412,16 +412,16 @@ protected function _cleanup($groupId)
412412
/**
413413
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection $history
414414
*/
415-
$history = $this->_scheduleFactory->create()->getCollection()->addFieldToFilter(
415+
$history = $this->scheduleFactory->create()->getCollection()->addFieldToFilter(
416416
'status',
417417
['in' => [Schedule::STATUS_SUCCESS, Schedule::STATUS_MISSED, Schedule::STATUS_ERROR]]
418418
)->load();
419419

420-
$historySuccess = (int)$this->_scopeConfig->getValue(
420+
$historySuccess = (int)$this->scopeConfig->getValue(
421421
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_SUCCESS,
422422
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
423423
);
424-
$historyFailure = (int)$this->_scopeConfig->getValue(
424+
$historyFailure = (int)$this->scopeConfig->getValue(
425425
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_FAILURE,
426426
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
427427
);
@@ -442,7 +442,7 @@ protected function _cleanup($groupId)
442442
}
443443

444444
// save time history cleanup was ran with no expiration
445-
$this->_cache->save(
445+
$this->cache->save(
446446
$this->timezone->scopeTimeStamp(),
447447
self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId,
448448
['crontab'],
@@ -458,7 +458,7 @@ protected function _cleanup($groupId)
458458
*/
459459
protected function getConfigSchedule($jobConfig)
460460
{
461-
$cronExpr = $this->_scopeConfig->getValue(
461+
$cronExpr = $this->scopeConfig->getValue(
462462
$jobConfig['config_path'],
463463
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
464464
);
@@ -499,7 +499,7 @@ protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exist
499499
*/
500500
protected function generateSchedule($jobCode, $cronExpression, $time)
501501
{
502-
$schedule = $this->_scheduleFactory->create()
502+
$schedule = $this->scheduleFactory->create()
503503
->setCronExpr($cronExpression)
504504
->setJobCode($jobCode)
505505
->setStatus(Schedule::STATUS_PENDING)
@@ -515,7 +515,7 @@ protected function generateSchedule($jobCode, $cronExpression, $time)
515515
*/
516516
protected function getScheduleTimeInterval($groupId)
517517
{
518-
$scheduleAheadFor = (int)$this->_scopeConfig->getValue(
518+
$scheduleAheadFor = (int)$this->scopeConfig->getValue(
519519
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_AHEAD_FOR,
520520
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
521521
);

0 commit comments

Comments
 (0)