Jenkins部署hxopx-kb-worker,并修改环境变量值
This commit is contained in:
1 parent
24f99070a3
commit
a9753fd10f
10 files changed
+345
-30
No files matched your search
@@ -180,6 +180,7 @@ class TestingConfig(Config):
|
||||
config = {
|
||||
'development': DevelopmentConfig,
|
||||
'test': TestingConfig,
|
||||
'testing': TestingConfig, # alias for create_app('testing') in pytest
|
||||
'production': ProductionConfig,
|
||||
'default': DevelopmentConfig,
|
||||
}
|
||||
@@ -35,3 +35,4 @@ lxml==6.0.2
|
||||
typer==0.17.4
|
||||
croniter
|
||||
pyyaml
|
||||
minio>=7.2.0
|
||||
+40
-10
@@ -4,43 +4,73 @@ backend/worker_kb.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
from app import create_app
|
||||
from app.services.knowledge_cleaning.queue_service import process_next_clean_job
|
||||
|
||||
_LOG_FORMAT = '%(asctime)s %(levelname)s [kb-worker] %(message)s'
|
||||
_LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
|
||||
def _configure_logging() -> logging.Logger:
|
||||
logger = logging.getLogger('kb-worker')
|
||||
if logger.handlers:
|
||||
return logger
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(logging.Formatter(_LOG_FORMAT, datefmt=_LOG_DATEFMT))
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.propagate = False
|
||||
return logger
|
||||
|
||||
|
||||
def _resolve_config_name() -> str:
|
||||
"""Keep the worker aligned with container/runtime environment selection."""
|
||||
explicit = os.getenv('APP_CONFIG')
|
||||
if explicit:
|
||||
return explicit.strip().lower()
|
||||
"""Keep the worker aligned with container/runtime environment selection.
|
||||
|
||||
flask_env = os.getenv('FLASK_ENV', 'production').strip().lower()
|
||||
Note: ``FLASK_ENV=test`` / ``ENV_NAME=test`` mean the *test deployment* host
|
||||
(``.env.test``), not pytest. Use ``production`` so DATABASE_URL from env_file
|
||||
is honored. ``testing`` is only for in-memory unit tests (``TestingConfig``).
|
||||
"""
|
||||
explicit = (os.getenv('APP_CONFIG') or '').strip().lower()
|
||||
if explicit:
|
||||
if explicit in ('testing', 'test'):
|
||||
# Allow explicit pytest-style config only when APP_CONFIG is set;
|
||||
# never infer this from FLASK_ENV=test on deploy hosts.
|
||||
return 'test'
|
||||
return explicit
|
||||
|
||||
env_name = (os.getenv('ENV_NAME') or os.getenv('FLASK_ENV') or 'production').strip().lower()
|
||||
mapping = {
|
||||
'dev': 'development',
|
||||
'development': 'development',
|
||||
'test': 'testing',
|
||||
'testing': 'testing',
|
||||
# Deploy env files: test/prod hosts use real DB via ProductionConfig
|
||||
'test': 'production',
|
||||
'env': 'production',
|
||||
'prod': 'production',
|
||||
'production': 'production',
|
||||
# Pytest-only aliases (not used on deploy unless APP_CONFIG forces it)
|
||||
'testing': 'test',
|
||||
}
|
||||
return mapping.get(flask_env, 'production')
|
||||
return mapping.get(env_name, 'production')
|
||||
|
||||
|
||||
def main():
|
||||
log = _configure_logging()
|
||||
app = create_app(_resolve_config_name())
|
||||
poll_seconds = float(app.config.get('KB_WORKER_POLL_SECONDS', 2))
|
||||
|
||||
with app.app_context():
|
||||
print(f'[kb-worker] started, poll_seconds={poll_seconds}')
|
||||
log.info('started, poll_seconds=%s', poll_seconds)
|
||||
while True:
|
||||
processed, message = process_next_clean_job()
|
||||
if not processed:
|
||||
time.sleep(poll_seconds)
|
||||
continue
|
||||
print(f'[kb-worker] processed job: {message}')
|
||||
log.info('processed job: %s', message)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user