def new_task(): task = Task(command='python command.py', hostname='localhost', _status=TaskStatus.not_running) cmd_segment = CommandSegment(name='--batch_size', _segment_type=SegmentType.parameter) task.add_cmd_segment(cmd_segment, '32') task.save() return task
def new_task_2(): task = Task(command='python command2.py', hostname='remotehost', _status=TaskStatus.not_running) cmd_segment = CommandSegment(name='--rank', _segment_type=SegmentType.parameter) task.add_cmd_segment(cmd_segment, '1') task.save() return task
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