def business_create(task: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: """Creates new Task db record. Fields which require to be of datetime type are explicitly converted here. """ try: new_task = Task( user_id=task['userId'], hostname=task['hostname'], command=task['command'], # TODO Adjust API spec, optional fields spawn_at=DateUtils.try_parse_string(task.get('spawnAt')), terminate_at=DateUtils.try_parse_string(task.get('terminateAt'))) # assert all(task.values()), 'fields cannot be blank or null' new_task.save() except ValueError: # Invalid string format for datetime content, status = {'msg': GENERAL['bad_request']}, 422 except KeyError: # At least one of required fields was not present content, status = {'msg': GENERAL['bad_request']}, 422 except AssertionError as e: content, status = { 'msg': TASK['create']['failure']['invalid'].format(reason=e) }, 422 except Exception as e: log.critical(e) content, status = {'msg': GENERAL['internal_error']}, 500 else: content, status = { 'msg': TASK['create']['success'], 'task': new_task.as_dict() }, 201 finally: return content, status
def business_create(task: Dict[str, Any], job_id: JobId) -> Tuple[Content, HttpStatusCode]: """ Creates new Task db record under the given parent job and new db records for given command segments of that task. """ try: new_task = Task( hostname=task['hostname'], command=task['command']) new_task.gpu_id = parse_gpu_id_from_command(task['command']) parent_job = Job.query.filter(Job.id == job_id).one() for segment in task['cmdsegments']['params']: new_segment = CommandSegment.query.filter(CommandSegment.segment_type == SegmentType.parameter, CommandSegment.name == segment['name']).first() if (new_segment is None): new_segment = CommandSegment( name=segment['name'], _segment_type=SegmentType.parameter) new_task.add_cmd_segment(new_segment, segment['value']) for segment in task['cmdsegments']['envs']: new_segment = CommandSegment.query.filter(CommandSegment.segment_type == SegmentType.env_variable, CommandSegment.name == segment['name']).first() if (new_segment is None): new_segment = CommandSegment( name=segment['name'], _segment_type=SegmentType.env_variable) new_task.add_cmd_segment(new_segment, segment['value']) new_task.save() parent_job.add_task(new_task) except KeyError: # At least one of required fields was not present content, status = {'msg': GENERAL['bad_request']}, 422 except AssertionError as e: content, status = {'msg': TASK['create']['failure']['invalid'].format(reason=e)}, 422 except Exception as e: log.critical(e) content, status = {'msg': GENERAL['internal_error']}, 500 else: content, status = {'msg': TASK['create']['success'], 'task': new_task.as_dict()}, 201 finally: return content, status