async def submit_record_by_judge( record_result: schemas.RecordResult, record: models.Record = Depends(parse_record_judger), user: models.User = Depends(parse_user_from_auth), ) -> StandardResponse[Empty]: if record.state != schemas.RecordState.fetched: raise BizError(ErrorCode.Error) return StandardResponse()
async def claim_record_by_judge( judge_claim: schemas.JudgeClaim, record: models.Record = Depends(parse_record_judger), user: models.User = Depends(parse_user_from_auth), ) -> StandardResponse[schemas.JudgeCredentials]: # task_id can only be obtained by listening to the celery task queue # we give the worker with task_id the chance to claim the record # celery tasks can be retried, only one worker can hold the task_id at the same time # if a rejudge is scheduled, task_id changes, so previous task will be ineffective # TODO: we probably need a lock to handle race condition of rejudge and claim if record.task_id is None or record.task_id != judge_claim.task_id: raise BizError(ErrorCode.Error) # if record.state not in (schemas.RecordState.queueing, schemas.RecordState.retrying): # raise BizError(ErrorCode.Error) # we can mark task failed if no problem config is available if record.problem_config is None or record.problem is None: raise BizError(ErrorCode.Error) # we always reset the state to "fetched", for both first attempt and retries record.judger_id = user.id record.state = schemas.RecordState.fetched await record.save_model() logger.info("judger claim record: {}", record) # initialize the permission of the judger to lakefs # the user have read access to all problems in the problem group, # actually only the access to one branch is necessary, # but it will create too many policies, so we grant all for simplicity # the user have read/write access to all records in the problem, # because the judger will write test result to the repo await record.fetch_related("problem") access_key = await models.UserAccessKey.get_lakefs_access_key(user) lakefs_problem_config = LakeFSProblemConfig(record.problem) lakefs_record = LakeFSRecord(record.problem, record) def sync_func() -> None: lakefs_problem_config.ensure_user_policy(user, "read") lakefs_record.ensure_user_policy(user, "all") await run_in_threadpool(sync_func) judge_credentials = schemas.JudgeCredentials( access_key_id=access_key.access_key_id, secret_access_key=access_key.secret_access_key, problem_config_repo_name=lakefs_problem_config.repo_name, problem_config_commit_id=record.problem_config.commit_id, record_repo_name=lakefs_record.repo_name, record_commit_id=record.commit_id, ) return StandardResponse(judge_credentials)
async def set_root_user( user: models.User = Depends(parse_user_from_auth), session: AsyncSession = Depends(db_session_dependency), ) -> StandardResponse[schemas.User]: root_user = await models.User.all(role=DefaultRole.ROOT) if root_user != []: raise BizError(ErrorCode.Error) current_user = await models.User.one_or_none(id=user.id) if current_user is None: raise BizError(ErrorCode.UserNotFoundError) current_user.role = DefaultRole.ROOT session.sync_session.add(current_user) await session.commit() await session.refresh(current_user) return StandardResponse(current_user)
async def update_record_state_by_judge( record: models.Record = Depends(parse_record_judger), user: models.User = Depends(parse_user_from_auth), ) -> StandardResponse[schemas.Record]: if record.judger_id != user.id: raise BizError(ErrorCode.Error) if record.state not in ( schemas.RecordState.fetched, schemas.RecordState.compiling, schemas.RecordState.running, schemas.RecordState.judging, ): raise BizError(ErrorCode.Error) record.state = schemas.RecordState.fetched await record.save_model() return StandardResponse(record)
async def get_record( record: schemas.RecordDetail = Depends(parse_record), ) -> StandardResponse[schemas.RecordDetail]: return StandardResponse(schemas.RecordDetail.from_orm(record))
async def test_error_report() -> StandardResponse[Empty]: assert False return StandardResponse()
async def jwt_decoded( jwt_access_token: JWTAccessToken = Depends(auth_jwt_decode_access_token), ) -> StandardResponse[JWTAccessToken]: return StandardResponse(jwt_access_token)