Skip to content

Commit 19b14fb

Browse files
author
igor-chepurnoi
committed
add translations, search model and new migration for delete unused columns
1 parent 5234df9 commit 19b14fb

File tree

10 files changed

+282
-58
lines changed

10 files changed

+282
-58
lines changed

README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,32 @@ To log cron actions you should add behavior to all commands that should be logge
7575
'cronLogger' => [
7676
'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior',
7777
'actions' => [ // action names that should be logged
78-
'index',
79-
'test'
78+
'index'
8079
],
8180
],
8281
];
8382
}
8483
```
85-
As the result, you will be able to view list of cron runs at ```http://project.com/admin/settings/cron``` which contains:
86-
* ID auto-increment
87-
* Job Code - name of the action
88-
* Status - return code (0 for success)
89-
* Messages - exception trace
90-
* Date Created
91-
* Date Scheduled
92-
* Date Executed
93-
* Date Finished
84+
85+
## Internationalization
86+
87+
All text and messages introduced in this extension are translatable under category 'yii2mod-cron-log'.
88+
You may use translations provided within this extension, using following application configuration:
89+
90+
```php
91+
return [
92+
'components' => [
93+
'i18n' => [
94+
'translations' => [
95+
'yii2mod-cron-log' => [
96+
'class' => 'yii\i18n\PhpMessageSource',
97+
'basePath' => '@yii2mod/cron/messages',
98+
],
99+
// ...
100+
],
101+
],
102+
// ...
103+
],
104+
// ...
105+
];
106+
```

actions/CronLogAction.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace yii2mod\cron\actions;
44

5+
use Yii;
56
use yii\base\Action;
6-
use yii\data\ActiveDataProvider;
7-
use yii2mod\cron\models\CronScheduleModel;
7+
use yii2mod\cron\models\search\CronScheduleSearch;
88

99
/**
1010
* Class CronLogAction
@@ -13,24 +13,20 @@
1313
class CronLogAction extends Action
1414
{
1515
/**
16-
* @var string
16+
* @var string name of the view, which should be rendered.
1717
*/
1818
public $view = '@vendor/yii2mod/yii2-cron-log/views/index';
1919

2020
/**
21-
* Run action
21+
* Lists of all cron logs.
2222
*/
2323
public function run()
2424
{
25-
$dataProvider = new ActiveDataProvider([
26-
'query' => CronScheduleModel::find(),
27-
'pagination' => [
28-
'pageSize' => 20,
29-
],
30-
'sort' => ['defaultOrder' => ['id' => SORT_DESC]]
31-
]);
25+
$searchModel = new CronScheduleSearch();
26+
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
3227

3328
return $this->controller->render($this->view, [
29+
'searchModel' => $searchModel,
3430
'dataProvider' => $dataProvider
3531
]);
3632
}

messages/config.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
return [
4+
// string, required, root directory of all source files
5+
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..',
6+
// array, required, list of language codes that the extracted messages
7+
// should be translated to. For example, ['zh-CN', 'de'].
8+
'languages' => ['en', 'ru'],
9+
// string, the name of the function for translating messages.
10+
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
11+
// translated. You may use a string for single function name or an array for
12+
// multiple function names.
13+
'translator' => 'Yii::t',
14+
// boolean, whether to sort messages by keys when merging new messages
15+
// with the existing ones. Defaults to false, which means the new (untranslated)
16+
// messages will be separated from the old (translated) ones.
17+
'sort' => true,
18+
// boolean, whether to remove messages that no longer appear in the source code.
19+
// Defaults to false, which means each of these messages will be enclosed with a pair of '@@' marks.
20+
'removeUnused' => false,
21+
// array, list of patterns that specify which files (not directories) should be processed.
22+
// If empty or not set, all files will be processed.
23+
// Please refer to "except" for details about the patterns.
24+
'only' => ['*.php'],
25+
// array, list of patterns that specify which files/directories should NOT be processed.
26+
// If empty or not set, all files/directories will be processed.
27+
// A path matches a pattern if it contains the pattern string at its end. For example,
28+
// '/a/b' will match all files and directories ending with '/a/b';
29+
// the '*.svn' will match all files and directories whose name ends with '.svn'.
30+
// and the '.svn' will match all files and directories named exactly '.svn'.
31+
// Note, the '/' characters in a pattern matches both '/' and '\'.
32+
// See helpers/FileHelper::findFiles() description for more details on pattern matching rules.
33+
// If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
34+
'except' => [
35+
'.svn',
36+
'.git',
37+
'.gitignore',
38+
'.gitkeep',
39+
'.hgignore',
40+
'.hgkeep',
41+
'/messages',
42+
'/tests',
43+
'/runtime',
44+
'/vendor',
45+
],
46+
47+
// 'php' output format is for saving messages to php files.
48+
'format' => 'php',
49+
// Root directory containing message translations.
50+
'messagePath' => __DIR__,
51+
// boolean, whether the message file should be overwritten with the merged messages
52+
'overwrite' => true,
53+
54+
// Message categories to ignore
55+
'ignoreCategories' => [
56+
'yii',
57+
],
58+
];

messages/en/yii2mod-cron-log.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message/extract' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Cron Schedule Log' => 'Cron Schedule Log',
21+
'Date Created' => 'Date Created',
22+
'Date Finished' => 'Date Finished',
23+
'ID' => 'ID',
24+
'Job Code' => 'Job Code',
25+
'Messages' => 'Messages',
26+
'Select Status' => 'Select Status',
27+
'Status' => 'Status',
28+
'Success' => 'Success',
29+
'Error' => 'Error',
30+
'Run' => 'Run'
31+
];

