File size: 11,552 Bytes
8cfed6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from flask import Flask, request, jsonify
from datetime import datetime, timedelta
import requests
import uuid
import json
from threading import Lock
from collections import deque
import os

app = Flask(__name__)

# Configuration
WORKER_URLS = os.getenv('WORKER_URLS', '').split(',')
API_KEY = os.getenv('API_KEY', '')

# Data structures
workers = {}
job_queue = deque()
jobs = {}
lock = Lock()

# Initialize workers from environment
for idx, url in enumerate(WORKER_URLS):
    if url.strip():
        worker_id = f"worker-{idx+1}"
        workers[worker_id] = {
            'url': url.strip(),
            'status': 'unknown',
            'last_heartbeat': None,
            'total_processed': 0,
            'current_job': None
        }

def verify_api_key():
    """Verify API key if configured"""
    if not API_KEY:
        return True
    
    auth_header = request.headers.get('Authorization', '')
    if auth_header.startswith('Bearer '):
        token = auth_header[7:]
        return token == API_KEY
    return False

@app.route('/health', methods=['GET'])
def health_check():
    """Health check endpoint"""
    return jsonify({
        'status': 'online',
        'timestamp': datetime.now().isoformat(),
        'workers': len(workers),
        'queue_size': len(job_queue),
        'active_jobs': len([j for j in jobs.values() if j['status'] == 'processing'])
    })

@app.route('/submit', methods=['POST'])
def submit_job():
    """Submit a new job"""
    if not verify_api_key():
        return jsonify({'error': 'Unauthorized'}), 401
    
    data = request.json
    
    if not data or 'unique_id' not in data:
        return jsonify({'error': 'Missing unique_id'}), 400
    
    if 'service_type' not in data:
        return jsonify({'error': 'Missing service_type'}), 400
    
    if 'image_data' not in data:
        return jsonify({'error': 'Missing image_data'}), 400
    
    unique_id = data['unique_id']
    
    with lock:
        if unique_id in jobs:
            return jsonify({
                'message': 'Job already exists',
                'unique_id': unique_id,
                'status': jobs[unique_id]['status']
            }), 200
        
        jobs[unique_id] = {
            'status': 'queued',
            'service_type': data['service_type'],
            'image_data': data['image_data'],
            'result': None,
            'created_at': datetime.now(),
            'worker_id': None,
            'error': None
        }
        
        job_queue.append(unique_id)
    
    assign_jobs_to_workers()
    
    return jsonify({
        'message': 'Job submitted successfully',
        'unique_id': unique_id,
        'queue_position': get_queue_position(unique_id)
    }), 202

@app.route('/status/<unique_id>', methods=['GET'])
def check_status(unique_id):
    """Check job status"""
    if not verify_api_key():
        return jsonify({'error': 'Unauthorized'}), 401
    
    with lock:
        if unique_id not in jobs:
            return jsonify({'error': 'Job not found'}), 404
        
        job = jobs[unique_id]
        
        response = {
            'unique_id': unique_id,
            'status': job['status'],
            'created_at': job['created_at'].isoformat()
        }
        
        if job['status'] == 'completed':
            response['data'] = job['result']
            response['service_type'] = job['service_type']
            
        elif job['status'] == 'failed':
            # ✅ FIX: Return complete error data structure
            error_data = job.get('result', {}) if isinstance(job.get('result'), dict) else {}
            
            if 'error' in error_data:
                response['error'] = error_data['error']
                response['data'] = error_data
            elif job.get('error'):
                response['error'] = job['error']
                response['data'] = {'error': job['error']}
            else:
                response['error'] = 'Unknown error'
                response['data'] = {'error': 'Unknown error'}
            
            print(f"[Orchestrator] Failed job {unique_id} - Error: {response['error']}")
            
        elif job['status'] == 'queued':
            response['queue_position'] = get_queue_position(unique_id)
            
        elif job['status'] == 'processing':
            response['worker_id'] = job['worker_id']
        
        return jsonify(response), 200

@app.route('/workers/status', methods=['GET'])
def workers_status():
    """Get status of all workers"""
    if not verify_api_key():
        return jsonify({'error': 'Unauthorized'}), 401
    
    with lock:
        worker_list = []
        for worker_id, worker in workers.items():
            worker_list.append({
                'id': worker_id,
                'status': worker['status'],
                'total_processed': worker['total_processed'],
                'current_job': worker['current_job'],
                'last_heartbeat': worker['last_heartbeat'].isoformat() if worker['last_heartbeat'] else None
            })
        
        return jsonify({
            'workers': worker_list,
            'total_workers': len(workers),
            'active_workers': len([w for w in workers.values() if w['status'] == 'idle' or w['status'] == 'busy']),
            'queue_size': len(job_queue)
        }), 200

