Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
coco
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
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
ops
coco
Commits
30713105
Commit
30713105
authored
Feb 20, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改录像策略
parent
0508afc4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
47 deletions
+50
-47
app.py
coco/app.py
+21
-35
conf.py
coco/conf.py
+2
-1
recorder.py
coco/recorder.py
+19
-11
utils.py
coco/utils.py
+8
-0
No files found.
coco/app.py
View file @
30713105
...
...
@@ -84,7 +84,8 @@ class Coco:
self
.
keep_load_extra_conf
()
self
.
keep_heartbeat
()
self
.
monitor_sessions
()
self
.
monitor_sessions_replay
()
if
config
.
UPLOAD_FAILED_REPLAY_ON_START
:
self
.
upload_failed_replay
()
# @ignore_error
def
heartbeat
(
self
):
...
...
@@ -132,24 +133,18 @@ class Coco:
thread
=
threading
.
Thread
(
target
=
func
)
thread
.
start
()
def
monitor_sessions_replay
(
self
):
interval
=
60
*
60
*
5
@staticmethod
def
upload_failed_replay
():
replay_dir
=
os
.
path
.
join
(
config
.
REPLAY_DIR
)
max_try
=
5
upload_failed
=
defaultdict
(
int
)
def
retry_upload_replay
(
session_id
,
f
ull
_path
,
target
):
def
retry_upload_replay
(
session_id
,
f
ile_gz
_path
,
target
):
recorder
=
get_replay_recorder
()
recorder
.
file_
path
=
full
_path
recorder
.
file_
gz_path
=
file_gz
_path
recorder
.
session_id
=
session_id
recorder
.
target
=
target
ok
,
msg
=
recorder
.
upload_replay
()
if
ok
:
upload_failed
.
pop
(
session_id
,
None
)
else
:
upload_failed
[
session_id
]
+=
1
recorder
.
upload_replay
()
def
check_replay_need_upload
(
full_path
):
def
check_replay_
is_
need_upload
(
full_path
):
filename
=
os
.
path
.
basename
(
full_path
)
suffix
=
filename
.
split
(
'.'
)[
-
1
]
if
suffix
!=
'gz'
:
...
...
@@ -157,30 +152,21 @@ class Coco:
session_id
=
filename
.
split
(
'.'
)[
0
]
if
len
(
session_id
)
!=
36
:
return
False
stat
=
os
.
stat
(
full_path
)
if
stat
.
st_mtime
>
time
.
time
()
-
24
*
60
*
60
:
return
False
return
True
def
func
():
while
not
self
.
stop_evt
.
is_set
():
for
d
in
os
.
listdir
(
replay_dir
):
date_path
=
os
.
path
.
join
(
replay_dir
,
d
)
for
filename
in
os
.
listdir
(
date_path
):
full_path
=
os
.
path
.
join
(
date_path
,
filename
)
session_id
=
filename
.
split
(
'.'
)[
0
]
# 是否是一天前的,因为现在多个coco共享了日志目录,
# 不能单纯判断session是否关闭
if
not
check_replay_need_upload
(
full_path
):
continue
# 失败次数过多
if
session_id
in
upload_failed
\
and
upload_failed
[
session_id
]
>=
max_try
:
continue
target
=
os
.
path
.
join
(
d
,
filename
)
retry_upload_replay
(
session_id
,
full_path
,
target
)
time
.
sleep
(
1
)
time
.
sleep
(
interval
)
for
d
in
os
.
listdir
(
replay_dir
):
date_path
=
os
.
path
.
join
(
replay_dir
,
d
)
for
filename
in
os
.
listdir
(
date_path
):
full_path
=
os
.
path
.
join
(
date_path
,
filename
)
session_id
=
filename
.
split
(
'.'
)[
0
]
# 检查是否需要上传
if
not
check_replay_is_need_upload
(
full_path
):
continue
logger
.
debug
(
"Retry upload retain replay: {}"
.
format
(
filename
))
target
=
os
.
path
.
join
(
d
,
filename
)
retry_upload_replay
(
session_id
,
full_path
,
target
)
time
.
sleep
(
1
)
thread
=
threading
.
Thread
(
target
=
func
)
thread
.
start
()
...
...
coco/conf.py
View file @
30713105
...
...
@@ -352,7 +352,8 @@ defaults = {
'SECURITY_MAX_IDLE_TIME'
:
60
,
'ASSET_LIST_PAGE_SIZE'
:
'auto'
,
'SFTP_ROOT'
:
'/tmp'
,
'SFTP_SHOW_HIDDEN_FILE'
:
False
'SFTP_SHOW_HIDDEN_FILE'
:
False
,
'UPLOAD_FAILED_REPLAY_ON_START'
:
True
}
...
...
coco/recorder.py
View file @
30713105
...
...
@@ -6,14 +6,13 @@ import threading
import
datetime
import
time
import
os
import
gzip
import
json
from
copy
import
deepcopy
import
jms_storage
from
.conf
import
config
from
.utils
import
get_logger
from
.utils
import
get_logger
,
gzip_file
from
.struct
import
MemoryQueue
from
.service
import
app_service
...
...
@@ -29,6 +28,8 @@ class ReplayRecorder(object):
filename
=
None
file
=
None
file_path
=
None
filename_gz
=
None
file_gz_path
=
None
def
__init__
(
self
):
self
.
get_storage
()
...
...
@@ -56,19 +57,26 @@ class ReplayRecorder(object):
def
session_start
(
self
,
session_id
):
self
.
time_start
=
time
.
time
()
self
.
session_id
=
session_id
self
.
filename
=
session_id
+
'.replay.gz'
self
.
filename
=
session_id
self
.
filename_gz
=
session_id
+
'.replay.gz'
date
=
datetime
.
datetime
.
utcnow
()
.
strftime
(
'
%
Y-
%
m-
%
d'
)
self
.
target
=
date
+
'/'
+
self
.
filename
replay_dir
=
os
.
path
.
join
(
config
.
REPLAY_DIR
,
date
)
if
not
os
.
path
.
isdir
(
replay_dir
):
os
.
makedirs
(
replay_dir
,
exist_ok
=
True
)
# 录像记录路径
self
.
file_path
=
os
.
path
.
join
(
replay_dir
,
self
.
filename
)
self
.
file
=
gzip
.
open
(
self
.
file_path
,
'at'
)
# 录像压缩到的路径
self
.
file_gz_path
=
os
.
path
.
join
(
replay_dir
,
self
.
filename_gz
)
# 录像上传上去的路径
self
.
target
=
date
+
'/'
+
self
.
filename_gz
self
.
file
=
open
(
self
.
file_path
,
'at'
)
self
.
file
.
write
(
'{'
)
def
session_end
(
self
,
session_id
):
self
.
file
.
write
(
'"0":""}'
)
self
.
file
.
close
()
gzip_file
(
self
.
file_path
,
self
.
file_gz_path
)
self
.
upload_replay_some_times
()
def
upload_replay_some_times
(
self
,
times
=
3
):
...
...
@@ -95,15 +103,15 @@ class ReplayRecorder(object):
def
upload_replay
(
self
):
# 如果文件为空就直接删除
if
not
os
.
path
.
isfile
(
self
.
file_path
):
return
False
,
'Not found the file: {}'
.
format
(
self
.
file_path
)
if
os
.
path
.
getsize
(
self
.
file_path
)
==
0
:
os
.
unlink
(
self
.
file_path
)
if
not
os
.
path
.
isfile
(
self
.
file_
gz_
path
):
return
False
,
'Not found the file: {}'
.
format
(
self
.
file_
gz_
path
)
if
os
.
path
.
getsize
(
self
.
file_
gz_
path
)
==
0
:
os
.
unlink
(
self
.
file_
gz_
path
)
return
True
,
''
ok
,
msg
=
self
.
storage
.
upload
(
self
.
file_path
,
self
.
target
)
ok
,
msg
=
self
.
storage
.
upload
(
self
.
file_
gz_
path
,
self
.
target
)
if
ok
:
self
.
finish_replay
(
3
,
self
.
session_id
)
os
.
unlink
(
self
.
file_path
)
os
.
unlink
(
self
.
file_
gz_
path
)
return
ok
,
msg
def
finish_replay
(
self
,
times
,
session_id
):
...
...
coco/utils.py
View file @
30713105
...
...
@@ -8,6 +8,7 @@ import logging
import
re
import
os
import
gettext
import
gzip
from
io
import
StringIO
from
binascii
import
hexlify
from
werkzeug.local
import
Local
,
LocalProxy
...
...
@@ -464,4 +465,11 @@ def ignore_error(func):
return
wrapper
def
gzip_file
(
src_path
,
dst_path
,
unlink_ori
=
True
):
with
open
(
src_path
,
'rt'
)
as
src
,
gzip
.
open
(
dst_path
,
'at'
)
as
dst
:
dst
.
writelines
(
src
)
if
unlink_ori
:
os
.
unlink
(
src_path
)
ugettext
=
LocalProxy
(
partial
(
_find
,
'LANGUAGE_CODE'
))
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