Skip to content

Commit c3e94c0

Browse files
author
Chepurnoy
committed
init
1 parent 17d7e32 commit c3e94c0

File tree

7 files changed

+488
-0
lines changed

7 files changed

+488
-0
lines changed

actions/CronLogAction.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: semenov
5+
* Date: 04.07.14
6+
* Time: 20:31
7+
*/
8+
9+
namespace yii2mod\cron\actions;
10+
11+
12+
use yii\base\Action;
13+
use yii\data\ActiveDataProvider;
14+
use yii2mod\cron\models\CronScheduleModel;
15+
16+
/**
17+
* Class CronLogAction
18+
* @package yii2mod\cron\actions
19+
*/
20+
class CronLogAction extends Action
21+
{
22+
/**
23+
* @var string
24+
*/
25+
public $view = '@vendor/yii2mod/yii2-cron-log/views/index';
26+
27+
/**
28+
*
29+
*/
30+
public function run()
31+
{
32+
$dataProvider = new ActiveDataProvider([
33+
'query' => CronScheduleModel::find(),
34+
'pagination' => [
35+
'pageSize' => 20,
36+
],
37+
'sort' => ['defaultOrder' => ['id' => SORT_DESC]]
38+
]);
39+
40+
return $this->controller->render($this->view, [
41+
'dataProvider' => $dataProvider
42+
]);
43+
}
44+
45+
}