@app.route('/worker/heartbeat', methods=['POST'])
def worker_heartbeat():
    """Worker heartbeat endpoint"""
    data = request.json
    
    if not data or 'worker_id' not in data:
        return jsonify({'error': 'Missing worker_id'}), 400
    
    worker_id = data['worker_id']
    
    with lock:
        if worker_id not in workers:
            workers[worker_id] = {
                'url': data.get('url', ''),
                'status': 'idle',
                'last_heartbeat': datetime.now(),
                'total_processed': 0,
                'current_job': None
            }
        else:
            workers[worker_id]['status'] = data.get('status', 'idle')
            workers[worker_id]['last_heartbeat'] = datetime.now()
            
            if data.get('status') == 'idle' and workers[worker_id]['current_job']:
                workers[worker_id]['current_job'] = None
    
    assign_jobs_to_workers()
    
    return jsonify({'message': 'Heartbeat received'}), 200

@app.route('/worker/result', methods=['POST'])
def worker_result():
    """Worker submits job result"""
    data = request.json
    
    if not data or 'unique_id' not in data:
        return jsonify({'error': 'Missing unique_id'}), 400
    
    unique_id = data['unique_id']
    worker_id = data.get('worker_id')
    result_status = data.get('status', 'failed')
    
    print(f"[Orchestrator] Received result for {unique_id} - Status: {result_status}")
    
    with lock:
        if unique_id not in jobs:
            print(f"[Orchestrator] Job {unique_id} not found")
            return jsonify({'error': 'Job not found'}), 404
        
        job = jobs[unique_id]
        
        # ✅ FIX: Store complete result structure
        if result_status == 'completed':
            job['status'] = 'completed'
            job['result'] = data.get('result', {})
            print(f"[Orchestrator] Job {unique_id} completed successfully")
            
        else:  # failed
            job['status'] = 'failed'
            
            # Store the complete error structure
            result_data = data.get('result', {})
            
            if isinstance(result_data, dict):
                job['result'] = result_data
                job['error'] = result_data.get('error', 'Unknown error')
            elif isinstance(result_data, str):
                job['result'] = {'error': result_data}
                job['error'] = result_data
            else:
                error_msg = data.get('error', 'Unknown error')
                job['result'] = {'error': error_msg}
                job['error'] = error_msg
            
            print(f"[Orchestrator] Job {unique_id} failed - Error: {job['error']}")
        
        # Update worker
        if worker_id and worker_id in workers:
            workers[worker_id]['status'] = 'idle'
            workers[worker_id]['current_job'] = None
            workers[worker_id]['total_processed'] += 1
    
    assign_jobs_to_workers()
    
    return jsonify({'message': 'Result received'}), 200

def assign_jobs_to_workers():
    """Assign pending jobs to idle workers"""
    with lock:
        while job_queue:
            idle_worker = None
            for worker_id, worker in workers.items():
                if worker['status'] == 'idle':
                    idle_worker = worker_id
                    break
            
            if not idle_worker:
                break
            
            unique_id = job_queue.popleft()
            
            if unique_id not in jobs:
                continue
            
            job = jobs[unique_id]
            
            job['status'] = 'processing'
            job['worker_id'] = idle_worker
            
            workers[idle_worker]['status'] = 'busy'
            workers[idle_worker]['current_job'] = unique_id
            
            try:
                worker_url = workers[idle_worker]['url']
                print(f"[Orchestrator] Assigning job {unique_id} to {idle_worker}")
                
                response = requests.post(
                    f"{worker_url}/process",
                    json={
                        'unique_id': unique_id,
                        'service_type': job['service_type'],
                        'image_data': job['image_data']
                    },
                    timeout=5
                )
                
                if response.status_code != 200:
                    print(f"[Orchestrator] Worker {idle_worker} rejected job {unique_id}")
                    job['status'] = 'queued'
                    job['worker_id'] = None
                    job_queue.append(unique_id)
                    workers[idle_worker]['status'] = 'idle'
                    workers[idle_worker]['current_job'] = None
                    
            except Exception as e:
                print(f"[Orchestrator] Error sending job to worker {idle_worker}: {str(e)}")
                job['status'] = 'queued'
                job['worker_id'] = None
                job_queue.append(unique_id)
                workers[idle_worker]['status'] = 'offline'
                workers[idle_worker]['current_job'] = None

def get_queue_position(unique_id):
    """Get position of job in queue"""
    try:
        return list(job_queue).index(unique_id) + 1
    except ValueError:
        return 0

def cleanup_old_jobs():
    """Clean up jobs older than 24 hours"""
    with lock:
        cutoff_time = datetime.now() - timedelta(hours=24)
        jobs_to_delete = []
        
        for unique_id, job in jobs.items():
            if job['created_at'] < cutoff_time:
                jobs_to_delete.append(unique_id)
        
        for unique_id in jobs_to_delete:
            del jobs[unique_id]
            print(f"[Orchestrator] Cleaned up old job: {unique_id}")

from threading import Timer

def periodic_cleanup():
    cleanup_old_jobs()
    Timer(3600, periodic_cleanup).start()

periodic_cleanup()

if __name__ == '__main__':
    port = int(os.getenv('PORT', 7860))
    print(f"[Orchestrator] Starting on port {port}")
    print(f"[Orchestrator] Workers configured: {len([w for w in WORKER_URLS if w.strip()])}")
    app.run(host='0.0.0.0', port=port)