-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathapp.py
162 lines (138 loc) · 6.62 KB
/
app.py
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
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
MAX_QUEUE_LENGTH = int(os.environ.get('MAX_QUEUE_LENGTH', 0))
def create_app():
app = Flask(__name__)
# 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
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
}
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:
response = f(job_id=job_id, data=data, *args, **kwargs)
run_time = time.time() - start_time
return {
"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
}, response[2]
else:
if MAX_QUEUE_LENGTH > 0 and task_queue.qsize() >= MAX_QUEUE_LENGTH:
return {
"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
}, 429
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
# Import blueprints
from routes.media_to_mp3 import convert_bp
from routes.transcribe_media import transcribe_bp
from routes.combine_videos import combine_bp
from routes.audio_mixing import audio_mixing_bp
from routes.gdrive_upload import gdrive_upload_bp
from routes.authenticate import auth_bp
from routes.caption_video import caption_bp
from routes.extract_keyframes import extract_keyframes_bp
from routes.image_to_video import image_to_video_bp
# Register blueprints
app.register_blueprint(convert_bp)
app.register_blueprint(transcribe_bp)
app.register_blueprint(combine_bp)
app.register_blueprint(audio_mixing_bp)
app.register_blueprint(gdrive_upload_bp)
app.register_blueprint(auth_bp)
app.register_blueprint(caption_bp)
app.register_blueprint(extract_keyframes_bp)
app.register_blueprint(image_to_video_bp)
# version 1.0
from routes.v1.ffmpeg.ffmpeg_compose import v1_ffmpeg_compose_bp
from routes.v1.media.media_transcribe import v1_media_transcribe_bp
from routes.v1.media.transform.media_to_mp3 import v1_media_transform_mp3_bp
from routes.v1.video.concatenate import v1_video_concatenate_bp
from routes.v1.video.caption_video import v1_video_caption_bp
from routes.v1.image.transform.image_to_video import v1_image_transform_video_bp
from routes.v1.toolkit.test import v1_toolkit_test_bp
from routes.v1.toolkit.authenticate import v1_toolkit_auth_bp
from routes.v1.code.execute.execute_python import v1_code_execute_bp
app.register_blueprint(v1_ffmpeg_compose_bp)
app.register_blueprint(v1_media_transcribe_bp)
app.register_blueprint(v1_media_transform_mp3_bp)
app.register_blueprint(v1_video_concatenate_bp)
app.register_blueprint(v1_video_caption_bp)
app.register_blueprint(v1_image_transform_video_bp)
app.register_blueprint(v1_toolkit_test_bp)
app.register_blueprint(v1_toolkit_auth_bp)
app.register_blueprint(v1_code_execute_bp)
return app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)