async def read_output(job_id: int): query = jobs.select(jobs.c.id == job_id) job = await database.fetch_one(query) path = f'output_zips/{job_id}.zip' if job is None or not os.path.exists(path): return None return FileResponse(path, filename=f'{job_id}.zip')
async def pop(robot_name: str): # Record this activity. query = activity.insert(None).values(robot=robot_name) await database.execute(query) # Pop the robot from the queue. query = jobs.select().where(jobs.c.status == STATUS['QUEUED']) \ .where(jobs.c.robot == robot_name) \ .order_by(jobs.c.timestamp.asc()) job = await database.fetch_one(query) if job is None: return None query = jobs.update().values(status=STATUS['RUNNING']) \ .where(jobs.c.id == job.id) await database.execute(query) return await database.fetch_one(jobs.select(jobs.c.id == job.id))
async def delete_job(job_id: int): query = jobs.select(jobs.c.id == job_id) job = await database.fetch_one(query) new_query = jobs.delete().where(jobs.c.id == job_id) await database.execute(new_query) for path in [f'code_zips/{job_id}.zip', f'output_zips/{job_id}.zip']: if os.path.exists(path): os.remove(path) return job
async def update_job(job_id: int, job_status: str, job_logs: UploadFile = File(...), output_zip: UploadFile = File(...)): job_logs_string = await job_logs.read() query = jobs.update().values(status=job_status, logs=job_logs_string) \ .where(jobs.c.id == job_id) await database.execute(query) ensure_storage() with open(f'output_zips/{job_id}.zip', 'wb+') as f: output = await output_zip.read() f.write(output) new_query = jobs.select(jobs.c.id == job_id) return await database.fetch_one(new_query)
async def create_job(container: str, mount: str, robot: str, run_command: str, code_zip: UploadFile = File(...)): query = jobs.insert(None).values(container=container, mount=mount, robot=robot, run_command=run_command) last_job_id = await database.execute(query) ensure_storage() with open(f'code_zips/{last_job_id}.zip', 'wb+') as f: output = await code_zip.read() f.write(output) new_query = jobs.select().where(jobs.c.id == last_job_id) return await database.fetch_one(new_query)
async def read_robot_history(robot_name: str): query = jobs.select().where(jobs.c.status != STATUS['QUEUED']) \ .where(jobs.c.robot == robot_name) \ .order_by(jobs.c.timestamp.desc()) return await database.fetch_all(query)
async def read_all_histories(): query = jobs.select().where(jobs.c.status != STATUS['QUEUED']) \ .order_by(jobs.c.timestamp.desc()) return await database.fetch_all(query)
async def read_job(job_id: int): query = jobs.select().where(jobs.c.id == job_id) return await database.fetch_one(query)