mirror of
https://github.com/langgenius/dify.git
synced 2026-03-25 05:01:18 -04:00
Made-with: Cursor # Conflicts: # .devcontainer/post_create_command.sh # api/commands.py # api/core/agent/cot_agent_runner.py # api/core/agent/fc_agent_runner.py # api/core/app/apps/workflow_app_runner.py # api/core/app/entities/queue_entities.py # api/core/app/entities/task_entities.py # api/core/workflow/workflow_entry.py # api/dify_graph/enums.py # api/dify_graph/graph/graph.py # api/dify_graph/graph_events/node.py # api/dify_graph/model_runtime/entities/message_entities.py # api/dify_graph/node_events/node.py # api/dify_graph/nodes/agent/agent_node.py # api/dify_graph/nodes/base/__init__.py # api/dify_graph/nodes/base/entities.py # api/dify_graph/nodes/base/node.py # api/dify_graph/nodes/llm/entities.py # api/dify_graph/nodes/llm/node.py # api/dify_graph/nodes/tool/tool_node.py # api/pyproject.toml # api/uv.lock # web/app/components/base/avatar/__tests__/index.spec.tsx # web/app/components/base/avatar/index.tsx # web/app/components/base/date-and-time-picker/time-picker/__tests__/index.spec.tsx # web/app/components/base/file-uploader/file-from-link-or-local/index.tsx # web/app/components/base/prompt-editor/index.tsx # web/app/components/datasets/metadata/edit-metadata-batch/modal.tsx # web/app/components/header/account-dropdown/index.spec.tsx # web/app/components/share/text-generation/index.tsx # web/app/components/workflow/block-selector/tool/action-item.tsx # web/app/components/workflow/block-selector/trigger-plugin/action-item.tsx # web/app/components/workflow/hooks/use-edges-interactions.ts # web/app/components/workflow/hooks/use-nodes-interactions.ts # web/app/components/workflow/index.tsx # web/app/components/workflow/nodes/_base/components/editor/code-editor/index.tsx # web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx # web/app/components/workflow/nodes/human-input/components/delivery-method/recipient/email-item.tsx # web/app/components/workflow/nodes/loop/use-interactions.ts # web/contract/router.ts # web/env.ts # web/eslint-suppressions.json # web/package.json # web/pnpm-lock.yaml
135 lines
4.9 KiB
Bash
Executable File
135 lines
4.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
# Set UTF-8 encoding to address potential encoding issues in containerized environments
|
|
# Use C.UTF-8 which is universally available in all containers
|
|
export LANG=${LANG:-C.UTF-8}
|
|
export LC_ALL=${LC_ALL:-C.UTF-8}
|
|
export PYTHONIOENCODING=${PYTHONIOENCODING:-utf-8}
|
|
|
|
if [[ "${MIGRATION_ENABLED}" == "true" ]]; then
|
|
echo "Running migrations"
|
|
flask upgrade-db
|
|
# Pure migration mode
|
|
if [[ "${MODE}" == "migration" ]]; then
|
|
echo "Migration completed, exiting normally"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
if [[ "${MODE}" == "worker" ]]; then
|
|
|
|
# Get the number of available CPU cores
|
|
if [ "${CELERY_AUTO_SCALE,,}" = "true" ]; then
|
|
# Set MAX_WORKERS to the number of available cores if not specified
|
|
AVAILABLE_CORES=$(nproc)
|
|
MAX_WORKERS=${CELERY_MAX_WORKERS:-$AVAILABLE_CORES}
|
|
MIN_WORKERS=${CELERY_MIN_WORKERS:-1}
|
|
CONCURRENCY_OPTION="--autoscale=${MAX_WORKERS},${MIN_WORKERS}"
|
|
else
|
|
CONCURRENCY_OPTION="-c ${CELERY_WORKER_AMOUNT:-1}"
|
|
fi
|
|
|
|
# Configure queues based on edition if not explicitly set
|
|
if [[ -z "${CELERY_QUEUES}" ]]; then
|
|
if [[ "${EDITION}" == "CLOUD" ]]; then
|
|
# Cloud edition: separate queues for dataset and trigger tasks
|
|
DEFAULT_QUEUES="api_token,dataset,dataset_summary,priority_dataset,priority_pipeline,pipeline,mail,ops_trace,app_deletion,plugin,workflow_storage,conversation,workflow_professional,workflow_team,workflow_sandbox,schedule_poller,schedule_executor,triggered_workflow_dispatcher,trigger_refresh_executor,retention,workflow_based_app_execution"
|
|
else
|
|
# Community edition (SELF_HOSTED): dataset, pipeline and workflow have separate queues
|
|
DEFAULT_QUEUES="api_token,dataset,dataset_summary,priority_dataset,priority_pipeline,pipeline,mail,ops_trace,app_deletion,plugin,workflow_storage,conversation,workflow,schedule_poller,schedule_executor,triggered_workflow_dispatcher,trigger_refresh_executor,retention,workflow_based_app_execution"
|
|
fi
|
|
else
|
|
DEFAULT_QUEUES="${CELERY_QUEUES}"
|
|
fi
|
|
|
|
# Support for Kubernetes deployment with specific queue workers
|
|
# Environment variables that can be set:
|
|
# - CELERY_WORKER_QUEUES: Comma-separated list of queues (overrides CELERY_QUEUES)
|
|
# - CELERY_WORKER_CONCURRENCY: Number of worker processes (overrides CELERY_WORKER_AMOUNT)
|
|
# - CELERY_WORKER_POOL: Pool implementation (overrides CELERY_WORKER_CLASS)
|
|
|
|
if [[ -n "${CELERY_WORKER_QUEUES}" ]]; then
|
|
DEFAULT_QUEUES="${CELERY_WORKER_QUEUES}"
|
|
echo "Using CELERY_WORKER_QUEUES: ${DEFAULT_QUEUES}"
|
|
fi
|
|
|
|
if [[ -n "${CELERY_WORKER_CONCURRENCY}" ]]; then
|
|
CONCURRENCY_OPTION="-c ${CELERY_WORKER_CONCURRENCY}"
|
|
echo "Using CELERY_WORKER_CONCURRENCY: ${CELERY_WORKER_CONCURRENCY}"
|
|
fi
|
|
|
|
WORKER_POOL="${CELERY_WORKER_POOL:-${CELERY_WORKER_CLASS:-gevent}}"
|
|
echo "Starting Celery worker with queues: ${DEFAULT_QUEUES}"
|
|
|
|
exec celery -A celery_entrypoint.celery worker -P ${WORKER_POOL} $CONCURRENCY_OPTION \
|
|
--max-tasks-per-child ${MAX_TASKS_PER_CHILD:-50} --loglevel ${LOG_LEVEL:-INFO} \
|
|
-Q ${DEFAULT_QUEUES} \
|
|
--prefetch-multiplier=${CELERY_PREFETCH_MULTIPLIER:-1}
|
|
|
|
elif [[ "${MODE}" == "beat" ]]; then
|
|
exec celery -A app.celery beat --loglevel ${LOG_LEVEL:-INFO}
|
|
|
|
elif [[ "${MODE}" == "job" ]]; then
|
|
# Job mode: Run a one-time Flask command and exit
|
|
# Pass Flask command and arguments via container args
|
|
# Example K8s usage:
|
|
# args:
|
|
# - create-tenant
|
|
# - --email
|
|
# - admin@example.com
|
|
#
|
|
# Example Docker usage:
|
|
# docker run -e MODE=job dify-api:latest create-tenant --email admin@example.com
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
echo "Error: No command specified for job mode."
|
|
echo ""
|
|
echo "Usage examples:"
|
|
echo " Kubernetes:"
|
|
echo " args: [create-tenant, --email, admin@example.com]"
|
|
echo ""
|
|
echo " Docker:"
|
|
echo " docker run -e MODE=job dify-api create-tenant --email admin@example.com"
|
|
echo ""
|
|
echo "Available commands:"
|
|
echo " create-tenant, reset-password, reset-email, upgrade-db,"
|
|
echo " vdb-migrate, install-plugins, and more..."
|
|
echo ""
|
|
echo "Run 'flask --help' to see all available commands."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Running Flask job command: flask $*"
|
|
|
|
# Temporarily disable exit on error to capture exit code
|
|
set +e
|
|
flask "$@"
|
|
JOB_EXIT_CODE=$?
|
|
set -e
|
|
|
|
if [[ ${JOB_EXIT_CODE} -eq 0 ]]; then
|
|
echo "Job completed successfully."
|
|
else
|
|
echo "Job failed with exit code ${JOB_EXIT_CODE}."
|
|
fi
|
|
|
|
exit ${JOB_EXIT_CODE}
|
|
|
|
else
|
|
if [[ "${DEBUG}" == "true" ]]; then
|
|
export HOST=${DIFY_BIND_ADDRESS:-0.0.0.0}
|
|
export PORT=${DIFY_PORT:-5001}
|
|
exec python -m app
|
|
else
|
|
exec gunicorn \
|
|
--bind "${DIFY_BIND_ADDRESS:-0.0.0.0}:${DIFY_PORT:-5001}" \
|
|
--workers ${SERVER_WORKER_AMOUNT:-1} \
|
|
--worker-class ${SERVER_WORKER_CLASS:-geventwebsocket.gunicorn.workers.GeventWebSocketWorker} \
|
|
--worker-connections ${SERVER_WORKER_CONNECTIONS:-10} \
|
|
--timeout ${GUNICORN_TIMEOUT:-200} \
|
|
app:socketio_app
|
|
fi
|
|
fi
|