forked from yiisoft/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogBehavior.php
More file actions
159 lines (142 loc) · 3.83 KB
/
LogBehavior.php
File metadata and controls
159 lines (142 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
declare(strict_types=1);
namespace yii\queue;
use Yii;
use yii\base\Behavior;
use yii\base\Component;
/**
* Log Behavior.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class LogBehavior extends Behavior
{
/**
* @var Queue|null|Component
* @inheritdoc
*/
public $owner;
/**
* @var bool
*/
public bool $autoFlush = true;
/**
* @inheritdoc
*/
public function events(): array
{
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): void
{
$title = $this->getJobTitle($event);
Yii::info("$title is pushed.", Queue::class);
}
/**
* @param ExecEvent $event
*/
public function beforeExec(ExecEvent $event): void
{
$title = $this->getExecTitle($event);
Yii::info("$title is started.", Queue::class);
Yii::beginProfile($title, Queue::class);
}
/**
* @param ExecEvent $event
*/
public function afterExec(ExecEvent $event): void
{
$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): void
{
$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): void
{
$workerPid = $event->sender->getWorkerPid();
if (null === $workerPid) {
$workerPid = '{PID not found}';
}
$title = 'Worker ' . $workerPid;
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): void
{
$workerPid = $event->sender->getWorkerPid();
if (null === $workerPid) {
$workerPid = '{PID not found}';
}
$title = 'Worker ' . $workerPid;
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): string
{
$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): string
{
$title = $this->getJobTitle($event);
$extra = "attempt: $event->attempt";
$pid = $event->sender->getWorkerPid();
if (null !== $pid) {
$extra .= ", PID: $pid";
}
return "$title ($extra)";
}
}