From 0d9fc99417bdac13cd0fdfc31aea5d9e81ed3bbf Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Fri, 8 Nov 2024 09:34:11 +0800 Subject: [PATCH] chore: use DEBUG in dify_config instead of parsing raw system environment variable in place (#10437) --- api/app.py | 2 +- api/app_factory.py | 4 +++- api/core/app/apps/advanced_chat/app_generator.py | 4 ++-- api/core/app/apps/agent_chat/app_generator.py | 4 ++-- api/core/app/apps/chat/app_generator.py | 4 ++-- api/core/app/apps/completion/app_generator.py | 4 ++-- api/core/app/apps/workflow/app_generator.py | 4 ++-- api/core/model_manager.py | 4 ++-- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/api/app.py b/api/app.py index ed214bde976bf..93774bd75dfa0 100644 --- a/api/app.py +++ b/api/app.py @@ -2,7 +2,7 @@ from configs import dify_config -if os.environ.get("DEBUG", "false").lower() != "true": +if dify_config.DEBUG: from gevent import monkey monkey.patch_all() diff --git a/api/app_factory.py b/api/app_factory.py index aba78ccab8539..f53cf14f4b578 100644 --- a/api/app_factory.py +++ b/api/app_factory.py @@ -1,6 +1,8 @@ import os -if os.environ.get("DEBUG", "false").lower() != "true": +from configs import dify_config + +if dify_config.DEBUG: from gevent import monkey monkey.patch_all() diff --git a/api/core/app/apps/advanced_chat/app_generator.py b/api/core/app/apps/advanced_chat/app_generator.py index 39ab87c9142b0..3010f8a03f356 100644 --- a/api/core/app/apps/advanced_chat/app_generator.py +++ b/api/core/app/apps/advanced_chat/app_generator.py @@ -1,6 +1,5 @@ import contextvars import logging -import os import threading import uuid from collections.abc import Generator @@ -10,6 +9,7 @@ from pydantic import ValidationError import contexts +from configs import dify_config from constants import UUID_NIL from core.app.app_config.features.file_upload.manager import FileUploadConfigManager from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager @@ -317,7 +317,7 @@ def _generate_worker( logger.exception("Validation Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except (ValueError, InvokeError) as e: - if os.environ.get("DEBUG", "false").lower() == "true": + if dify_config.DEBUG: logger.exception("Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except Exception as e: diff --git a/api/core/app/apps/agent_chat/app_generator.py b/api/core/app/apps/agent_chat/app_generator.py index de12f5a441d7d..73d433d94d57f 100644 --- a/api/core/app/apps/agent_chat/app_generator.py +++ b/api/core/app/apps/agent_chat/app_generator.py @@ -1,5 +1,4 @@ import logging -import os import threading import uuid from collections.abc import Generator @@ -8,6 +7,7 @@ from flask import Flask, current_app from pydantic import ValidationError +from configs import dify_config from constants import UUID_NIL from core.app.app_config.easy_ui_based_app.model_config.converter import ModelConfigConverter from core.app.app_config.features.file_upload.manager import FileUploadConfigManager @@ -230,7 +230,7 @@ def _generate_worker( logger.exception("Validation Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except (ValueError, InvokeError) as e: - if os.environ.get("DEBUG") and os.environ.get("DEBUG").lower() == "true": + if dify_config.DEBUG: logger.exception("Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except Exception as e: diff --git a/api/core/app/apps/chat/app_generator.py b/api/core/app/apps/chat/app_generator.py index 5c074f5306e4c..d0ba90cc5e3a4 100644 --- a/api/core/app/apps/chat/app_generator.py +++ b/api/core/app/apps/chat/app_generator.py @@ -1,5 +1,4 @@ import logging -import os import threading import uuid from collections.abc import Generator @@ -8,6 +7,7 @@ from flask import Flask, current_app from pydantic import ValidationError +from configs import dify_config from constants import UUID_NIL from core.app.app_config.easy_ui_based_app.model_config.converter import ModelConfigConverter from core.app.app_config.features.file_upload.manager import FileUploadConfigManager @@ -227,7 +227,7 @@ def _generate_worker( logger.exception("Validation Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except (ValueError, InvokeError) as e: - if os.environ.get("DEBUG") and os.environ.get("DEBUG").lower() == "true": + if dify_config.DEBUG: logger.exception("Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except Exception as e: diff --git a/api/core/app/apps/completion/app_generator.py b/api/core/app/apps/completion/app_generator.py index 46450d39c0d6e..3bb05d05d8bb2 100644 --- a/api/core/app/apps/completion/app_generator.py +++ b/api/core/app/apps/completion/app_generator.py @@ -1,5 +1,4 @@ import logging -import os import threading import uuid from collections.abc import Generator @@ -8,6 +7,7 @@ from flask import Flask, current_app from pydantic import ValidationError +from configs import dify_config from core.app.app_config.easy_ui_based_app.model_config.converter import ModelConfigConverter from core.app.app_config.features.file_upload.manager import FileUploadConfigManager from core.app.apps.base_app_queue_manager import AppQueueManager, GenerateTaskStoppedError, PublishFrom @@ -203,7 +203,7 @@ def _generate_worker( logger.exception("Validation Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except (ValueError, InvokeError) as e: - if os.environ.get("DEBUG") and os.environ.get("DEBUG").lower() == "true": + if dify_config.DEBUG: logger.exception("Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except Exception as e: diff --git a/api/core/app/apps/workflow/app_generator.py b/api/core/app/apps/workflow/app_generator.py index a865c8a68b3aa..6e9c6804f9c79 100644 --- a/api/core/app/apps/workflow/app_generator.py +++ b/api/core/app/apps/workflow/app_generator.py @@ -1,6 +1,5 @@ import contextvars import logging -import os import threading import uuid from collections.abc import Generator, Mapping, Sequence @@ -10,6 +9,7 @@ from pydantic import ValidationError import contexts +from configs import dify_config from core.app.app_config.features.file_upload.manager import FileUploadConfigManager from core.app.apps.base_app_generator import BaseAppGenerator from core.app.apps.base_app_queue_manager import AppQueueManager, GenerateTaskStoppedError, PublishFrom @@ -261,7 +261,7 @@ def _generate_worker( logger.exception("Validation Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except (ValueError, InvokeError) as e: - if os.environ.get("DEBUG") and os.environ.get("DEBUG", "false").lower() == "true": + if dify_config.DEBUG: logger.exception("Error when generating") queue_manager.publish_error(e, PublishFrom.APPLICATION_MANAGER) except Exception as e: diff --git a/api/core/model_manager.py b/api/core/model_manager.py index e21449ec24a9e..059ba6c3d1f26 100644 --- a/api/core/model_manager.py +++ b/api/core/model_manager.py @@ -1,8 +1,8 @@ import logging -import os from collections.abc import Callable, Generator, Iterable, Sequence from typing import IO, Any, Optional, Union, cast +from configs import dify_config from core.entities.embedding_type import EmbeddingInputType from core.entities.provider_configuration import ProviderConfiguration, ProviderModelBundle from core.entities.provider_entities import ModelLoadBalancingConfiguration @@ -473,7 +473,7 @@ def fetch_next(self) -> Optional[ModelLoadBalancingConfiguration]: continue - if bool(os.environ.get("DEBUG", "False").lower() == "true"): + if dify_config.DEBUG: logger.info( f"Model LB\nid: {config.id}\nname:{config.name}\n" f"tenant_id: {self._tenant_id}\nprovider: {self._provider}\n"