behaviors/CronLoggerBehavior.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
namespace yii2mod\cron\behaviors;
3+
4+
use Yii;
5+
use yii\base\Behavior;
6+
use yii\console\Controller;
7+
use yii2mod\cron\models\CronScheduleModel;
8+
9+
/**
10+
* CronLoggerBehavior allows logging of the console command running schedule.
11+
* This behavior adjusts the application event handles to intercept any errors and exception and log
12+
* them properly.
13+
*
14+
* 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'.
16+
* In order to signal an error, command action should return string error message.
17+
*
18+
* Usage:
19+
* <code>
20+
* class MyCommand extends CConsoleCommand
21+
* {
22+
* public function behaviors()
23+
* {
24+
* return array(
25+
* 'mutexBehavior' => array(
26+
* 'class' => 'CronLoggerBehavior',
27+
* 'actions' => array('index'),
28+
* ),
29+
* );
30+
* }
31+
* }
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
40+
*/
41+
class CronLoggerBehavior extends Behavior
42+
{
43+
/**
44+
* @var CronScheduleModel
45+
*/
46+
protected $schedule;
47+
48+
/**
49+
* @var array list of action names, which should be logged.
50+
*/
51+
public $actions = array();
52+
53+
/**
54+
* @var string error message
55+
*/
56+
public $message = '';
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function events()
62+
{
63+
return [
64+
Controller::EVENT_BEFORE_ACTION => 'beforeAction',
65+
Controller::EVENT_AFTER_ACTION => 'afterAction',
66+
];
67+
}
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
public function beforeAction($event)
73+
{
74+
if (isset($this->actions) && is_array($this->actions) && in_array(strtolower($event->action->id), $this->actions)) {
75+
/* @var CConsoleCommand $sender */
76+
$sender = $event->sender;
77+
$command = $sender->id . '/' . $sender->action->id;
78+
$this->schedule = new CronScheduleModel();
79+
$this->schedule->startCronSchedule($command);
80+
$this->setupApplicationErrorHandlers();
81+
}
82+
}
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function afterAction($event)
88+
{
89+
if ($this->schedule) {
90+
$exitCode = (int)$event->result;
91+
if ($exitCode == 0) {
92+
$exitCode = 'success';
93+
} else {
94+
$exitCode = 'error';
95+
}
96+
$this->schedule->endCronSchedule($exitCode);
97+
$this->schedule = null;
98+
}
99+
}
100+
101+
/**
102+
* Sets up application error event handlers.
103+
*/
104+
protected function setupApplicationErrorHandlers()
105+
{
106+
$errorHandler = Yii::$app->get('errorHandler');
107+
$errorHandler->schedule = &$this->schedule;
108+
}
109+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: semenov
5+
* Date: 08.07.14
6+
* Time: 11:13
7+
*/
8+
9+
namespace yii2mod\cron\behaviors;
10+
11+
use yii\base\Behavior;
12+
use yii\console\Controller;
13+
14+
15+
/**
16+
* MutexConsoleCommandBehavior allows console command actions being run with mutex protection.
17+
*
18+
* Usage:
19+
* <code>
20+
* class MyCommand extends Controller
21+
* {
22+
* public function behaviors()
23+
* {
24+
* return array(
25+
* 'mutexBehavior' => array(
26+
* 'class' => 'yii2mod\cron\behaviors\MutexConsoleCommandBehavior',
27+
* 'mutexActions' => array('index'),
28+
* ),
29+
* );
30+
* }
31+
* }
32+
* </code>
33+
*
34+
* @method \Controller getOwner()
35+
*
36+
* @author Klimov Paul <klimov@zfort.com>
37+
* @author Dmitry Semenov <disemx@gmail.com>
38+
* @version $Id$
39+
* @package zfort\mutex\behavior
40+
* @since 1.0
41+
*/
42+
class MutexConsoleCommandBehavior extends Behavior
43+
{
44+
/**
45+
* @var string name of the mutex application component.
46+
*/
47+
public $mutex = 'mutex';
48+
/**
49+
* @var array list of action names, which mutex should be applied to.
50+
*/
51+
public $mutexActions = array();
52+
/**
53+
* @var integer exit code, which should be returned by console command in case it
54+
* is terminated due to mutex lock.
55+
*/
56+
public $mutexExitCode = 100;
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function events()
62+
{
63+
return [
64+
Controller::EVENT_BEFORE_ACTION => 'beforeAction',
65+
Controller::EVENT_AFTER_ACTION => 'afterAction',
66+
];
67+
}
68+
69+
/**
70+
* @return Mutex mutex application component instance.
71+
*/
72+
public function getMutex()
73+
{
74+
return \Yii::$app->get($this->mutex);
75+
}
76+
77+
/**
78+
* Composes the mutex name.
79+
*
80+
* @param string $action command action name.
81+
*
82+
* @return string mutex name.
83+
*/
84+
protected function composeMutexName($action)
85+
{
86+
return $this->getOwner()->getName() . '-' . $action;
87+
}
88+
89+
/**
90+
* Checks if specified action is among mutex actions.
91+
*
92+
* @param string $action action name.
93+
*
94+
* @return boolean whether action should be under mutex.
95+
*/
96+
public function checkIsMutexAction($action)
97+
{
98+
return in_array(strtolower($action), $this->mutexActions);
99+
}
100+
101+
/**
102+
* Responds to {@link CConsoleCommand::onBeforeAction} event.
103+
* Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
104+
*
105+
* @param CConsoleCommandEvent $event event parameter
106+
*/
107+
public function beforeAction($event)
108+
{
109+
if ($this->checkIsMutexAction($event->action)) {
110+
$mutexName = $this->composeMutexName($event->action);
111+
if (!$this->getMutex()->acquire($mutexName)) {
112+
echo "Execution terminated: command is already running.\n";
113+
$event->stopCommand = true;
114+
$event->exitCode = $this->mutexExitCode;
115+
}
116+
}
117+
}
118+
119+
/**
120+
* Responds to {@link CConsoleCommand::onAfterAction} event.
121+
* Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
122+
*
123+
* @param CConsoleCommandEvent $event event parameter
124+
*/
125+
public function afterAction($event)
126+
{
127+
if ($this->checkIsMutexAction($event->action)) {
128+
$mutexName = $this->composeMutexName($event->action);
129+
$this->getMutex()->release($mutexName);
130+
}
131+
}
132+
}

components/ErrorHandler.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace yii2mod\cron\components;
3+
4+
use Yii;
5+
use yii\web\HttpException;
6+
7+
/**
8+
* Class ErrorHandler
9+
* @package yii2mod\cron\components
10+
*/
11+
class ErrorHandler extends \yii\console\ErrorHandler
12+
{
13+
/**
14+
* @var Schedule model
15+
*/
16+
public $schedule;
17+
18+
/**
19+
* Logs the given exception
20+
*
21+
* @param \Exception $exception the exception to be logged
22+
*/
23+
protected function logException($exception)
24+
{
25+
$category = get_class($exception);
26+
if ($exception instanceof HttpException) {
27+
$category = 'yii\\web\\HttpException:' . $exception->statusCode;
28+
} elseif ($exception instanceof \ErrorException) {
29+
$category .= ':' . $exception->getSeverity();
30+
}
31+
if ($this->schedule) {
32+
$this->schedule->endCronSchedule('error', (string)$exception);
33+
$this->schedule = null;
34+
}
35+
\Yii::error((string)$exception, $category);
36+
}
37+
38+
}

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "yii2mod/yii2-cron-log",
3+
"description": "Component for logging cron jobs",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2", "extension"],
6+
"license": "Apache-2.0",
7+
"authors": [
8+
{
9+
"name": "Dmitry Semenov",
10+
"email": "disemx@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"yii2mod\\cron\\": ""
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)