|
| 1 | +"""Run a pipeline step locally in a Docker container. |
| 2 | +
|
| 3 | +Reuses Docker infrastructure from ModelRunLocally for building and managing |
| 4 | +containers, but runs pipeline_step.py instead of the model server. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import signal |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import time |
| 12 | + |
| 13 | +from clarifai.runners.models.model_run_locally import ModelRunLocally |
| 14 | +from clarifai.runners.pipeline_steps.pipeline_step_builder import PipelineStepBuilder |
| 15 | +from clarifai.utils.logging import logger |
| 16 | + |
| 17 | + |
| 18 | +class PipelineStepRunLocally: |
| 19 | + """Run a single pipeline step locally in a Docker container. |
| 20 | +
|
| 21 | + Reuses ModelRunLocally for Docker build/image/container management, |
| 22 | + but overrides the container command to run pipeline_step.py. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, step_path): |
| 26 | + self.step_path = os.path.abspath(step_path) |
| 27 | + self.builder = PipelineStepBuilder(self.step_path) |
| 28 | + self.config = self.builder.config |
| 29 | + |
| 30 | + # Create a ModelRunLocally instance for Docker utilities. |
| 31 | + # We bypass __init__ since it expects a ModelBuilder, and set the |
| 32 | + # attributes that the Docker methods rely on directly. |
| 33 | + self._docker = ModelRunLocally.__new__(ModelRunLocally) |
| 34 | + self._docker.model_path = self.step_path |
| 35 | + self._docker.requirements_file = os.path.join(self.step_path, "requirements.txt") |
| 36 | + |
| 37 | + # ── Delegated Docker utilities ────────────────────────────────────── |
| 38 | + |
| 39 | + def is_docker_installed(self): |
| 40 | + return self._docker.is_docker_installed() |
| 41 | + |
| 42 | + def docker_image_exists(self, image_name): |
| 43 | + return self._docker.docker_image_exists(image_name) |
| 44 | + |
| 45 | + def build_docker_image(self, image_name): |
| 46 | + return self._docker.build_docker_image(image_name=image_name) |
| 47 | + |
| 48 | + def container_exists(self, container_name): |
| 49 | + return self._docker.container_exists(container_name) |
| 50 | + |
| 51 | + def stop_docker_container(self, container_name): |
| 52 | + return self._docker.stop_docker_container(container_name) |
| 53 | + |
| 54 | + def remove_docker_container(self, container_name): |
| 55 | + return self._docker.remove_docker_container(container_name) |
| 56 | + |
| 57 | + def remove_docker_image(self, image_name): |
| 58 | + return self._docker.remove_docker_image(image_name) |
| 59 | + |
| 60 | + def _docker_hash(self): |
| 61 | + return self._docker._docker_hash() |
| 62 | + |
| 63 | + def _gpu_is_available(self): |
| 64 | + return self._docker._gpu_is_available() |
| 65 | + |
| 66 | + # ── Pipeline-step-specific container run ──────────────────────────── |
| 67 | + |
| 68 | + def run_pipeline_step_container( |
| 69 | + self, |
| 70 | + image_name, |
| 71 | + container_name="clarifai-pipeline-step-container", |
| 72 | + env_vars=None, |
| 73 | + ): |
| 74 | + """Run pipeline_step.py inside a Docker container and wait for it to finish. |
| 75 | +
|
| 76 | + Unlike ``ModelRunLocally.run_docker_container`` which starts a long-running |
| 77 | + server, this method executes the pipeline step script once and exits. |
| 78 | + """ |
| 79 | + try: |
| 80 | + cmd = ["docker", "run", "--name", container_name, "--rm", "--network", "host"] |
| 81 | + |
| 82 | + if self._gpu_is_available(): |
| 83 | + cmd.extend(["--gpus", "all"]) |
| 84 | + |
| 85 | + # Mount step directory into container (same target as model serve) |
| 86 | + cmd.extend( |
| 87 | + [ |
| 88 | + "--mount", |
| 89 | + f"type=bind,source={self.step_path},target=/home/nonroot/main", |
| 90 | + ] |
| 91 | + ) |
| 92 | + |
| 93 | + if env_vars: |
| 94 | + for key, value in env_vars.items(): |
| 95 | + cmd.extend(["-e", f"{key}={value}"]) |
| 96 | + |
| 97 | + cmd.extend(["-e", "PYTHONDONTWRITEBYTECODE=1"]) |
| 98 | + |
| 99 | + # Override entrypoint to run pipeline_step.py directly |
| 100 | + cmd.extend(["--entrypoint", "python"]) |
| 101 | + cmd.append(image_name) |
| 102 | + cmd.extend(["/home/nonroot/main/1/pipeline_step.py"]) |
| 103 | + |
| 104 | + logger.info(f"Running pipeline step in container '{container_name}'...") |
| 105 | + logger.info(f"Docker command: {cmd}") |
| 106 | + |
| 107 | + process = subprocess.Popen(cmd) |
| 108 | + |
| 109 | + # Graceful Ctrl+C handling |
| 110 | + original_sigint = signal.getsignal(signal.SIGINT) |
| 111 | + |
| 112 | + def signal_handler(sig, frame): |
| 113 | + logger.info(f"Stopping container '{container_name}'...") |
| 114 | + subprocess.run(["docker", "stop", container_name], check=False) |
| 115 | + process.terminate() |
| 116 | + signal.signal(signal.SIGINT, original_sigint) |
| 117 | + time.sleep(1) |
| 118 | + sys.exit(0) |
| 119 | + |
| 120 | + signal.signal(signal.SIGINT, signal_handler) |
| 121 | + |
| 122 | + process.wait() |
| 123 | + |
| 124 | + # Restore original handler |
| 125 | + signal.signal(signal.SIGINT, original_sigint) |
| 126 | + |
| 127 | + if process.returncode != 0: |
| 128 | + logger.error(f"Pipeline step failed with exit code {process.returncode}") |
| 129 | + sys.exit(process.returncode) |
| 130 | + |
| 131 | + logger.info("Pipeline step completed successfully!") |
| 132 | + |
| 133 | + except subprocess.CalledProcessError as e: |
| 134 | + logger.error(f"Error running pipeline step container: {e}") |
| 135 | + sys.exit(1) |
| 136 | + except Exception as e: |
| 137 | + logger.error(f"Error running pipeline step container: {e}") |
| 138 | + sys.exit(1) |
0 commit comments