Skip to content

Commit

Permalink
Merge pull request #22 from Never-Over/bridge-stop
Browse files Browse the repository at this point in the history
bridge stop
  • Loading branch information
emdoyle committed Apr 16, 2024
2 parents ba020d2 + 3222d20 commit 2ff66ca
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
8 changes: 7 additions & 1 deletion bridge/cli/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from bridge.cli.db import open_database_shell
from bridge.cli.init import initialize
from bridge.cli.redis import open_redis_shell
from bridge.cli.stop import stop
from bridge.config import get_config
from bridge.framework import Framework

Expand All @@ -19,6 +20,9 @@ def main():
parser.add_argument("--version", action="version", version="%(prog)s 0.0.24")
subparsers = parser.add_subparsers(dest="command")

# Parser for 'stop'
subparsers.add_parser("stop", help="Stop all running local services")

# Parser for 'init' command
init_parser = subparsers.add_parser(
"init", help="Initialize configuration for a given platform (Render, Heroku)"
Expand All @@ -44,7 +48,9 @@ def main():
framework = detect_framework()
bridge_config = get_config()

if args.command == "init":
if args.command == "stop":
stop()
elif args.command == "init":
initialize(
framework=framework,
platform=args.init_platform,
Expand Down
39 changes: 39 additions & 0 deletions bridge/cli/stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import signal
from typing import cast

import docker
import psutil
from docker.errors import NotFound
from docker.models.containers import Container

from bridge.console import log_task
from bridge.utils.filesystem import resolve_dot_bridge


def stop():
with log_task("Stopping bridge services...", "All bridge services stopped"):
bridge_path = resolve_dot_bridge()
# Docker - postgres, redis
cid_path = bridge_path / "cid"
if os.path.exists(cid_path):
docker_client = docker.from_env()
with open(cid_path) as f:
for cid in f.readlines():
cid = cid.strip()
try:
container = docker_client.containers.get(cid)
container = cast(Container, container)
container.stop()
except NotFound:
pass
os.remove(cid_path)
# Processes - celery, flower
for proc in psutil.process_iter(["pid", "name", "cmdline"]):
try:
# Check if the name fragment is in the command line; this field is a list
proc_name = proc.info["cmdline"]
if proc_name and "bridge.service.django_celery" in proc_name:
proc.send_signal(signal.SIGTERM)
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
12 changes: 11 additions & 1 deletion bridge/service/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rich.console import Console

from bridge.console import log_error, log_task
from bridge.utils.filesystem import resolve_dot_bridge
from bridge.utils.pydantic import Empty

if TYPE_CHECKING:
Expand Down Expand Up @@ -49,6 +50,7 @@ class DockerService(ABC, Generic[T_ContainerConfig]):
def __init__(self, client: docker.DockerClient, config: T_ContainerConfig) -> None:
self.client = client
self.config = config
self.container_id = None
# todo add self.container - should we start or fetch the container on startup?

def start(self):
Expand All @@ -60,10 +62,16 @@ def start(self):
self.pull_image()
self.start_container()
self.ensure_ready()
self.register()
console.print(
f"[bold bright_green]Service [white]{self.config.name}[/white] started!"
)

def register(self):
bridge_cid_path = resolve_dot_bridge() / "cid"
with open(bridge_cid_path, "a") as f:
f.write(f"{self.container_id}\n")

def pull_image(self):
with log_task(
start_message=f"Pulling [white]{self.config.image}",
Expand All @@ -87,10 +95,12 @@ def start_container(self):
if container.status in ["paused", "exited"]:
container.restart()
else:
self.client.containers.run(
container = self.client.containers.run(
**self.config.model_dump(),
detach=True,
)
container = cast(Container, container)
self.container_id = container.id

@abstractmethod
def ensure_ready(self) -> None:
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "python-bridge"
version = "0.0.24"
version = "0.0.25"
authors = [
{ name="Caelean Barnes", email="caeleanb@gmail.com" },
{ name="Evan Doyle", email="evanmdoyle@gmail.com" },
Expand Down Expand Up @@ -32,7 +32,8 @@ dependencies = [
"rich==13.7.1",
"pydantic==2.6.4",
"pyyaml==6.0.1",
"dj-database-url==2.1.0"
"dj-database-url==2.1.0",
"psutil==5.9.8",
]

[project.urls]
Expand Down

0 comments on commit 2ff66ca

Please sign in to comment.