Skip to content

Commit

Permalink
feat: improve reaction time
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Dec 5, 2024
1 parent ab725b6 commit 23a4804
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions hive_cli/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,36 @@ def get_container_states(self) -> list[ContainerState]:
return [ContainerState.model_validate_json(line) for line in res.splitlines()]

def _task_start(self) -> None:
if self.state == DockerState.STOPPED:
self.state = DockerState.PULLING
for image_name in self.recipe.config.images:
_LOGGER.info(f"Pulling image: {image_name}")
pipe = self.compose_do("pull")
for line in pipe.stdout:
_LOGGER.debug(line.decode("utf-8").strip())
_LOGGER.info("Starting Docker Compose")
self.state = DockerState.STARTING
pipe = self.compose_do("up", "-d")
for image_name in self.recipe.config.images:
_LOGGER.info(f"Pulling image: {image_name}")
pipe = self.compose_do("pull")
for line in pipe.stdout:
_LOGGER.debug(line.decode("utf-8").strip())
self.state = DockerState.STARTED
_LOGGER.info("Starting Docker Compose")
self.state = DockerState.STARTING
pipe = self.compose_do("up", "-d")
for line in pipe.stdout:
_LOGGER.debug(line.decode("utf-8").strip())
self.state = DockerState.STARTED

def _task_stop(self) -> None:
if self.state == DockerState.STARTED:
_LOGGER.info("Stopping Docker Compose")
self.state = DockerState.STOPPING
pipe = self.compose_do("down")
for line in pipe.stdout:
_LOGGER.debug(line.decode("utf-8").strip())
self.state = DockerState.STOPPED
_LOGGER.info("Stopping Docker Compose")
pipe = self.compose_do("down")
for line in pipe.stdout:
_LOGGER.debug(line.decode("utf-8").strip())
self.state = DockerState.STOPPED

def start(self) -> None:
self._runner = Thread(target=self._task_start)
self._runner.start()
time.sleep(1)
if self.state == DockerState.STOPPED:
self.state = DockerState.PULLING
self._runner = Thread(target=self._task_start)
self._runner.start()

def stop(self) -> None:
self._runner = Thread(target=self._task_stop)
self._runner.start()
time.sleep(1)
if self.state == DockerState.STARTED:
self.state = DockerState.STOPPING
self._runner = Thread(target=self._task_stop)
self._runner.start()

@property
def images(self) -> list[docker.models.images.Image]:
Expand Down

0 comments on commit 23a4804

Please sign in to comment.