import logging from services.job_queue import ( _job_queue_lock, _job_queue, _job_history, _queue_worker_event, ) logger = logging.getLogger('gaze') def register_routes(app): @app.route('/api/queue') def api_queue_list(): """Return the current queue as JSON.""" with _job_queue_lock: jobs = [ { 'id': j['id'], 'label': j['label'], 'status': j['status'], 'error': j['error'], 'created_at': j['created_at'], } for j in _job_queue ] return {'jobs': jobs, 'count': len(jobs)} @app.route('/api/queue/count') def api_queue_count(): """Return just the count of active (non-done, non-failed) jobs.""" with _job_queue_lock: count = sum(1 for j in _job_queue if j['status'] in ('pending', 'processing', 'paused')) return {'count': count} @app.route('/api/queue//remove', methods=['POST']) def api_queue_remove(job_id): """Remove a pending or paused job from the queue.""" with _job_queue_lock: job = _job_history.get(job_id) if not job: return {'error': 'Job not found'}, 404 if job['status'] == 'processing': return {'error': 'Cannot remove a job that is currently processing'}, 400 try: _job_queue.remove(job) except ValueError: pass # Already not in queue job['status'] = 'removed' return {'status': 'ok'} @app.route('/api/queue//pause', methods=['POST']) def api_queue_pause(job_id): """Toggle pause/resume on a pending job.""" with _job_queue_lock: job = _job_history.get(job_id) if not job: return {'error': 'Job not found'}, 404 if job['status'] == 'pending': job['status'] = 'paused' elif job['status'] == 'paused': job['status'] = 'pending' _queue_worker_event.set() else: return {'error': f'Cannot pause/resume job with status {job["status"]}'}, 400 return {'status': 'ok', 'new_status': job['status']} @app.route('/api/queue/clear', methods=['POST']) def api_queue_clear(): """Clear all pending jobs from the queue (allows current processing job to finish).""" removed_count = 0 with _job_queue_lock: pending_jobs = [j for j in _job_queue if j['status'] == 'pending'] for job in pending_jobs: try: _job_queue.remove(job) job['status'] = 'removed' removed_count += 1 except ValueError: pass logger.info("Cleared %d pending jobs from queue", removed_count) return {'status': 'ok', 'removed_count': removed_count} @app.route('/api/queue//status') def api_queue_job_status(job_id): """Return the status of a specific job.""" with _job_queue_lock: job = _job_history.get(job_id) if not job: return {'error': 'Job not found'}, 404 resp = { 'id': job['id'], 'label': job['label'], 'status': job['status'], 'error': job['error'], 'comfy_prompt_id': job['comfy_prompt_id'], } if job.get('result'): resp['result'] = job['result'] return resp