mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
Add long_polling_jobs to API (#78)
This commit is contained in:
@@ -55,15 +55,30 @@ def sorted_jobs(all_jobs, sort_by_date=True):
|
||||
@server.get('/api/jobs')
|
||||
def jobs_json():
|
||||
try:
|
||||
hash_token = request.args.get('token', None)
|
||||
all_jobs = [x.json() for x in RenderQueue.all_jobs()]
|
||||
job_cache_int = int(json.dumps(all_jobs).__hash__())
|
||||
job_cache_token = num_to_alphanumeric(job_cache_int)
|
||||
if hash_token and hash_token == job_cache_token:
|
||||
return [], 204 # no need to update
|
||||
return {'jobs': all_jobs, 'token': job_cache_token}
|
||||
except Exception as e:
|
||||
logger.exception(f"Exception fetching all_jobs_cached: {e}")
|
||||
logger.exception(f"Exception fetching jobs_json: {e}")
|
||||
return [], 500
|
||||
|
||||
|
||||
@server.get('/api/jobs_long_poll')
|
||||
def long_polling_jobs():
|
||||
try:
|
||||
hash_token = request.args.get('token', None)
|
||||
start_time = time.time()
|
||||
while True:
|
||||
all_jobs = jobs_json()
|
||||
if all_jobs['token'] != hash_token:
|
||||
return all_jobs
|
||||
# Break after 30 seconds to avoid gateway timeout
|
||||
if time.time() - start_time > 30:
|
||||
return [], 204
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
logger.exception(f"Exception fetching long_polling_jobs: {e}")
|
||||
return [], 500
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user