3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
/**
8
7
* Handling cron jobs
9
8
*/
10
9
namespace Magento \Cron \Observer ;
11
10
11
+ use Magento \Cron \Model \Schedule ;
12
12
use Magento \Framework \App \State ;
13
13
use Magento \Framework \Console \Cli ;
14
14
use Magento \Framework \Event \ObserverInterface ;
15
- use Magento \Cron \Model \Schedule ;
16
15
use Magento \Framework \Profiler \Driver \Standard \Stat ;
17
16
use Magento \Framework \Profiler \Driver \Standard \StatFactory ;
18
17
@@ -204,7 +203,6 @@ public function __construct(
204
203
*/
205
204
public function execute (\Magento \Framework \Event \Observer $ observer )
206
205
{
207
-
208
206
$ currentTime = $ this ->dateTime ->gmtTimestamp ();
209
207
$ jobGroupsRoot = $ this ->_config ->getJobs ();
210
208
// sort jobs groups to start from used in separated process
@@ -258,7 +256,6 @@ function ($groupId) use ($currentTime, $jobsRoot) {
258
256
*/
259
257
private function lockGroup ($ groupId , callable $ callback )
260
258
{
261
-
262
259
if (!$ this ->lockManager ->lock (self ::LOCK_PREFIX . $ groupId , self ::LOCK_TIMEOUT )) {
263
260
$ this ->logger ->warning (
264
261
sprintf (
@@ -293,17 +290,20 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
293
290
$ scheduleLifetime = $ scheduleLifetime * self ::SECONDS_IN_MINUTE ;
294
291
if ($ scheduledTime < $ currentTime - $ scheduleLifetime ) {
295
292
$ schedule ->setStatus (Schedule::STATUS_MISSED );
293
+ // phpcs:ignore Magento2.Exceptions.DirectThrow
296
294
throw new \Exception (sprintf ('Cron Job %s is missed at %s ' , $ jobCode , $ schedule ->getScheduledAt ()));
297
295
}
298
296
299
297
if (!isset ($ jobConfig ['instance ' ], $ jobConfig ['method ' ])) {
300
298
$ schedule ->setStatus (Schedule::STATUS_ERROR );
299
+ // phpcs:ignore Magento2.Exceptions.DirectThrow
301
300
throw new \Exception (sprintf ('No callbacks found for cron job %s ' , $ jobCode ));
302
301
}
303
302
$ model = $ this ->_objectManager ->create ($ jobConfig ['instance ' ]);
304
303
$ callback = [$ model , $ jobConfig ['method ' ]];
305
304
if (!is_callable ($ callback )) {
306
305
$ schedule ->setStatus (Schedule::STATUS_ERROR );
306
+ // phpcs:ignore Magento2.Exceptions.DirectThrow
307
307
throw new \Exception (
308
308
sprintf ('Invalid callback: %s::%s can \'t be called ' , $ jobConfig ['instance ' ], $ jobConfig ['method ' ])
309
309
);
@@ -314,15 +314,18 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
314
314
$ this ->startProfiling ();
315
315
try {
316
316
$ this ->logger ->info (sprintf ('Cron Job %s is run ' , $ jobCode ));
317
+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
317
318
call_user_func_array ($ callback , [$ schedule ]);
318
319
} catch (\Throwable $ e ) {
319
320
$ schedule ->setStatus (Schedule::STATUS_ERROR );
320
- $ this ->logger ->error (sprintf (
321
- 'Cron Job %s has an error: %s. Statistics: %s ' ,
322
- $ jobCode ,
323
- $ e ->getMessage (),
324
- $ this ->getProfilingStat ()
325
- ));
321
+ $ this ->logger ->error (
322
+ sprintf (
323
+ 'Cron Job %s has an error: %s. Statistics: %s ' ,
324
+ $ jobCode ,
325
+ $ e ->getMessage (),
326
+ $ this ->getProfilingStat ()
327
+ )
328
+ );
326
329
if (!$ e instanceof \Exception) {
327
330
$ e = new \RuntimeException (
328
331
'Error when running a cron job ' ,
@@ -335,16 +338,22 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
335
338
$ this ->stopProfiling ();
336
339
}
337
340
338
- $ schedule ->setStatus (Schedule::STATUS_SUCCESS )->setFinishedAt (strftime (
339
- '%Y-%m-%d %H:%M:%S ' ,
340
- $ this ->dateTime ->gmtTimestamp ()
341
- ));
341
+ $ schedule ->setStatus (
342
+ Schedule::STATUS_SUCCESS
343
+ )->setFinishedAt (
344
+ strftime (
345
+ '%Y-%m-%d %H:%M:%S ' ,
346
+ $ this ->dateTime ->gmtTimestamp ()
347
+ )
348
+ );
342
349
343
- $ this ->logger ->info (sprintf (
344
- 'Cron Job %s is successfully finished. Statistics: %s ' ,
345
- $ jobCode ,
346
- $ this ->getProfilingStat ()
347
- ));
350
+ $ this ->logger ->info (
351
+ sprintf (
352
+ 'Cron Job %s is successfully finished. Statistics: %s ' ,
353
+ $ jobCode ,
354
+ $ this ->getProfilingStat ()
355
+ )
356
+ );
348
357
}
349
358
350
359
/**
@@ -395,6 +404,28 @@ private function getPendingSchedules($groupId)
395
404
return $ pendingJobs ;
396
405
}
397
406
407
+ /**
408
+ * Return job collection from database with status 'pending', 'running' or 'success'
409
+ *
410
+ * @param string $groupId
411
+ * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
412
+ */
413
+ private function getNonExitedSchedules ($ groupId )
414
+ {
415
+ $ jobs = $ this ->_config ->getJobs ();
416
+ $ pendingJobs = $ this ->_scheduleFactory ->create ()->getCollection ();
417
+ $ pendingJobs ->addFieldToFilter (
418
+ 'status ' ,
419
+ [
420
+ 'in ' => [
421
+ Schedule::STATUS_PENDING , Schedule::STATUS_RUNNING , Schedule::STATUS_SUCCESS
422
+ ]
423
+ ]
424
+ );
425
+ $ pendingJobs ->addFieldToFilter ('job_code ' , ['in ' => array_keys ($ jobs [$ groupId ])]);
426
+ return $ pendingJobs ;
427
+ }
428
+
398
429
/**
399
430
* Generate cron schedule
400
431
*
@@ -426,7 +457,7 @@ private function generateSchedules($groupId)
426
457
null
427
458
);
428
459
429
- $ schedules = $ this ->getPendingSchedules ($ groupId );
460
+ $ schedules = $ this ->getNonExitedSchedules ($ groupId );
430
461
$ exists = [];
431
462
/** @var Schedule $schedule */
432
463
foreach ($ schedules as $ schedule ) {
@@ -669,11 +700,14 @@ private function cleanupScheduleMismatches()
669
700
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
670
701
$ scheduleResource = $ this ->_scheduleFactory ->create ()->getResource ();
671
702
foreach ($ this ->invalid as $ jobCode => $ scheduledAtList ) {
672
- $ scheduleResource ->getConnection ()->delete ($ scheduleResource ->getMainTable (), [
673
- 'status = ? ' => Schedule::STATUS_PENDING ,
674
- 'job_code = ? ' => $ jobCode ,
675
- 'scheduled_at in (?) ' => $ scheduledAtList ,
676
- ]);
703
+ $ scheduleResource ->getConnection ()->delete (
704
+ $ scheduleResource ->getMainTable (),
705
+ [
706
+ 'status = ? ' => Schedule::STATUS_PENDING ,
707
+ 'job_code = ? ' => $ jobCode ,
708
+ 'scheduled_at in (?) ' => $ scheduledAtList ,
709
+ ]
710
+ );
677
711
}
678
712
return $ this ;
679
713
}
0 commit comments