yii2mod / yii2-cron-log
Component for logging cron jobs
Installs: 22 665
Dependents: 1
Suggesters: 0
Security: 0
Stars: 23
Watchers: 7
Forks: 8
Open Issues: 0
Type:yii2-extension
Requires
- yii2mod/yii2-enum: >=1.4
- yiisoft/yii2: ~2.0.12
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ~6.0
README
Yii2 Cron Log Extension
Component for logging cron jobs.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-cron-log "*"
or add
"yii2mod/yii2-cron-log": "*"
to the require section of your composer.json.
Configuration
Database Migrations
Before using this extension, we'll also need to prepare the database.
php yii migrate/up --migrationPath=@yii2mod/cron/migrations
Error Handler and File Mutex Setup
Error handler must be defined inside console config, it will be used to log exceptions into database.
FileMutex implements mutex "lock" mechanism via local file system files.
Add the following code to your console application configuration:
'components' => [ 'errorHandler' => [ 'class' => 'yii2mod\cron\components\ErrorHandler', ], 'mutex' => [ 'class' => 'yii\mutex\FileMutex' ], ],
Usage
- To access the list of executed commands, you need to define
CronLogAction
in any controller (for example /modules/admin/SettingsController.php):
public function actions() { return [ 'cron' => 'yii2mod\cron\actions\CronLogAction', // Also you can override some action properties in following way: 'cron' => [ 'class' => 'yii2mod\cron\actions\CronLogAction', 'searchClass' => [ 'class' => 'yii2mod\cron\models\search\CronScheduleSearch', 'pageSize' => 10 ], 'view' => 'custom name of the view, which should be rendered.' ] ]; }
This action is used to view list of executed commands: http://project.com/admin/settings/cron
- To log cron actions you should add behavior to all commands that should be logged. In the following example
CronLoggerBehavior
will be log theindex
action.
namespace app\commands; use yii\console\Controller; /** * This command echoes the first argument that you have entered. * * This command is provided as an example for you to learn how to create console commands. * */ class HelloController extends Controller { public function behaviors() { return [ 'cronLogger' => [ 'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior', 'actions' => ['index'] ], // Example of usage the `MutexConsoleCommandBehavior` 'mutexBehavior' => [ 'class' => 'yii2mod\cron\behaviors\MutexConsoleCommandBehavior', 'mutexActions' => ['index'], 'timeout' => 3600, //default 0 ] ]; } /** * This command echoes what you have entered as the message. * @param string $message the message to be echoed. */ public function actionIndex($message = 'hello world') { echo $message . "\n"; } }
Internationalization
All text and messages introduced in this extension are translatable under category 'yii2mod-cron-log'. You may use translations provided within this extension, using following application configuration:
return [ 'components' => [ 'i18n' => [ 'translations' => [ 'yii2mod-cron-log' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@yii2mod/cron/messages', ], // ... ], ], // ... ], // ... ];
Support us
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.