Skip to content

Commit 5caa248

Browse files
author
Chepurnoy
committed
2 parents ab0540d + ad07a9b commit 5caa248

File tree

3 files changed

+122
-40
lines changed

3 files changed

+122
-40
lines changed

README.md

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ Cron log
22
=============
33
Component for logging cron jobs
44

5-
To use this extension, add to actions in your controller below code:
6-
7-
```php
8-
public function actions()
9-
{
10-
return [
11-
'cron' => 'yii2mod\cron\actions\CronLogAction',
12-
];
13-
}
14-
```
15-
165
Installation
176
------------
187

@@ -31,3 +20,72 @@ or add
3120
```
3221

3322
to the require section of your composer.json.
23+
24+
run
25+
```php
26+
php yii migrate/up --migrationPath=@yii2mod/cron/migrations
27+
```
28+
Please note that messages are wrapped with ```Yii::t()``` to support message translations, you should define default message source for them if you don't use i18n.
29+
```php
30+
'i18n' => [
31+
'translations' => [
32+
'*' => [
33+
'class' => 'yii\i18n\PhpMessageSource'
34+
],
35+
],
36+
],
37+
```
38+
39+
Usage
40+
------------
41+
Error handler must be defined inside console config, it will be used to log exceptions into database.
42+
```php
43+
'components' => [
44+
'errorHandler' => [
45+
'class' => 'yii2mod\cron\components\ErrorHandler',
46+
],
47+
'mutex' => [
48+
'class' => 'yii\mutex\FileMutex'
49+
],
50+
],
51+
```
52+
53+
To use this extension you need define action in any controller (for example /modules/admin/SettingsController.php)
54+
```php
55+
public function actions()
56+
{
57+
return [
58+
'cron' => 'yii2mod\cron\actions\CronLogAction',
59+
];
60+
}
61+
```
62+
This action is used to view list of executed commands. // http://project.com/admin/settings/cron
63+
64+
65+
To log cron actions you should add behavior to all commands that should be logged.
66+
```php
67+
/**
68+
* @return array behavior configurations.
69+
*/
70+
public function behaviors()
71+
{
72+
return array(
73+
'cronLogger' => array(
74+
'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior',
75+
'actions' => array( // action names that should be logged
76+
'index',
77+
'test
78+
),
79+
),
80+
);
81+
}
82+
```
83+
As the result, you will be able to view list of cron runs at ```http://project.com/admin/settings/cron``` which contains:
84+
* ID auto-increment
85+
* Job Code - name of the action
86+
* Status - return code (0 for success)
87+
* Messages - exception trace
88+
* Date Created
89+
* Date Scheduled
90+
* Date Executed
91+
* Date Finished

behaviors/MutexConsoleCommandBehavior.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: semenov
5-
* Date: 08.07.14
6-
* Time: 11:13
7-
*/
8-
92
namespace yii2mod\cron\behaviors;
103

114
use yii\base\Behavior;
@@ -36,7 +29,7 @@
3629
* @author Klimov Paul <klimov@zfort.com>
3730
* @author Dmitry Semenov <disemx@gmail.com>
3831
* @version $Id$
39-
* @package zfort\mutex\behavior
32+
* @package yii2mod\cron\behaviors
4033
* @since 1.0
4134
*/
4235
class MutexConsoleCommandBehavior extends Behavior
@@ -49,11 +42,7 @@ class MutexConsoleCommandBehavior extends Behavior
4942
* @var array list of action names, which mutex should be applied to.
5043
*/
5144
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;
45+
5746

5847
/**
5948
* @inheritdoc
@@ -83,7 +72,7 @@ public function getMutex()
8372
*/
8473
protected function composeMutexName($action)
8574
{
86-
return $this->getOwner()->getName() . '-' . $action;
75+
return $this->owner->getUniqueId() . '-' . $action;
8776
}
8877

8978
/**
@@ -98,35 +87,32 @@ public function checkIsMutexAction($action)
9887
return in_array(strtolower($action), $this->mutexActions);
9988
}
10089

90+
10191
/**
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
92+
* @param $event
93+
* @return bool
10694
*/
10795
public function beforeAction($event)
10896
{
109-
if ($this->checkIsMutexAction($event->action)) {
110-
$mutexName = $this->composeMutexName($event->action);
97+
if ($this->checkIsMutexAction($event->action->id)) {
98+
$mutexName = $this->composeMutexName($event->action->id);
11199
if (!$this->getMutex()->acquire($mutexName)) {
112100
echo "Execution terminated: command is already running.\n";
113-
$event->stopCommand = true;
114-
$event->exitCode = $this->mutexExitCode;
101+
$event->isValid = false;
102+
return false;
115103
}
116104
}
117105
}
118106

107+
119108
/**
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
109+
* @param $event
124110
*/
125111
public function afterAction($event)
126112
{
127-
if ($this->checkIsMutexAction($event->action)) {
128-
$mutexName = $this->composeMutexName($event->action);
113+
if ($this->checkIsMutexAction($event->action->id)) {
114+
$mutexName = $this->composeMutexName($event->action->id);
129115
$this->getMutex()->release($mutexName);
130116
}
131117
}
132-
}
118+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use yii\db\Schema;
4+
use yii\db\Migration;
5+
6+
class m150105_172247_create_cron_schedule_tbl extends Migration
7+
{
8+
public function safeUp()
9+
{
10+
$tableOptions = null;
11+
if ($this->db->driverName === 'mysql') {
12+
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
13+
}
14+
15+
$this->createTable(
16+
'{{%CronSchedule}}',
17+
[
18+
'id' => Schema::TYPE_PK,
19+
'jobCode' => Schema::TYPE_STRING . '(255) NULL DEFAULT NULL',
20+
'status' => Schema::TYPE_STRING . '(255) NULL DEFAULT NULL',
21+
'messages' => Schema::TYPE_TEXT . ' NULL',
22+
'dateCreated' => Schema::TYPE_TIMESTAMP . ' NULL DEFAULT NULL',
23+
'dateScheduled' => Schema::TYPE_TIMESTAMP . ' NULL DEFAULT NULL',
24+
'dateExecuted' => Schema::TYPE_TIMESTAMP . ' NULL DEFAULT NULL',
25+
'dateFinished' => Schema::TYPE_TIMESTAMP . ' NULL DEFAULT NULL',
26+
],
27+
$tableOptions
28+
);
29+
30+
$this->createIndex('IDX_CRON_SCHEDULE_JOB_CODE', '{{%CronSchedule}}', ['jobCode']);
31+
$this->createIndex('IDX_CRON_SCHEDULE_SCHEDULED_AT_STATUS', '{{%CronSchedule}}', ['dateScheduled', 'status']);
32+
}
33+
34+
public function safeDown()
35+
{
36+
$this->dropTable('{{%CronSchedule}}');
37+
}
38+
}

0 commit comments

Comments
 (0)