| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\queue;
- use Yii;
- use yii\base\Behavior;
- /**
- * Log Behavior.
- *
- * @author Roman Zhuravlev <zhuravljov@gmail.com>
- */
- class LogBehavior extends Behavior
- {
- /**
- * @var Queue
- * @inheritdoc
- */
- public $owner;
- /**
- * @var bool
- */
- public $autoFlush = true;
- /**
- * @inheritdoc
- */
- public function events()
- {
- return [
- Queue::EVENT_AFTER_PUSH => 'afterPush',
- Queue::EVENT_BEFORE_EXEC => 'beforeExec',
- Queue::EVENT_AFTER_EXEC => 'afterExec',
- Queue::EVENT_AFTER_ERROR => 'afterError',
- cli\Queue::EVENT_WORKER_START => 'workerStart',
- cli\Queue::EVENT_WORKER_STOP => 'workerStop',
- ];
- }
- /**
- * @param PushEvent $event
- */
- public function afterPush(PushEvent $event)
- {
- $title = $this->getJobTitle($event);
- Yii::info("$title is pushed.", Queue::class);
- }
- /**
- * @param ExecEvent $event
- */
- public function beforeExec(ExecEvent $event)
- {
- $title = $this->getExecTitle($event);
- Yii::info("$title is started.", Queue::class);
- Yii::beginProfile($title, Queue::class);
- }
- /**
- * @param ExecEvent $event
- */
- public function afterExec(ExecEvent $event)
- {
- $title = $this->getExecTitle($event);
- Yii::endProfile($title, Queue::class);
- Yii::info("$title is finished.", Queue::class);
- if ($this->autoFlush) {
- Yii::getLogger()->flush(true);
- }
- }
- /**
- * @param ExecEvent $event
- */
- public function afterError(ExecEvent $event)
- {
- $title = $this->getExecTitle($event);
- Yii::endProfile($title, Queue::class);
- Yii::error("$title is finished with error: $event->error.", Queue::class);
- if ($this->autoFlush) {
- Yii::getLogger()->flush(true);
- }
- }
- /**
- * @param cli\WorkerEvent $event
- * @since 2.0.2
- */
- public function workerStart(cli\WorkerEvent $event)
- {
- $title = 'Worker ' . $event->sender->getWorkerPid();
- Yii::info("$title is started.", Queue::class);
- Yii::beginProfile($title, Queue::class);
- if ($this->autoFlush) {
- Yii::getLogger()->flush(true);
- }
- }
- /**
- * @param cli\WorkerEvent $event
- * @since 2.0.2
- */
- public function workerStop(cli\WorkerEvent $event)
- {
- $title = 'Worker ' . $event->sender->getWorkerPid();
- Yii::endProfile($title, Queue::class);
- Yii::info("$title is stopped.", Queue::class);
- if ($this->autoFlush) {
- Yii::getLogger()->flush(true);
- }
- }
- /**
- * @param JobEvent $event
- * @return string
- * @since 2.0.2
- */
- protected function getJobTitle(JobEvent $event)
- {
- $name = $event->job instanceof JobInterface ? get_class($event->job) : 'unknown job';
- return "[$event->id] $name";
- }
- /**
- * @param ExecEvent $event
- * @return string
- * @since 2.0.2
- */
- protected function getExecTitle(ExecEvent $event)
- {
- $title = $this->getJobTitle($event);
- $extra = "attempt: $event->attempt";
- if ($pid = $event->sender->getWorkerPid()) {
- $extra .= ", PID: $pid";
- }
- return "$title ($extra)";
- }
- }
|