Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
bd1dd5bb
Commit
bd1dd5bb
authored
May 10, 2023
by
Liu lu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
日志批处理
parent
43fdf66e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
32 deletions
+43
-32
LogTraceHandler.php
src/Handler/LogTrace/LogTraceHandler.php
+7
-3
LogTraceQueue.php
src/Handler/LogTrace/LogTraceQueue.php
+4
-0
RedisQueueBatchHandler.php
src/Handler/RedisQueueBatchHandler.php
+32
-29
No files found.
src/Handler/LogTrace/LogTraceHandler.php
View file @
bd1dd5bb
...
@@ -36,8 +36,6 @@ class LogTraceHandler
...
@@ -36,8 +36,6 @@ class LogTraceHandler
//流程3 抛出一个异常
//流程3 抛出一个异常
throw new Exception('test111');
throw new Exception('test111');
//流程执行完成标记结束
LogTraceHandler::markComplete();
}catch (\Throwable $exception){
}catch (\Throwable $exception){
//记录异常日志
//记录异常日志
...
@@ -51,13 +49,19 @@ class LogTraceHandler
...
@@ -51,13 +49,19 @@ class LogTraceHandler
public
static
function
createLogTrace
(
$source
,
$params
)
public
static
function
createLogTrace
(
$source
,
$params
)
{
{
if
(
!
Coroutine
::
inCoroutine
())
return
;
if
(
!
Coroutine
::
inCoroutine
())
return
;
$userName
=
''
;
try
{
$userName
=
\Meibuyu\Micro\Model\Auth
::
user
()[
'name'
];
}
catch
(
\Throwable
$exception
){
}
LogTrace
::
insertOrIgnore
([
LogTrace
::
insertOrIgnore
([
'request_id'
=>
self
::
getRequestId
(),
'request_id'
=>
self
::
getRequestId
(),
'origin_params'
=>
json_encode
(
$params
),
'origin_params'
=>
json_encode
(
$params
),
'source'
=>
$source
,
'source'
=>
$source
,
'created_at'
=>
now
(),
'created_at'
=>
now
(),
'process_info'
=>
''
'process_info'
=>
''
,
'user_name'
=>
$userName
]);
]);
}
}
...
...
src/Handler/LogTrace/LogTraceQueue.php
View file @
bd1dd5bb
...
@@ -38,6 +38,10 @@ class LogTraceQueue extends RedisQueueBatchHandler
...
@@ -38,6 +38,10 @@ class LogTraceQueue extends RedisQueueBatchHandler
UNIQUE KEY `request_id` (`request_id`)
UNIQUE KEY `request_id` (`request_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5950 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=5950 DEFAULT CHARSET=utf8mb4;
"
);
"
);
Db
::
update
(
"
ALTER TABLE `trace_logs`
ADD COLUMN `user_name` varchar(6) NOT NULL DEFAULT '' COMMENT '用户名' AFTER `process_info`;
"
);
}
}
/**
/**
...
...
src/Handler/RedisQueueBatchHandler.php
View file @
bd1dd5bb
...
@@ -20,7 +20,9 @@ abstract class RedisQueueBatchHandler
...
@@ -20,7 +20,9 @@ abstract class RedisQueueBatchHandler
const
MAX_RETRY_TIMES
=
3
;
const
MAX_RETRY_TIMES
=
3
;
const
BATCH_DEAL_NUM
=
36
;
// const BATCH_DEAL_NUM = 36;
protected
$batch_deal_num
=
8
;
const
ERROR_RETRY_CODE
=
9000
;
const
ERROR_RETRY_CODE
=
9000
;
...
@@ -44,13 +46,12 @@ abstract class RedisQueueBatchHandler
...
@@ -44,13 +46,12 @@ abstract class RedisQueueBatchHandler
}
}
/**
/**
* 将执行失败的数据重回队列
* 将执行失败的数据重回队列
尾部 不影响其他数据执行
* @param $arr
* @param $arr
*/
*/
protected
function
backToQueue
(
$arr
)
protected
function
backToQueue
(
$arr
)
{
{
array_unshift
(
$arr
,
$this
->
queue_name
);
$this
->
redis
->
rPush
(
$this
->
queue_name
,
...
$arr
);
call_user_func_array
([
$this
->
redis
,
'lPush'
],
$arr
);
}
}
//批处理具体逻辑
//批处理具体逻辑
...
@@ -70,40 +71,42 @@ abstract class RedisQueueBatchHandler
...
@@ -70,40 +71,42 @@ abstract class RedisQueueBatchHandler
$this
->
redis
->
lPush
(
$this
->
queue_name
,
$exist
[
1
]);
$this
->
redis
->
lPush
(
$this
->
queue_name
,
$exist
[
1
]);
//每次从列表取100
//每次从列表取100
$arr
=
$this
->
redis
->
lRange
(
$this
->
queue_name
,
0
,
self
::
BATCH_DEAL_NUM
-
1
);
$arr
=
$this
->
redis
->
lRange
(
$this
->
queue_name
,
0
,
$this
->
batch_deal_num
-
1
);
//数据格式化
//数据格式化
$formatArr
=
array_map
(
function
(
$item
){
$formatArr
=
array_map
(
function
(
$item
){
return
json_decode
(
$item
,
true
);
return
json_decode
(
$item
,
true
);
},
$arr
);
},
$arr
);
try
{
//具体批处理逻辑
$this
->
batchDeal
(
$formatArr
);
//取完 从队列删掉
//取完 从队列删掉
$this
->
redis
->
lTrim
(
$this
->
queue_name
,
count
(
$arr
),
-
1
);
$this
->
redis
->
lTrim
(
$this
->
queue_name
,
count
(
$arr
),
-
1
);
}
catch
(
\Throwable
$exception
){
//错误码为100 重新推到队列
if
(
!
$this
->
tryTreeTimesBatchDeal
(
$formatArr
)){
if
(
$exception
->
getCode
()
==
self
::
ERROR_RETRY_CODE
){
$this
->
backToQueue
(
$arr
);
if
(
$this
->
retry
<
self
::
MAX_RETRY_TIMES
){
//重试次数不超过3次
//$this->backToQueue($arr);
$this
->
retry
++
;
}
else
{
$this
->
redis
->
lTrim
(
$this
->
queue_name
,
count
(
$arr
),
-
1
);
$this
->
retry
=
0
;
//重置当前次数
$this
->
errorWriteToFile
(
$formatArr
,
$exception
);
}
}
}
else
{
$this
->
redis
->
lTrim
(
$this
->
queue_name
,
count
(
$arr
),
-
1
);
$this
->
retry
=
0
;
$this
->
errorWriteToFile
(
$formatArr
,
$exception
);
}
}
}
}
/**
* 重试三次批处理 每次延迟6秒执行
* @param $formatArr
* @param int $retry
* @author Liu lu
* date 2023-05-10
*/
private
function
tryTreeTimesBatchDeal
(
$formatArr
,
$retry
=
0
)
{
if
(
$retry
>=
3
)
return
false
;
try
{
//具体批处理逻辑
$this
->
batchDeal
(
$formatArr
);
return
true
;
}
catch
(
\Throwable
$exception
){
$retry
++
;
sleep
(
6
);
$this
->
errorWriteToFile
(
$formatArr
,
$exception
);
return
$this
->
tryTreeTimesBatchDeal
(
$formatArr
,
$retry
);
}
}
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment