-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop_api.py
More file actions
111 lines (95 loc) · 4.62 KB
/
stop_api.py
File metadata and controls
111 lines (95 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
API Stopper - Stop the CFDI Dashboard API
Run this from anywhere: python stop_api.py
"""
import subprocess
import sys
import platform
def stop_api():
"""Stop the running API server."""
print(" Stopping CFDI Dashboard API Server")
print("=" * 50)
try:
system = platform.system()
if system == "Windows":
# Find Python processes running uvicorn or run_api.py
cmd = ['tasklist', '/fi', 'imagename eq python.exe', '/fo', 'csv']
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
# Parse the output to find API processes
lines = result.stdout.strip().split('\n')
api_pids = []
for line in lines[1:]: # Skip header
if 'python.exe' in line:
parts = line.split(',')
if len(parts) >= 2:
pid = parts[1].strip('"')
# Check if this is our API process
try:
detail_cmd = ['tasklist', '/fi', f'pid eq {pid}', '/v', '/fo', 'csv']
detail_result = subprocess.run(detail_cmd, capture_output=True, text=True)
if 'uvicorn' in detail_result.stdout or 'run_api' in detail_result.stdout:
api_pids.append(pid)
except:
pass
if api_pids:
for pid in api_pids:
try:
subprocess.run(['taskkill', '/f', '/pid', pid],
check=True, capture_output=True)
print(f" Stopped API process (PID: {pid})")
except subprocess.CalledProcessError:
print(f" Could not stop process {pid}")
else:
print(" No running API processes found")
else:
print(" Could not check running processes")
else:
# Unix-like systems (Linux, macOS)
try:
# Find processes running uvicorn or containing run_api
cmd = ['pgrep', '-f', '(uvicorn|run_api)']
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
pids = result.stdout.strip().split('\n')
for pid in pids:
if pid:
try:
subprocess.run(['kill', '-TERM', pid], check=True)
print(f" Stopped API process (PID: {pid})")
except subprocess.CalledProcessError:
print(f" Could not stop process {pid}")
else:
print(" No running API processes found")
except FileNotFoundError:
print(" pgrep command not found. Trying alternative method...")
# Alternative: use ps and grep
try:
cmd = ['ps', 'aux']
ps_result = subprocess.run(cmd, capture_output=True, text=True)
if ps_result.returncode == 0:
lines = ps_result.stdout.split('\n')
for line in lines:
if 'uvicorn' in line or 'run_api' in line:
parts = line.split()
if len(parts) >= 2:
pid = parts[1]
try:
subprocess.run(['kill', '-TERM', pid], check=True)
print(f" Stopped API process (PID: {pid})")
except subprocess.CalledProcessError:
print(f" Could not stop process {pid}")
else:
print(" Could not list processes")
except FileNotFoundError:
print(" Could not find process management tools")
except Exception as e:
print(f" Error stopping API: {e}")
return
print("=" * 50)
print(" API stop completed")
print(" To start again, run: python run_api.py")
if __name__ == "__main__":
stop_api()