Skip to content

Commit ab0540d

Browse files
author
Chepurnoy
committed
update cron behavior, model and error handler
1 parent 2f4d339 commit ab0540d

File tree

6 files changed

+85
-67
lines changed

6 files changed

+85
-67
lines changed

actions/CronLogAction.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: semenov
5-
* Date: 04.07.14
6-
* Time: 20:31
7-
*/
82

93
namespace yii2mod\cron\actions;
104

11-
125
use yii\base\Action;
136
use yii\data\ActiveDataProvider;
147
use yii2mod\cron\models\CronScheduleModel;
@@ -25,7 +18,7 @@ class CronLogAction extends Action
2518
public $view = '@vendor/yii2mod/yii2-cron-log/views/index';
2619

2720
/**
28-
*
21+
* Run action
2922
*/
3023
public function run()
3124
{

behaviors/CronLoggerBehavior.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
11
<?php
2+
23
namespace yii2mod\cron\behaviors;
34

45
use Yii;
56
use yii\base\Behavior;
67
use yii\console\Controller;
78
use yii2mod\cron\models\CronScheduleModel;
9+
use yii2mod\cron\models\enumerables\CronScheduleStatus;
810

911
/**
1012
* CronLoggerBehavior allows logging of the console command running schedule.
1113
* This behavior adjusts the application event handles to intercept any errors and exception and log
1214
* them properly.
1315
*
1416
* You may adjust log result using the exit code, which is returned by command action.
15-
* In order to mark success, command action should return '0'.
1617
* In order to signal an error, command action should return string error message.
1718
*
18-
* Usage:
19-
* <code>
20-
* class MyCommand extends CConsoleCommand
19+
* ~~~
20+
* class JobCommand extends Controller
2121
* {
2222
* public function behaviors()
2323
* {
24-
* return array(
25-
* 'mutexBehavior' => array(
24+
* return [
25+
* 'cronLogger' => [
2626
* 'class' => 'CronLoggerBehavior',
27-
* 'actions' => array('index'),
28-
* ),
29-
* );
27+
* 'actions' => ['index'], // OR ['*'] - attach to all actions
28+
* ],
29+
* ];
3030
* }
3131
* }
32-
* </code>
33-
*
34-
* @author Roman Protsenko <protsenko@zfort.com>
35-
* @author Klimov Paul <klimov@zfort.com>
36-
* @author Dmitry Semenov <disemx@gmail.com>
37-
* @version $Id$
38-
* @package default
39-
* @since 1.0
32+
* ~~~
4033
*/
4134
class CronLoggerBehavior extends Behavior
4235
{
@@ -48,15 +41,17 @@ class CronLoggerBehavior extends Behavior
4841
/**
4942
* @var array list of action names, which should be logged.
5043
*/
51-
public $actions = array();
44+
public $actions = [];
5245

5346
/**
5447
* @var string error message
5548
*/
5649
public $message = '';
5750

5851
/**
59-
* @inheritdoc
52+
* Declares event handlers for the [[owner]]'s events.
53+
*
54+
* @return array events (array keys) and the corresponding event handler methods (array values).
6055
*/
6156
public function events()
6257
{
@@ -67,12 +62,12 @@ public function events()
6762
}
6863

6964
/**
70-
* @inheritdoc
65+
* Before action
66+
* @param $event \yii\base\ActionEvent
7167
*/
7268
public function beforeAction($event)
7369
{
74-
if (isset($this->actions) && is_array($this->actions) && in_array(strtolower($event->action->id), $this->actions)) {
75-
/* @var CConsoleCommand $sender */
70+
if (in_array($event->action->id, $this->actions) || in_array('*', $this->actions)) {
7671
$sender = $event->sender;
7772
$command = $sender->id . '/' . $sender->action->id;
7873
$this->schedule = new CronScheduleModel();
@@ -82,16 +77,16 @@ public function beforeAction($event)
8277
}
8378

8479
/**
85-
* @inheritdoc
80+
* After action
81+
* @param $event \yii\base\ActionEvent
8682
*/
8783
public function afterAction($event)
8884
{
8985
if ($this->schedule) {
90-
$exitCode = (int)$event->result;
91-
if ($exitCode == 0) {
92-
$exitCode = 'success';
86+
if ($event->result === false) {
87+
$exitCode = CronScheduleStatus::ERROR;
9388
} else {
94-
$exitCode = 'error';
89+
$exitCode = CronScheduleStatus::SUCCESS;
9590
}
9691
$this->schedule->endCronSchedule($exitCode);
9792
$this->schedule = null;

components/ErrorHandler.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
23
namespace yii2mod\cron\components;
34

45
use Yii;
56
use yii\web\HttpException;
7+
use yii2mod\cron\models\CronScheduleModel;
8+
use yii2mod\cron\models\enumerables\CronScheduleStatus;
69

710
/**
811
* Class ErrorHandler
@@ -11,7 +14,7 @@
1114
class ErrorHandler extends \yii\console\ErrorHandler
1215
{
1316
/**
14-
* @var Schedule model
17+
* @var CronScheduleModel model
1518
*/
1619
public $schedule;
1720

@@ -20,7 +23,7 @@ class ErrorHandler extends \yii\console\ErrorHandler
2023
*
2124
* @param \Exception $exception the exception to be logged
2225
*/
23-
protected function logException($exception)
26+
public function logException($exception)
2427
{
2528
$category = get_class($exception);
2629
if ($exception instanceof HttpException) {
@@ -29,10 +32,10 @@ protected function logException($exception)
2932
$category .= ':' . $exception->getSeverity();
3033
}
3134
if ($this->schedule) {
32-
$this->schedule->endCronSchedule('error', (string)$exception);
35+
$this->schedule->endCronSchedule(CronScheduleStatus::ERROR, (string)$exception);
3336
$this->schedule = null;
3437
}
3538
\Yii::error((string)$exception, $category);
3639
}
3740

38-
}
41+
}

composer.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
"keywords": ["yii2", "extension"],
66
"license": "Apache-2.0",
77
"authors": [
8-
{
9-
"name": "Dmitry Semenov",
10-
"email": "disemx@gmail.com"
11-
}
8+
{
9+
"name": "Dmitry Semenov",
10+
"email": "disemx@gmail.com"
11+
},
12+
{
13+
"name": "Igor Chepurnoy",
14+
"email": "igorzfort@gmail.com"
15+
}
1216
],
17+
"require": {
18+
"yii2mod/yii2-enum": "dev-master"
19+
},
1320
"autoload": {
14-
"psr-4": {
15-
"yii2mod\\cron\\": ""
16-
}
21+
"psr-4": {
22+
"yii2mod\\cron\\": ""
23+
}
1724
}
1825
}

models/CronScheduleModel.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2+
23
namespace yii2mod\cron\models;
34

45
use Yii;
56
use yii\db\ActiveRecord;
67
use yii\db\Expression;
8+
use yii2mod\cron\models\enumerables\CronScheduleStatus;
79

810
/**
911
* This is the model class for table "CronSchedule".
@@ -19,29 +21,33 @@
1921
*/
2022
class CronScheduleModel extends ActiveRecord
2123
{
24+
2225
/**
23-
* @inheritdoc
26+
* Declares the name of the database table associated with this AR class.
27+
* @return string the table name
2428
*/
2529
public static function tableName()
2630
{
27-
return 'CronSchedule';
31+
return '{{%CronSchedule}}';
2832
}
2933

3034
/**
31-
* @inheritdoc
35+
* Returns the validation rules for attributes.
36+
* @return array validation rules
3237
*/
3338
public function rules()
3439
{
3540
return [
3641
[['messages'], 'string'],
3742
[['dateCreated', 'dateScheduled', 'dateExecuted', 'dateFinished'], 'safe'],
3843
[['jobCode'], 'string', 'max' => 255],
39-
[['status'], 'string', 'max' => 255]
44+
['status', 'integer'],
4045
];
4146
}
4247

4348
/**
44-
* @inheritdoc
49+
* Returns the attribute labels.
50+
* @return array attribute labels (name => label)
4551
*/
4652
public function attributeLabels()
4753
{
@@ -59,38 +65,30 @@ public function attributeLabels()
5965

6066

6167
/**
62-
*
63-
* @author Roman Protsenko <protsenko@zfort.com>
68+
* Start cron schedule
6469
*
6570
* @param string $jobCode
66-
* @param string $status
67-
* @param null $messages
68-
*
69-
* @internal param string $message
70-
* @return boolean
71+
* @param int $status
72+
* @param null $messages
73+
* @return bool
7174
*/
72-
public function startCronSchedule($jobCode, $status = null, $messages = null)
75+
public function startCronSchedule($jobCode, $status = CronScheduleStatus::RUN, $messages = null)
7376
{
74-
if ($status === null) {
75-
$status = 'running';
76-
}
7777
$this->jobCode = $jobCode;
7878
$this->status = $status;
7979
$this->messages = $messages;
80-
8180
$this->dateScheduled = new Expression('NOW()');
8281
$this->dateExecuted = new Expression('NOW()');
82+
8383
return $this->save();
8484
}
8585

8686
/**
87-
*
88-
* @author Roman Protsenko <protsenko@zfort.com>
87+
* End cron schedule
8988
*
9089
* @param string $status
91-
* @param null $messages
90+
* @param null $messages
9291
*
93-
* @internal param string $message
9492
* @return boolean
9593
*/
9694
public function endCronSchedule($status, $messages = null)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace yii2mod\cron\models\enumerables;
4+
5+
use yii2mod\enum\helpers\BaseEnum;
6+
7+
/**
8+
* Class CronScheduleStatus
9+
* @package yii2mod\cron\models\enumerables
10+
*/
11+
class CronScheduleStatus extends BaseEnum
12+
{
13+
const SUCCESS = 0;
14+
const ERROR = 1;
15+
const RUN = 2;
16+
17+
public static $list = [
18+
self::ERROR => 'Error',
19+
self::RUN => 'Run',
20+
self::SUCCESS => 'Complete',
21+
];
22+
}

0 commit comments

Comments
 (0)