1
0
forked from erp-dev/erp

feat: task for auto upload plate_image to tencent each day 03:00

This commit is contained in:
2026-01-16 20:22:35 +08:00
parent ea28d7a4cf
commit a39b9648a4
7 changed files with 311 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ SDK reference: https://github.com/TencentCloud/tencentcloud-sdk-python
"""
import json
import time
import uuid
from urllib.parse import urlparse
@@ -24,6 +25,27 @@ def _truncate(s: str, max_len: int) -> str:
return s[:max_len]
class SimpleRateLimiter:
"""
Very small in-process rate limiter (token-bucket-ish) to keep API calls under QPS.
This is used by the scheduled task to respect TencentCloud API limits (e.g. 10 req/s).
"""
def __init__(self, *, qps: float):
if qps <= 0:
raise ValueError('qps 必须 > 0')
self._min_interval = 1.0 / float(qps)
self._last_ts = 0.0
def wait(self) -> None:
now = time.monotonic()
elapsed = now - self._last_ts
if self._last_ts > 0 and elapsed < self._min_interval:
time.sleep(self._min_interval - elapsed)
self._last_ts = time.monotonic()
def upload_image_url_to_tencent_tiia(*, image_url: str, entity_id: str) -> dict:
"""
Upload an image by URL to Tencent Cloud TIIA gallery using CreateImage.
@@ -157,7 +179,7 @@ def _extract_urls_from_plate_image(plate_image) -> list[str]:
return deduped
def upload_plate_order_images_to_tencent_tiia(*, plate_order_id: int) -> list[dict]:
def upload_plate_order_images_to_tencent_tiia(*, plate_order_id: int, rate_limiter: SimpleRateLimiter | None = None) -> list[dict]:
"""
包装函数:给定 plate_order_id读取 PlateOrder.plate_image(JSONField) 中所有图片并上传到腾讯云图库。
@@ -205,6 +227,8 @@ def upload_plate_order_images_to_tencent_tiia(*, plate_order_id: int) -> list[di
})
continue
try:
if rate_limiter is not None:
rate_limiter.wait()
resp = upload_image_url_to_tencent_tiia(image_url=url_str, entity_id=entity_id)
results.append({'image_url': url_str, 'ok': True, 'response': resp, 'error': None})
except Exception as e:
@@ -212,5 +236,9 @@ def upload_plate_order_images_to_tencent_tiia(*, plate_order_id: int) -> list[di
return results
__all__ = ['upload_image_url_to_tencent_tiia', 'upload_plate_order_images_to_tencent_tiia']
__all__ = [
'SimpleRateLimiter',
'upload_image_url_to_tencent_tiia',
'upload_plate_order_images_to_tencent_tiia',
]