File size: 2,347 Bytes
0bee3d6
0ca4a58
5069383
0ca4a58
1b43f13
dd52df4
5069383
9ebfeae
5069383
 
 
 
 
670e9bc
 
5069383
 
 
dd52df4
1b43f13
 
 
b8619bc
1b43f13
 
 
4fef6ae
1b43f13
 
 
 
 
 
 
 
0ca4a58
9ebfeae
1b43f13
 
5069383
 
 
1b43f13
02dde9f
1b43f13
dd52df4
1b43f13
0ca4a58
9ebfeae
1b43f13
9ebfeae
1b43f13
02dde9f
1b43f13
02dde9f
1b43f13
9ebfeae
1b43f13
0ca4a58
1b43f13
5069383
 
 
 
 
 
 
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
import streamlit as st
import subprocess
import threading
import os
import pymongo

# Track process and logs
process = None
log_lines = []

# Function to handle real-time log output
def read_logs(process):
    global log_lines
    for line in iter(process.stdout.readline, ""):
        log_lines.append(line.strip())  # Append the string directly
        if len(log_lines) > 100:  # Keep only the last 100 lines
            log_lines.pop(0)
        st.experimental_rerun()  # Rerun the Streamlit app to refresh logs

# Title
st.title("Powers Control Panel ⚡")
st.write("Use the buttons below to **start** or **stop** the `powers` Python process.")

# MongoDB Connection Test
mongo_uri = os.getenv("MONGO_URI")
st.write(f"Connecting to MongoDB with URI: {mongo_uri}")

try:
    client = pymongo.MongoClient(mongo_uri)
    client.admin.command('ping')  # Test the connection
    st.success("Connected to MongoDB successfully!")
except Exception as e:
    st.error(f"Failed to connect to MongoDB: {e}")

# Start button: Installs dependencies and runs `powers`
if st.button("Start Powers"):
    if process is None or process.poll() is not None:
        with st.spinner("Installing dependencies..."):
            subprocess.run(["pip3", "install", "-U", "-r", "requirements.txt"])
        # Start the `powers` process and read logs in a separate thread
        process = subprocess.Popen(["python3", "-m", "powers"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        threading.Thread(target=read_logs, args=(process,), daemon=True).start()
        st.success("`powers` process started!")
    else:
        st.warning("`powers` is already running!")

# Stop button: Terminates the `powers` process
if st.button("Stop Powers"):
    if process is not None:
        process.terminate()  # Stop the process
        process = None
        st.success("`powers` process stopped!")
    else:
        st.warning("No running process to stop.")

# Display the status of the `powers` process
if process and process.poll() is None:
    st.write("`powers` is **running**! 🚀")
else:
    st.write("`powers` is **not running**.")

# Display real-time logs
st.subheader("Real-Time Logs 📜")
if log_lines:
    st.text_area("Logs", "\n".join(log_lines), height=300)
else:
    st.write("No logs yet. Logs will appear here when `powers` is running.")