mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 10:28:10 +08:00
[autofix.ci] apply automated fixes
This commit is contained in:
@ -358,18 +358,18 @@ def validate_and_get_api_token(scope: str | None = None):
|
|||||||
def _async_update_token_last_used_at(auth_token: str, scope: str | None):
|
def _async_update_token_last_used_at(auth_token: str, scope: str | None):
|
||||||
"""
|
"""
|
||||||
Asynchronously update the last_used_at timestamp for a token.
|
Asynchronously update the last_used_at timestamp for a token.
|
||||||
|
|
||||||
This schedules a Celery task to update the database without blocking
|
This schedules a Celery task to update the database without blocking
|
||||||
the current request. The start time is passed to ensure only older
|
the current request. The start time is passed to ensure only older
|
||||||
records are updated, providing natural concurrency control.
|
records are updated, providing natural concurrency control.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
from tasks.update_api_token_last_used_task import update_api_token_last_used_task
|
from tasks.update_api_token_last_used_task import update_api_token_last_used_task
|
||||||
|
|
||||||
# Record the request start time for concurrency control
|
# Record the request start time for concurrency control
|
||||||
start_time = naive_utc_now()
|
start_time = naive_utc_now()
|
||||||
start_time_iso = start_time.isoformat()
|
start_time_iso = start_time.isoformat()
|
||||||
|
|
||||||
# Fire and forget - don't wait for result
|
# Fire and forget - don't wait for result
|
||||||
update_api_token_last_used_task.delay(auth_token, scope, start_time_iso)
|
update_api_token_last_used_task.delay(auth_token, scope, start_time_iso)
|
||||||
logger.debug("Scheduled async update for last_used_at (scope: %s, start_time: %s)", scope, start_time_iso)
|
logger.debug("Scheduled async update for last_used_at (scope: %s, start_time: %s)", scope, start_time_iso)
|
||||||
|
|||||||
@ -17,7 +17,7 @@ logger = logging.getLogger(__name__)
|
|||||||
class CachedApiToken:
|
class CachedApiToken:
|
||||||
"""
|
"""
|
||||||
Simple data class to represent a cached API token.
|
Simple data class to represent a cached API token.
|
||||||
|
|
||||||
This is NOT a SQLAlchemy model instance, but a plain Python object
|
This is NOT a SQLAlchemy model instance, but a plain Python object
|
||||||
that mimics the ApiToken model interface for read-only access.
|
that mimics the ApiToken model interface for read-only access.
|
||||||
"""
|
"""
|
||||||
@ -110,7 +110,7 @@ class ApiTokenCache:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(cached_data)
|
data = json.loads(cached_data)
|
||||||
|
|
||||||
# Create a simple data object (NOT a SQLAlchemy model instance)
|
# Create a simple data object (NOT a SQLAlchemy model instance)
|
||||||
# This is safe because it's just a plain Python object with attributes
|
# This is safe because it's just a plain Python object with attributes
|
||||||
token_obj = CachedApiToken(
|
token_obj = CachedApiToken(
|
||||||
|
|||||||
@ -20,10 +20,10 @@ logger = logging.getLogger(__name__)
|
|||||||
def update_api_token_last_used_task(self, token: str, scope: str | None, start_time_iso: str):
|
def update_api_token_last_used_task(self, token: str, scope: str | None, start_time_iso: str):
|
||||||
"""
|
"""
|
||||||
Asynchronously update the last_used_at timestamp for an API token.
|
Asynchronously update the last_used_at timestamp for an API token.
|
||||||
|
|
||||||
Uses timestamp comparison to ensure only updates when last_used_at is older
|
Uses timestamp comparison to ensure only updates when last_used_at is older
|
||||||
than the request start time, providing natural concurrency control.
|
than the request start time, providing natural concurrency control.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token: The API token string
|
token: The API token string
|
||||||
scope: The token type/scope (e.g., 'app', 'dataset')
|
scope: The token type/scope (e.g., 'app', 'dataset')
|
||||||
@ -31,10 +31,10 @@ def update_api_token_last_used_task(self, token: str, scope: str | None, start_t
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Parse start_time from ISO format
|
# Parse start_time from ISO format
|
||||||
start_time = datetime.fromisoformat(start_time_iso)
|
start_time = datetime.fromisoformat(start_time_iso)
|
||||||
# Update database
|
# Update database
|
||||||
current_time = naive_utc_now()
|
current_time = naive_utc_now()
|
||||||
|
|
||||||
with Session(db.engine, expire_on_commit=False) as session:
|
with Session(db.engine, expire_on_commit=False) as session:
|
||||||
update_stmt = (
|
update_stmt = (
|
||||||
update(ApiToken)
|
update(ApiToken)
|
||||||
@ -46,7 +46,7 @@ def update_api_token_last_used_task(self, token: str, scope: str | None, start_t
|
|||||||
.values(last_used_at=current_time)
|
.values(last_used_at=current_time)
|
||||||
)
|
)
|
||||||
result = session.execute(update_stmt)
|
result = session.execute(update_stmt)
|
||||||
|
|
||||||
if hasattr(result, "rowcount") and result.rowcount > 0:
|
if hasattr(result, "rowcount") and result.rowcount > 0:
|
||||||
session.commit()
|
session.commit()
|
||||||
logger.info("Updated last_used_at for token (async): %s... (scope: %s)", token[:10], scope)
|
logger.info("Updated last_used_at for token (async): %s... (scope: %s)", token[:10], scope)
|
||||||
@ -54,7 +54,7 @@ def update_api_token_last_used_task(self, token: str, scope: str | None, start_t
|
|||||||
else:
|
else:
|
||||||
logger.debug("No update needed for token: %s... (already up-to-date)", token[:10])
|
logger.debug("No update needed for token: %s... (already up-to-date)", token[:10])
|
||||||
return {"status": "no_update_needed", "reason": "last_used_at >= start_time"}
|
return {"status": "no_update_needed", "reason": "last_used_at >= start_time"}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Failed to update last_used_at for token (async): %s", e)
|
logger.warning("Failed to update last_used_at for token (async): %s", e)
|
||||||
# Don't retry on failure to avoid blocking the queue
|
# Don't retry on failure to avoid blocking the queue
|
||||||
|
|||||||
Reference in New Issue
Block a user