LogBehavior.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\queue;
  8. use Yii;
  9. use yii\base\Behavior;
  10. /**
  11. * Log Behavior.
  12. *
  13. * @author Roman Zhuravlev <zhuravljov@gmail.com>
  14. */
  15. class LogBehavior extends Behavior
  16. {
  17. /**
  18. * @var Queue
  19. * @inheritdoc
  20. */
  21. public $owner;
  22. /**
  23. * @var bool
  24. */
  25. public $autoFlush = true;
  26. /**
  27. * @inheritdoc
  28. */
  29. public function events()
  30. {
  31. return [
  32. Queue::EVENT_AFTER_PUSH => 'afterPush',
  33. Queue::EVENT_BEFORE_EXEC => 'beforeExec',
  34. Queue::EVENT_AFTER_EXEC => 'afterExec',
  35. Queue::EVENT_AFTER_ERROR => 'afterError',
  36. cli\Queue::EVENT_WORKER_START => 'workerStart',
  37. cli\Queue::EVENT_WORKER_STOP => 'workerStop',
  38. ];
  39. }
  40. /**
  41. * @param PushEvent $event
  42. */
  43. public function afterPush(PushEvent $event)
  44. {
  45. $title = $this->getJobTitle($event);
  46. Yii::info("$title is pushed.", Queue::class);
  47. }
  48. /**
  49. * @param ExecEvent $event
  50. */
  51. public function beforeExec(ExecEvent $event)
  52. {
  53. $title = $this->getExecTitle($event);
  54. Yii::info("$title is started.", Queue::class);
  55. Yii::beginProfile($title, Queue::class);
  56. }
  57. /**
  58. * @param ExecEvent $event
  59. */
  60. public function afterExec(ExecEvent $event)
  61. {
  62. $title = $this->getExecTitle($event);
  63. Yii::endProfile($title, Queue::class);
  64. Yii::info("$title is finished.", Queue::class);
  65. if ($this->autoFlush) {
  66. Yii::getLogger()->flush(true);
  67. }
  68. }
  69. /**
  70. * @param ExecEvent $event
  71. */
  72. public function afterError(ExecEvent $event)
  73. {
  74. $title = $this->getExecTitle($event);
  75. Yii::endProfile($title, Queue::class);
  76. Yii::error("$title is finished with error: $event->error.", Queue::class);
  77. if ($this->autoFlush) {
  78. Yii::getLogger()->flush(true);
  79. }
  80. }
  81. /**
  82. * @param cli\WorkerEvent $event
  83. * @since 2.0.2
  84. */
  85. public function workerStart(cli\WorkerEvent $event)
  86. {
  87. $title = 'Worker ' . $event->sender->getWorkerPid();
  88. Yii::info("$title is started.", Queue::class);
  89. Yii::beginProfile($title, Queue::class);
  90. if ($this->autoFlush) {
  91. Yii::getLogger()->flush(true);
  92. }
  93. }
  94. /**
  95. * @param cli\WorkerEvent $event
  96. * @since 2.0.2
  97. */
  98. public function workerStop(cli\WorkerEvent $event)
  99. {
  100. $title = 'Worker ' . $event->sender->getWorkerPid();
  101. Yii::endProfile($title, Queue::class);
  102. Yii::info("$title is stopped.", Queue::class);
  103. if ($this->autoFlush) {
  104. Yii::getLogger()->flush(true);
  105. }
  106. }
  107. /**
  108. * @param JobEvent $event
  109. * @return string
  110. * @since 2.0.2
  111. */
  112. protected function getJobTitle(JobEvent $event)
  113. {
  114. $name = $event->job instanceof JobInterface ? get_class($event->job) : 'unknown job';
  115. return "[$event->id] $name";
  116. }
  117. /**
  118. * @param ExecEvent $event
  119. * @return string
  120. * @since 2.0.2
  121. */
  122. protected function getExecTitle(ExecEvent $event)
  123. {
  124. $title = $this->getJobTitle($event);
  125. $extra = "attempt: $event->attempt";
  126. if ($pid = $event->sender->getWorkerPid()) {
  127. $extra .= ", PID: $pid";
  128. }
  129. return "$title ($extra)";
  130. }
  131. }