Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
data_report
/
data-report
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 8e40284e
authored
Aug 11, 2026
by
周立明
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'main' of
http://192.168.3.233:8090/data_report/data-report
2 parents
e864dae8
41343504
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
13 deletions
data-process/oss_utils.py
data-process/processor.py
data-process/requirements.txt
data-process/scheduler.py
data-process/oss_utils.py
View file @
8e40284
...
...
@@ -9,7 +9,9 @@ import base64
import
hashlib
import
hmac
import
json
import
logging
import
os
import
threading
import
time
import
uuid
from
datetime
import
datetime
,
timedelta
,
timezone
...
...
@@ -18,6 +20,8 @@ import oss2
from
config
import
Config
logger
=
logging
.
getLogger
(
'oss_utils'
)
_BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
# 直传签名有效期(秒)
...
...
@@ -110,14 +114,20 @@ def get_object_key_from_url(file_url):
return
file_url
def
download_file
(
file_url
,
local_path
):
"""从 OSS 下载文件到本地路径。
def
download_file
(
file_url
,
local_path
,
timeout_seconds
=
60
):
"""从 OSS 下载文件到本地路径
,带超时保护
。
- 解析 file_url 获取 OSS object key
- 使用 oss2.Bucket.get_object_to_file() 下载
- 本地回退模式:如果 file_url 以 /uploads/ 开头,直接从本地 uploads 目录复制
- 目录不存在时自动创建
- 下载失败时抛出 Exception
- 超时后抛出 TimeoutError
"""
import
threading
import
time
as
time_module
download_start
=
time_module
.
time
()
# 本地回退模式:从本地 uploads 目录复制
if
file_url
.
startswith
(
'/uploads/'
):
source_path
=
os
.
path
.
join
(
Config
.
UPLOAD_FOLDER
,
file_url
[
len
(
'/uploads/'
):])
...
...
@@ -126,24 +136,45 @@ def download_file(file_url, local_path):
raise
Exception
(
f
"本地文件不存在: {source_path}"
)
import
shutil
shutil
.
copy2
(
source_path
,
local_path
)
elapsed
=
time_module
.
time
()
-
download_start
logger
.
info
(
f
"[OSS下载] 本地复制完成: {file_url}, 耗时: {elapsed:.2f}s"
)
return
if
not
Config
.
OSS_ENABLED
:
raise
Exception
(
"OSS 未启用,无法下载远程文件"
)
# 从 URL 中解析 object key
object_key
=
get_object_key_from_url
(
file_url
)
# 确保本地目录存在
os
.
makedirs
(
os
.
path
.
dirname
(
local_path
),
exist_ok
=
True
)
# 从 OSS 下载
auth
=
oss2
.
Auth
(
Config
.
OSS_ACCESS_KEY_ID
,
Config
.
OSS_ACCESS_KEY_SECRET
)
bucket
=
oss2
.
Bucket
(
auth
,
Config
.
OSS_ENDPOINT
,
Config
.
OSS_BUCKET
)
try
:
bucket
.
get_object_to_file
(
object_key
,
local_path
)
except
Exception
as
e
:
raise
Exception
(
f
"OSS 下载失败: {str(e)}"
)
logger
.
info
(
f
"[OSS下载] 开始下载: url={file_url[:80]}..., timeout={timeout_seconds}s"
)
# 使用线程实现超时控制
result
=
{
'error'
:
None
}
def
_do_download
():
try
:
auth
=
oss2
.
Auth
(
Config
.
OSS_ACCESS_KEY_ID
,
Config
.
OSS_ACCESS_KEY_SECRET
)
bucket
=
oss2
.
Bucket
(
auth
,
Config
.
OSS_ENDPOINT
,
Config
.
OSS_BUCKET
)
bucket
.
get_object_to_file
(
object_key
,
local_path
)
except
Exception
as
e
:
result
[
'error'
]
=
str
(
e
)
download_thread
=
threading
.
Thread
(
target
=
_do_download
,
daemon
=
True
)
download_thread
.
start
()
download_thread
.
join
(
timeout
=
timeout_seconds
)
if
download_thread
.
is_alive
():
elapsed
=
time_module
.
time
()
-
download_start
raise
TimeoutError
(
f
"下载超时: {file_url[:80]}, 超过 {timeout_seconds}s (实际耗时 {elapsed:.2f}s)"
)
if
result
[
'error'
]:
raise
Exception
(
f
"OSS 下载失败: {result['error']}"
)
elapsed
=
time_module
.
time
()
-
download_start
logger
.
info
(
f
"[OSS下载] 下载完成: url={file_url[:80]}..., 耗时: {elapsed:.2f}s"
)
def
upload_file
(
local_path
,
object_key
=
None
):
...
...
data-process/processor.py
View file @
8e40284
This diff is collapsed.
Click to expand it.
data-process/requirements.txt
View file @
8e40284
...
...
@@ -6,3 +6,6 @@ cryptography>=42.0
python-dotenv>=1.0
openpyxl>=3.1
requests>=2.31
pandas>=2.0
xlrd>=2.0.1
msoffcrypto-tool>=5.3
data-process/scheduler.py
View file @
8e40284
...
...
@@ -15,12 +15,25 @@
import
threading
import
logging
import
sys
import
traceback
from
datetime
import
datetime
from
config
import
Config
logger
=
logging
.
getLogger
(
'TaskScheduler'
)
# 注册线程异常钩子,捕获 daemon 线程中的未处理异常
def
_threading_excepthook
(
args
):
"""捕获线程中未处理的异常并记录完整堆栈。"""
error_msg
=
f
"线程异常: {args.exc_type.__name__}: {args.exc_value}
\n
"
error_msg
+=
''
.
join
(
traceback
.
format_exception
(
args
.
exc_type
,
args
.
exc_value
,
args
.
exc_traceback
))
logger
.
error
(
error_msg
)
sys
.
stderr
.
write
(
error_msg
)
sys
.
stderr
.
flush
()
threading
.
excepthook
=
_threading_excepthook
class
TaskScheduler
:
"""
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment