def mutate_and_get_payload(cls, root, info, owner, labbook_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name) d = Dispatcher() lb_jobs = d.get_jobs_for_labbook(lb.key) jobs = [ j for j in d.get_jobs_for_labbook(lb.key) if j.meta.get('method') == 'build_image' and j.status == 'started' ] if len(jobs) == 1: d.abort_task(jobs[0].job_key) ContainerOperations.delete_image(lb, username=username) return CancelBuild(build_stopped=True, message="Stopped build") elif len(jobs) == 0: logger.warning(f"No build_image tasks found for {str(lb)}") return CancelBuild(build_stopped=False, message="No build task found") else: logger.warning(f"Multiple build jobs found for {str(lb)}") return CancelBuild(build_stopped=False, message="Multiple builds found")
def helper_resolve_image_status(self, labbook): """Helper to resolve the image status of a labbook""" labbook_image_key = infer_docker_image_name( labbook_name=self.name, owner=self.owner, username=get_logged_in_username()) dispatcher = Dispatcher() lb_jobs = [ dispatcher.query_task(j.job_key) for j in dispatcher.get_jobs_for_labbook(labbook.key) ] for j in lb_jobs: logger.debug("Current job for labbook: status {}, meta {}".format( j.status, j.meta)) # First, check if image exists or not -- The first step of building an image untags any existing ones. # Therefore, we know that if one exists, there most likely is not one being built. try: client = get_docker_client() client.images.get(labbook_image_key) image_status = ImageStatus.EXISTS except (ImageNotFound, requests.exceptions.ConnectionError): image_status = ImageStatus.DOES_NOT_EXIST if any([ j.status == 'failed' and j.meta.get('method') == 'build_image' for j in lb_jobs ]): logger.debug("Image status for {} is BUILD_FAILED".format( labbook.key)) if image_status == ImageStatus.EXISTS: # The indication that there's a failed job is probably lingering from a while back, so don't # change the status to FAILED. Only do that if there is no Docker image. logger.debug( f'Got failed build_image for labbook {labbook.key}, but image exists.' ) else: image_status = ImageStatus.BUILD_FAILED if any([ j.status in ['started'] and j.meta.get('method') == 'build_image' for j in lb_jobs ]): logger.debug( f"Image status for {labbook.key} is BUILD_IN_PROGRESS") # build_image being in progress takes precedence over if image already exists (unlikely event). if image_status == ImageStatus.EXISTS: logger.warning( f'Got build_image for labbook {labbook.key}, but image exists.' ) image_status = ImageStatus.BUILD_IN_PROGRESS if any([ j.status in ['queued'] and j.meta.get('method') == 'build_image' for j in lb_jobs ]): logger.warning( f"build_image for {labbook.key} stuck in queued state") image_status = ImageStatus.BUILD_QUEUED return image_status.value
def helper_resolve_background_jobs(labbook): """Helper to generate background job info from a labbook""" d = Dispatcher() jobs = d.get_jobs_for_labbook(labbook_key=labbook.key) return [JobStatus(j.job_key.key_str) for j in jobs]