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 ' ,
@@ -336,7 +339,8 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
336
339
}
337
340
338
341
$ schedule ->setStatus (
339
- Schedule::STATUS_SUCCESS )->setFinishedAt (
342
+ Schedule::STATUS_SUCCESS
343
+ )->setFinishedAt (
340
344
strftime (
341
345
'%Y-%m-%d %H:%M:%S ' ,
342
346
$ this ->dateTime ->gmtTimestamp ()
@@ -411,7 +415,7 @@ private function getNonExitedSchedules($groupId)
411
415
$ jobs = $ this ->_config ->getJobs ();
412
416
$ pendingJobs = $ this ->_scheduleFactory ->create ()->getCollection ();
413
417
$ pendingJobs ->addFieldToFilter (
414
- 'status ' ,
418
+ 'status ' ,
415
419
[
416
420
'in ' => [
417
421
Schedule::STATUS_PENDING , Schedule::STATUS_RUNNING , Schedule::STATUS_SUCCESS
0 commit comments