@@ -14,28 +14,34 @@ class AsyncLogSubject
14
14
* 存储观察者
15
15
* @var array
16
16
*/
17
- private $ _observers = array ();
17
+ private $ observers = array ();
18
18
19
19
/**
20
20
* 日志格式
21
21
* @var string
22
22
*/
23
- protected $ _log_path ;
23
+ protected $ log_path ;
24
24
25
25
/**
26
26
* 日志处理信息
27
27
* @var array
28
28
*/
29
29
protected $ log_deal ;
30
30
31
+ /**
32
+ * 出现异常的回调函数
33
+ * @var callback
34
+ */
35
+ protected $ exception_callback = null ;
36
+
31
37
/**
32
38
* AsyncLogSubject constructor.
33
39
* @param string $time
34
40
*/
35
41
public function __construct ($ time = 'today ' )
36
42
{
37
43
// 1. 设置日志路径
38
- $ this ->_getLogPath ($ time );
44
+ $ this ->getLogPath ($ time );
39
45
40
46
// 2. 设置日志处理 变量的数据格式
41
47
$ this ->log_deal = [
@@ -58,7 +64,7 @@ public function __construct($time = 'today')
58
64
*/
59
65
public function addObserver (AsyncLogObserver $ observer )
60
66
{
61
- $ this ->_observers [] = $ observer ;
67
+ $ this ->observers [] = $ observer ;
62
68
}
63
69
64
70
/**
@@ -68,29 +74,30 @@ public function addObserver(AsyncLogObserver $observer)
68
74
public function handle ()
69
75
{
70
76
// 1. 验证文件是否存在
71
- if (!file_exists ($ this ->_log_path )) {
72
- Log::warning ('执行定时任务,读取日志文件来完成订阅与发布,找不到异步日志文件 ' , ['文件名: ' . $ this ->_log_path ]);
77
+ if (!file_exists ($ this ->log_path )) {
78
+ Log::warning ('执行定时任务,读取日志文件来完成订阅与发布,找不到异步日志文件 ' , ['文件名: ' . $ this ->log_path ]);
79
+ $ this ->exception ('执行定时任务,读取日志文件来完成订阅与发布,找不到异步日志文件,文件名: ' . $ this ->log_path );
73
80
exit ;
74
81
}
75
82
76
83
// 2. 读取日志文件
77
- $ fp = fopen ($ this ->_log_path , "r " );
84
+ $ fp = fopen ($ this ->log_path , "r " );
78
85
$ lineNum = 1 ;
79
86
while (!feof ($ fp )) {//循环读取,直至读取完整个文件
80
87
81
88
// 2.1 格式化数据结构
82
- $ this ->_handleBefore (fgets ($ fp , 1024 * 1024 * 1024 ), $ lineNum ++);
89
+ $ this ->handleBefore (fgets ($ fp , 1024 * 1024 * 1024 ), $ lineNum ++);
83
90
// 2.2 判断是否已经执行完毕
84
91
if (empty ($ this ->log_deal ['deal_contain ' ])) {
85
- $ this ->_dealSuccessContain ();
92
+ $ this ->dealSuccessContain ();
86
93
Log::notice ("执行定时任务,读取日志文件来完成订阅与发布,执行完毕!!! " );
87
94
break ;
88
95
}
89
- dump ('执行定时任务,读取日志文件来完成订阅与发布,正在执行第 ' . $ this ->log_deal ['deal_contain ' ]['line_num ' ]. '行... ' );
96
+ dump ('执行定时任务,读取日志文件来完成订阅与发布,正在执行第 ' . $ this ->log_deal ['deal_contain ' ]['line_num ' ] . '行... ' );
90
97
91
98
// 2.3 执行对应的message事物。
92
99
$ observerExists = $ observerResult = null ;
93
- foreach ($ this ->_observers as $ key => $ observer ) {
100
+ foreach ($ this ->observers as $ key => $ observer ) {
94
101
if (!empty ($ this ->log_deal ['deal_contain ' ]['async_message ' ]) && $ this ->log_deal ['deal_contain ' ]['async_message ' ] == $ observer ->message ) {
95
102
$ observerResult = $ observer ->onHandle ($ this ->log_deal ['deal_contain ' ]['async_context ' ]);
96
103
$ observerExists = true ;
@@ -99,35 +106,38 @@ public function handle()
99
106
100
107
if (empty ($ observerExists )) {
101
108
// 2.4 如果不存在这个观察者
102
- $ this ->_dealSuccessContain ();
109
+ $ this ->dealSuccessContain ();
103
110
Log::warning (
104
111
'执行定时任务,读取日志文件来完成订阅与发布,发现第 ' . $ this ->log_deal ['deal_contain ' ]['line_num ' ] . '行找不到与其对应的观察者 ' ,
105
112
[
106
113
'async的message: ' . $ this ->log_deal ['deal_contain ' ]['async_message ' ],
107
- '日志路径为: ' . $ this ->_log_path
114
+ '日志路径为: ' . $ this ->log_path
108
115
]);
116
+ $ this ->exception ($ this ->log_deal ['deal_contain ' ]);
109
117
} else if ($ observerResult === null ) {
110
118
// 2.5 如果对应的观察者没有返回值
111
- $ this ->_dealSuccessContain ();
119
+ $ this ->dealSuccessContain ();
112
120
Log::warning (
113
121
'执行定时任务,读取日志文件来完成订阅与发布,发现第 ' . $ this ->log_deal ['deal_contain ' ]['line_num ' ] . '行执行后没有返回值 ' ,
114
122
[
115
123
'async的message: ' . $ this ->log_deal ['deal_contain ' ]['async_message ' ],
116
- '日志路径为: ' . $ this ->_log_path
124
+ '日志路径为: ' . $ this ->log_path
117
125
]);
126
+ $ this ->exception ($ this ->log_deal ['deal_contain ' ]);
118
127
} else if ($ observerResult === false ) {
119
128
// 2.6 执行失败
120
- $ this ->_dealSuccessContain ();
129
+ $ this ->dealSuccessContain ();
121
130
Log::error (
122
131
'执行定时任务,读取日志文件来完成订阅与发布,发现第 ' . $ this ->log_deal ['deal_contain ' ]['line_num ' ] . '行执行失败 ' ,
123
132
[
124
133
'async的message: ' . $ this ->log_deal ['deal_contain ' ]['async_message ' ],
125
- '日志路径为: ' . $ this ->_log_path
134
+ '日志路径为: ' . $ this ->log_path
126
135
]);
136
+ $ this ->exception ($ this ->log_deal ['deal_contain ' ]);
127
137
} else if ($ observerResult === true ) {
128
138
// 2.7 执行成功
129
139
$ this ->log_deal ['success_contain ' ] = [
130
- 'start_line ' => $ this ->log_deal ['success_contain ' ]['start_line ' ]?: $ this ->log_deal ['deal_contain ' ]['line_num ' ],
140
+ 'start_line ' => $ this ->log_deal ['success_contain ' ]['start_line ' ] ?: $ this ->log_deal ['deal_contain ' ]['line_num ' ],
131
141
'end_line ' => $ this ->log_deal ['deal_contain ' ]['line_num ' ]
132
142
];
133
143
}
@@ -142,22 +152,43 @@ public function handle()
142
152
*/
143
153
public function removeObserver ($ observerName )
144
154
{
145
- foreach ($ this ->_observers as $ key => $ observer ) {
155
+ foreach ($ this ->observers as $ key => $ observer ) {
146
156
if ($ observer ->getName () == $ observerName ) {
147
- array_splice ($ this ->_observers , $ key , 1 );
157
+ array_splice ($ this ->observers , $ key , 1 );
148
158
return true ;
149
159
}
150
160
}
151
161
return false ;
152
162
}
153
163
164
+ /**
165
+ * 绑定异常监听函数
166
+ * @param $callback
167
+ * @Author jiaWen.chen
168
+ */
169
+ public function onException ($ callback )
170
+ {
171
+ $ this ->exception_callback = $ callback ;
172
+ }
173
+
174
+ /**
175
+ * 调用异常处理
176
+ * @param $message
177
+ * @Author jiaWen.chen
178
+ */
179
+ protected function exception ($ message )
180
+ {
181
+ if (!empty ($ this ->exception_callback ))
182
+ ($ this ->exception_callback )($ message );
183
+ }
184
+
154
185
/**
155
186
* 格式化日志数据结构
156
187
* @param $logStr
157
188
* @param $lineNum
158
189
* @Author jiaWen.chen
159
190
*/
160
- protected function _handleBefore ($ logStr , $ lineNum )
191
+ protected function handleBefore ($ logStr , $ lineNum )
161
192
{
162
193
// 1. 如果到最后一行,则清空deal_contain
163
194
if ($ logStr == false ) {
@@ -185,13 +216,13 @@ protected function _handleBefore($logStr, $lineNum)
185
216
* 对执行成功的日志进行合并输出
186
217
* @Author jiaWen.chen
187
218
*/
188
- protected function _dealSuccessContain ()
219
+ protected function dealSuccessContain ()
189
220
{
190
221
if (!empty ($ this ->log_deal ['success_contain ' ]['start_line ' ]) && !empty ($ this ->log_deal ['success_contain ' ]['end_line ' ])) {
191
222
if ($ this ->log_deal ['success_contain ' ]['start_line ' ] == $ this ->log_deal ['success_contain ' ]['end_line ' ]) {
192
- Log::notice ("执行定时任务,读取日志文件来完成订阅与发布,第 " . $ this ->log_deal ['success_contain ' ]['start_line ' ]. "行执行成功 " );
223
+ Log::notice ("执行定时任务,读取日志文件来完成订阅与发布,第 " . $ this ->log_deal ['success_contain ' ]['start_line ' ] . "行执行成功 " );
193
224
} else {
194
- Log::notice ("执行定时任务,读取日志文件来完成订阅与发布,第 " . $ this ->log_deal ['success_contain ' ]['start_line ' ]. "行至 " . $ this ->log_deal ['success_contain ' ]['end_line ' ]. "行执行成功 " );
225
+ Log::notice ("执行定时任务,读取日志文件来完成订阅与发布,第 " . $ this ->log_deal ['success_contain ' ]['start_line ' ] . "行至 " . $ this ->log_deal ['success_contain ' ]['end_line ' ] . "行执行成功 " );
195
226
}
196
227
}
197
228
$ this ->log_deal ['success_contain ' ] = [
@@ -205,9 +236,9 @@ protected function _dealSuccessContain()
205
236
* @param string $time
206
237
* @Author jiaWen.chen
207
238
*/
208
- protected function _getLogPath ($ time = 'today ' )
239
+ protected function getLogPath ($ time = 'today ' )
209
240
{
210
- $ this ->_log_path = storage_path () . '/logs/async/async- ' . date ('Y-m-d ' , strtotime ($ time )) . '.log ' ;
241
+ $ this ->log_path = storage_path () . '/logs/async/async- ' . date ('Y-m-d ' , strtotime ($ time )) . '.log ' ;
211
242
}
212
243
213
244
}
0 commit comments