Spaces:
Runtime error
Runtime error
File size: 3,824 Bytes
5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a 5c6037b e96a62a |
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 112 113 114 115 116 117 118 119 120 121 |
from flask import Flask, render_template_string
from apscheduler.schedulers.background import BackgroundScheduler
import subprocess
from datetime import datetime
import logging
from io import StringIO
app = Flask(__name__)
# In-memory log storage
log_buffer = StringIO()
log_entries = []
def run_cli_script():
"""Execute python cli.py and capture logs"""
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
entry = {"time": timestamp, "content": []}
try:
entry["content"].append(f"{timestamp} - Starting script execution")
result = subprocess.run(
["python", "cli.py"],
check=True,
capture_output=True,
text=True
)
# Capture stdout
if result.stdout:
entry["content"].append("Output:\n" + result.stdout)
# Capture stderr
if result.stderr:
entry["content"].append("Errors:\n" + result.stderr)
entry["content"].append(f"{timestamp} - Script completed successfully")
except subprocess.CalledProcessError as e:
entry["content"].append(f"Error: {str(e)}\n" + e.stderr)
except Exception as e:
entry["content"].append(f"Unexpected error: {str(e)}")
finally:
log_entries.append(entry)
# Keep only last 50 entries
if len(log_entries) > 50:
log_entries.pop(0)
# Configure scheduler
scheduler = BackgroundScheduler()
scheduler.add_job(run_cli_script, 'interval', hours=3)
scheduler.start()
@app.route('/')
def home():
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Script Scheduler</title>
<meta http-equiv="refresh" content="5">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.log-container {
background: #1a1a1a;
color: #00ff00;
padding: 15px;
border-radius: 5px;
white-space: pre-wrap;
max-width: 800px;
margin: 20px 0;
}
a { color: #4CAF50; text-decoration: none; }
</style>
</head>
<body>
<h1>Scheduler Status</h1>
<p>Python script runs every 3 hours</p>
<p>Next run: {{ scheduler.next_run_time if scheduler else 'N/A' }}</p>
<p><a href="/logs">View Live Logs</a></p>
</body>
</html>
''', scheduler=scheduler)
@app.route('/logs')
def show_logs():
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Live Logs</title>
<meta http-equiv="refresh" content="5">
<style>
body { font-family: monospace; margin: 20px; }
.log-entry {
background: #1a1a1a;
color: #00ff00;
padding: 15px;
border-radius: 5px;
margin-bottom: 10px;
white-space: pre-wrap;
}
.timestamp { color: #888; font-size: 0.9em; }
</style>
</head>
<body>
<h1>Execution Logs</h1>
<a href="/">← Back to Status</a>
{% for entry in logs|reverse %}
<div class="log-entry">
<div class="timestamp">{{ entry.time }}</div>
{% for line in entry.content %}
{{ line }}<br>
{% endfor %}
</div>
{% endfor %}
</body>
</html>
''', logs=log_entries)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860) |