Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,121 @@
|
|
| 1 |
-
from flask import Flask
|
| 2 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def run_cli_script():
|
| 8 |
-
"""
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
except subprocess.CalledProcessError as e:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Configure
|
| 17 |
scheduler = BackgroundScheduler()
|
| 18 |
scheduler.add_job(run_cli_script, 'interval', hours=3)
|
| 19 |
scheduler.start()
|
| 20 |
|
| 21 |
@app.route('/')
|
| 22 |
def home():
|
| 23 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if __name__ == '__main__':
|
| 26 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string
|
| 2 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 3 |
import subprocess
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import logging
|
| 6 |
+
from io import StringIO
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
# In-memory log storage
|
| 11 |
+
log_buffer = StringIO()
|
| 12 |
+
log_entries = []
|
| 13 |
+
|
| 14 |
def run_cli_script():
|
| 15 |
+
"""Execute python cli.py and capture logs"""
|
| 16 |
+
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 17 |
+
entry = {"time": timestamp, "content": []}
|
| 18 |
+
|
| 19 |
try:
|
| 20 |
+
entry["content"].append(f"{timestamp} - Starting script execution")
|
| 21 |
+
result = subprocess.run(
|
| 22 |
+
["python", "cli.py"],
|
| 23 |
+
check=True,
|
| 24 |
+
capture_output=True,
|
| 25 |
+
text=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Capture stdout
|
| 29 |
+
if result.stdout:
|
| 30 |
+
entry["content"].append("Output:\n" + result.stdout)
|
| 31 |
+
|
| 32 |
+
# Capture stderr
|
| 33 |
+
if result.stderr:
|
| 34 |
+
entry["content"].append("Errors:\n" + result.stderr)
|
| 35 |
+
|
| 36 |
+
entry["content"].append(f"{timestamp} - Script completed successfully")
|
| 37 |
+
|
| 38 |
except subprocess.CalledProcessError as e:
|
| 39 |
+
entry["content"].append(f"Error: {str(e)}\n" + e.stderr)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
entry["content"].append(f"Unexpected error: {str(e)}")
|
| 42 |
+
finally:
|
| 43 |
+
log_entries.append(entry)
|
| 44 |
+
# Keep only last 50 entries
|
| 45 |
+
if len(log_entries) > 50:
|
| 46 |
+
log_entries.pop(0)
|
| 47 |
|
| 48 |
+
# Configure scheduler
|
| 49 |
scheduler = BackgroundScheduler()
|
| 50 |
scheduler.add_job(run_cli_script, 'interval', hours=3)
|
| 51 |
scheduler.start()
|
| 52 |
|
| 53 |
@app.route('/')
|
| 54 |
def home():
|
| 55 |
+
return render_template_string('''
|
| 56 |
+
<!DOCTYPE html>
|
| 57 |
+
<html>
|
| 58 |
+
<head>
|
| 59 |
+
<title>Script Scheduler</title>
|
| 60 |
+
<meta http-equiv="refresh" content="5">
|
| 61 |
+
<style>
|
| 62 |
+
body { font-family: Arial, sans-serif; margin: 20px; }
|
| 63 |
+
.log-container {
|
| 64 |
+
background: #1a1a1a;
|
| 65 |
+
color: #00ff00;
|
| 66 |
+
padding: 15px;
|
| 67 |
+
border-radius: 5px;
|
| 68 |
+
white-space: pre-wrap;
|
| 69 |
+
max-width: 800px;
|
| 70 |
+
margin: 20px 0;
|
| 71 |
+
}
|
| 72 |
+
a { color: #4CAF50; text-decoration: none; }
|
| 73 |
+
</style>
|
| 74 |
+
</head>
|
| 75 |
+
<body>
|
| 76 |
+
<h1>Scheduler Status</h1>
|
| 77 |
+
<p>Python script runs every 3 hours</p>
|
| 78 |
+
<p>Next run: {{ scheduler.next_run_time if scheduler else 'N/A' }}</p>
|
| 79 |
+
<p><a href="/logs">View Live Logs</a></p>
|
| 80 |
+
</body>
|
| 81 |
+
</html>
|
| 82 |
+
''', scheduler=scheduler)
|
| 83 |
+
|
| 84 |
+
@app.route('/logs')
|
| 85 |
+
def show_logs():
|
| 86 |
+
return render_template_string('''
|
| 87 |
+
<!DOCTYPE html>
|
| 88 |
+
<html>
|
| 89 |
+
<head>
|
| 90 |
+
<title>Live Logs</title>
|
| 91 |
+
<meta http-equiv="refresh" content="5">
|
| 92 |
+
<style>
|
| 93 |
+
body { font-family: monospace; margin: 20px; }
|
| 94 |
+
.log-entry {
|
| 95 |
+
background: #1a1a1a;
|
| 96 |
+
color: #00ff00;
|
| 97 |
+
padding: 15px;
|
| 98 |
+
border-radius: 5px;
|
| 99 |
+
margin-bottom: 10px;
|
| 100 |
+
white-space: pre-wrap;
|
| 101 |
+
}
|
| 102 |
+
.timestamp { color: #888; font-size: 0.9em; }
|
| 103 |
+
</style>
|
| 104 |
+
</head>
|
| 105 |
+
<body>
|
| 106 |
+
<h1>Execution Logs</h1>
|
| 107 |
+
<a href="/">← Back to Status</a>
|
| 108 |
+
{% for entry in logs|reverse %}
|
| 109 |
+
<div class="log-entry">
|
| 110 |
+
<div class="timestamp">{{ entry.time }}</div>
|
| 111 |
+
{% for line in entry.content %}
|
| 112 |
+
{{ line }}<br>
|
| 113 |
+
{% endfor %}
|
| 114 |
+
</div>
|
| 115 |
+
{% endfor %}
|
| 116 |
+
</body>
|
| 117 |
+
</html>
|
| 118 |
+
''', logs=log_entries)
|
| 119 |
|
| 120 |
if __name__ == '__main__':
|
| 121 |
app.run(host='0.0.0.0', port=7860)
|