messages/ru/yii2mod-cron-log.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message/extract' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Cron Schedule Log' => 'Журнал выполнения cron задач',
21+
'Date Created' => 'Дата Создания',
22+
'Date Finished' => 'Дата Завершения',
23+
'ID' => 'ИД',
24+
'Job Code' => 'Команда',
25+
'Messages' => 'Сообщение',
26+
'Select Status' => 'Выберите статус',
27+
'Status' => 'Статус',
28+
'Success' => 'Успешное завершение',
29+
'Error' => 'Ошибка',
30+
'Run' => 'Выполняется'
31+
];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use app\components\Migration;
4+
5+
class m160804_101551_drop_dateSheduled_and_dateExecuted_columns extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->dropColumn('{{%CronSchedule}}', 'dateScheduled');
10+
$this->dropColumn('{{%CronSchedule}}', 'dateExecuted');
11+
12+
$this->dropIndex('idx-CronSchedule-dateScheduled-status', '{{%CronSchedule}}');
13+
$this->createIndex('idx-CronSchedule-status', '{{%CronSchedule}}', ['status']);
14+
}
15+
16+
public function down()
17+
{
18+
$this->addColumn('{{%CronSchedule}}', 'dateScheduled', $this->timestamp()->null());
19+
$this->addColumn('{{%CronSchedule}}', 'dateExecuted', $this->timestamp()->null());
20+
21+
$this->dropIndex('idx-CronSchedule-status', '{{%CronSchedule}}');
22+
$this->createIndex('idx-CronSchedule-dateScheduled-status', '{{%CronSchedule}}', ['dateScheduled', 'status']);
23+
}
24+
25+
/*
26+
// Use safeUp/safeDown to run migration code within a transaction
27+
public function safeUp()
28+
{
29+
}
30+
31+
public function safeDown()
32+
{
33+
}
34+
*/
35+
}

models/CronScheduleModel.php

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,46 @@
1515
* @property string $status
1616
* @property string $messages
1717
* @property string $dateCreated
18-
* @property string $dateScheduled
19-
* @property string $dateExecuted
2018
* @property string $dateFinished
2119
*/
2220
class CronScheduleModel extends ActiveRecord
2321
{
24-
2522
/**
26-
* Declares the name of the database table associated with this AR class.
27-
* @return string the table name
23+
* @inheritdoc
2824
*/
2925
public static function tableName()
3026
{
3127
return '{{%CronSchedule}}';
3228
}
3329

3430
/**
35-
* Returns the validation rules for attributes.
36-
* @return array validation rules
31+
* @inheritdoc
3732
*/
3833
public function rules()
3934
{
4035
return [
41-
[['messages'], 'string'],
42-
[['dateCreated', 'dateScheduled', 'dateExecuted', 'dateFinished'], 'safe'],
43-
[['jobCode'], 'string', 'max' => 255],
36+
['messages', 'string'],
37+
['jobCode', 'string', 'max' => 255],
4438
['status', 'integer'],
39+
[['dateCreated', 'dateFinished'], 'safe'],
4540
];
4641
}
4742

4843
/**
49-
* Returns the attribute labels.
50-
* @return array attribute labels (name => label)
44+
* @inheritdoc
5145
*/
5246
public function attributeLabels()
5347
{
5448
return [
55-
'id' => Yii::t('cron', 'ID'),
56-
'jobCode' => Yii::t('cron', 'Job Code'),
57-
'status' => Yii::t('cron', 'Status'),
58-
'messages' => Yii::t('cron', 'Messages'),
59-
'dateCreated' => Yii::t('cron', 'Date Created'),
60-
'dateScheduled' => Yii::t('cron', 'Date Scheduled'),
61-
'dateExecuted' => Yii::t('cron', 'Date Executed'),
62-
'dateFinished' => Yii::t('cron', 'Date Finished'),
49+
'id' => Yii::t('yii2mod-cron-log', 'ID'),
50+
'jobCode' => Yii::t('yii2mod-cron-log', 'Job Code'),
51+
'status' => Yii::t('yii2mod-cron-log', 'Status'),
52+
'messages' => Yii::t('yii2mod-cron-log', 'Messages'),
53+
'dateCreated' => Yii::t('yii2mod-cron-log', 'Date Created'),
54+
'dateFinished' => Yii::t('yii2mod-cron-log', 'Date Finished'),
6355
];
6456
}
6557

66-
6758
/**
6859
* Start cron schedule
6960
*
@@ -77,8 +68,7 @@ public function startCronSchedule($jobCode, $status = CronScheduleStatus::RUN, $
7768
$this->jobCode = $jobCode;
7869
$this->status = $status;
7970
$this->messages = $messages;
80-
$this->dateScheduled = new Expression('NOW()');
81-
$this->dateExecuted = new Expression('NOW()');
71+
$this->dateCreated = new Expression('NOW()');
8272

8373
return $this->save();
8474
}
@@ -97,11 +87,10 @@ public function endCronSchedule($status, $messages = null)
9787
$this->dateFinished = new Expression('NOW()');
9888
$this->status = $status;
9989
$this->messages = $messages;
90+
10091
return $this->save();
10192
}
93+
10294
return false;
10395
}
104-
}
105-
106-
107-
96+
}

models/enumerables/CronScheduleStatus.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ class CronScheduleStatus extends BaseEnum
1414
const ERROR = 1;
1515
const RUN = 2;
1616

17+
/**
18+
* @var string message category
19+
*/
20+
public static $messageCategory = 'yii2mod-cron-log';
21+
22+
/**
23+
* @var array
24+
*/
1725
public static $list = [
26+
self::SUCCESS => 'Success',
1827
self::ERROR => 'Error',
19-
self::RUN => 'Run',
20-
self::SUCCESS => 'Complete',
28+
self::RUN => 'Run'
2129
];
2230
}

0 commit comments

Comments
 (0)