Spaces:
Runtime error
Runtime error
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() | |
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) | |
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) |