File size: 8,652 Bytes
9ea1f4d b7aaf91 9ea1f4d b7aaf91 9ea1f4d 37af33a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | # Copyright (c) 2025 Stephen G. Pope
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from flask import Flask, request
from queue import Queue
from services.webhook import send_webhook
import threading
import uuid
import os
import time
from version import BUILD_NUMBER # Import the BUILD_NUMBER
from app_utils import log_job_status, discover_and_register_blueprints # Import the discover_and_register_blueprints function
MAX_QUEUE_LENGTH = int(os.environ.get('MAX_QUEUE_LENGTH', 0))
def create_app():
app = Flask(__name__)
@app.route("/")
def home():
# 您可以返回一个简单的欢迎信息
return "API server is running. Please use the specific endpoints to interact."
# 或者返回一个更友好的 HTML 页面
# return "<h1>欢迎!这是一个API服务器。</h1>"
# Create a queue to hold tasks
task_queue = Queue()
queue_id = id(task_queue) # Generate a single queue_id for this worker
# Function to process tasks from the queue
def process_queue():
while True:
job_id, data, task_func, queue_start_time = task_queue.get()
queue_time = time.time() - queue_start_time
run_start_time = time.time()
pid = os.getpid() # Get the PID of the actual processing thread
# Log job status as running
log_job_status(job_id, {
"job_status": "running",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": None
})
response = task_func()
run_time = time.time() - run_start_time
total_time = time.time() - queue_start_time
response_data = {
"endpoint": response[1],
"code": response[2],
"id": data.get("id"),
"job_id": job_id,
"response": response[0] if response[2] == 200 else None,
"message": "success" if response[2] == 200 else response[0],
"pid": pid,
"queue_id": queue_id,
"run_time": round(run_time, 3),
"queue_time": round(queue_time, 3),
"total_time": round(total_time, 3),
"queue_length": task_queue.qsize(),
"build_number": BUILD_NUMBER # Add build number to response
}
# Log job status as done
log_job_status(job_id, {
"job_status": "done",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": response_data
})
# Only send webhook if webhook_url has an actual value (not an empty string)
if data.get("webhook_url") and data.get("webhook_url") != "":
send_webhook(data.get("webhook_url"), response_data)
task_queue.task_done()
# Start the queue processing in a separate thread
threading.Thread(target=process_queue, daemon=True).start()
# Decorator to add tasks to the queue or bypass it
def queue_task(bypass_queue=False):
def decorator(f):
def wrapper(*args, **kwargs):
job_id = str(uuid.uuid4())
data = request.json if request.is_json else {}
pid = os.getpid() # Get PID for non-queued tasks
start_time = time.time()
if bypass_queue or 'webhook_url' not in data:
# Log job status as running immediately (bypassing queue)
log_job_status(job_id, {
"job_status": "running",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": None
})
response = f(job_id=job_id, data=data, *args, **kwargs)
run_time = time.time() - start_time
response_obj = {
"code": response[2],
"id": data.get("id"),
"job_id": job_id,
"response": response[0] if response[2] == 200 else None,
"message": "success" if response[2] == 200 else response[0],
"run_time": round(run_time, 3),
"queue_time": 0,
"total_time": round(run_time, 3),
"pid": pid,
"queue_id": queue_id,
"queue_length": task_queue.qsize(),
"build_number": BUILD_NUMBER # Add build number to response
}
# Log job status as done
log_job_status(job_id, {
"job_status": "done",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": response_obj
})
return response_obj, response[2]
else:
if MAX_QUEUE_LENGTH > 0 and task_queue.qsize() >= MAX_QUEUE_LENGTH:
error_response = {
"code": 429,
"id": data.get("id"),
"job_id": job_id,
"message": f"MAX_QUEUE_LENGTH ({MAX_QUEUE_LENGTH}) reached",
"pid": pid,
"queue_id": queue_id,
"queue_length": task_queue.qsize(),
"build_number": BUILD_NUMBER # Add build number to response
}
# Log the queue overflow error
log_job_status(job_id, {
"job_status": "done",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": error_response
})
return error_response, 429
# Log job status as queued
log_job_status(job_id, {
"job_status": "queued",
"job_id": job_id,
"queue_id": queue_id,
"process_id": pid,
"response": None
})
task_queue.put((job_id, data, lambda: f(job_id=job_id, data=data, *args, **kwargs), start_time))
return {
"code": 202,
"id": data.get("id"),
"job_id": job_id,
"message": "processing",
"pid": pid,
"queue_id": queue_id,
"max_queue_length": MAX_QUEUE_LENGTH if MAX_QUEUE_LENGTH > 0 else "unlimited",
"queue_length": task_queue.qsize(),
"build_number": BUILD_NUMBER # Add build number to response
}, 202
return wrapper
return decorator
app.queue_task = queue_task
# Register special route for Next.js root asset paths first
from routes.v1.media.feedback import create_root_next_routes
create_root_next_routes(app)
# Use the discover_and_register_blueprints function to register all blueprints
discover_and_register_blueprints(app)
return app
app = create_app()
if __name__ == '__main__':
app.launch(server_name="0.0.0.0", server_port=7860)
|