Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,52 +3,68 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|
| 3 |
import subprocess
|
| 4 |
from datetime import datetime
|
| 5 |
import logging
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def run_cli_script():
|
| 15 |
-
"""Execute
|
| 16 |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
result = subprocess.run(
|
| 22 |
["python", "cli.py"],
|
| 23 |
-
check=True,
|
| 24 |
capture_output=True,
|
| 25 |
-
text=True
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
if result.stdout:
|
| 30 |
-
entry["content"].append("Output:\n" + result.stdout)
|
| 31 |
-
|
| 32 |
-
# Capture stderr
|
| 33 |
if result.stderr:
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
except subprocess.CalledProcessError as e:
|
| 39 |
-
entry["content"].append(f"Error: {str(e)}\n" + e.stderr)
|
| 40 |
except Exception as e:
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
finally:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if len(
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
scheduler
|
| 51 |
-
scheduler.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@app.route('/')
|
| 54 |
def home():
|
|
@@ -57,65 +73,48 @@ def home():
|
|
| 57 |
<html>
|
| 58 |
<head>
|
| 59 |
<title>Script Scheduler</title>
|
| 60 |
-
<meta http-equiv="refresh" content="
|
| 61 |
<style>
|
| 62 |
-
body { font-family: Arial, sans-serif;
|
| 63 |
-
.log-
|
| 64 |
-
background: #
|
| 65 |
-
color: #
|
| 66 |
padding: 15px;
|
| 67 |
border-radius: 5px;
|
|
|
|
| 68 |
white-space: pre-wrap;
|
| 69 |
-
max-width: 800px;
|
| 70 |
-
margin: 20px 0;
|
| 71 |
}
|
| 72 |
-
|
|
|
|
| 73 |
</style>
|
| 74 |
</head>
|
| 75 |
<body>
|
| 76 |
-
<h1>
|
| 77 |
-
<p>
|
| 78 |
-
<
|
| 79 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
</body>
|
| 81 |
</html>
|
| 82 |
-
''', scheduler=scheduler)
|
| 83 |
|
| 84 |
-
@app.route('/
|
| 85 |
-
def
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 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)
|
|
|
|
| 3 |
import subprocess
|
| 4 |
from datetime import datetime
|
| 5 |
import logging
|
| 6 |
+
import sys
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# Configure basic logging
|
| 12 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 13 |
+
app.logger.addHandler(logging.StreamHandler(sys.stdout))
|
| 14 |
+
|
| 15 |
+
# Global log storage
|
| 16 |
+
execution_logs = []
|
| 17 |
+
MAX_LOG_ENTRIES = 20
|
| 18 |
|
| 19 |
def run_cli_script():
|
| 20 |
+
"""Execute the script and store logs with timestamp"""
|
| 21 |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 22 |
+
log_entry = {
|
| 23 |
+
'time': timestamp,
|
| 24 |
+
'output': '',
|
| 25 |
+
'error': ''
|
| 26 |
+
}
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
app.logger.info(f"Starting script execution at {timestamp}")
|
| 30 |
result = subprocess.run(
|
| 31 |
["python", "cli.py"],
|
|
|
|
| 32 |
capture_output=True,
|
| 33 |
+
text=True,
|
| 34 |
+
timeout=300 # 5 minutes timeout
|
| 35 |
)
|
| 36 |
|
| 37 |
+
log_entry['output'] = result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if result.stderr:
|
| 39 |
+
log_entry['error'] = result.stderr
|
| 40 |
|
| 41 |
+
app.logger.info("Script execution completed")
|
| 42 |
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
+
error_msg = f"Execution failed: {str(e)}"
|
| 45 |
+
log_entry['error'] = error_msg
|
| 46 |
+
app.logger.error(error_msg)
|
| 47 |
+
|
| 48 |
finally:
|
| 49 |
+
# Maintain log history
|
| 50 |
+
execution_logs.append(log_entry)
|
| 51 |
+
if len(execution_logs) > MAX_LOG_ENTRIES:
|
| 52 |
+
execution_logs.pop(0)
|
| 53 |
|
| 54 |
+
# Initialize scheduler
|
| 55 |
+
def init_scheduler():
|
| 56 |
+
scheduler = BackgroundScheduler()
|
| 57 |
+
scheduler.add_job(run_cli_script, 'interval', hours=3)
|
| 58 |
+
scheduler.start()
|
| 59 |
+
# Initial immediate run for testing
|
| 60 |
+
run_cli_script()
|
| 61 |
+
return scheduler
|
| 62 |
+
|
| 63 |
+
# Start scheduler when app starts
|
| 64 |
+
try:
|
| 65 |
+
scheduler = init_scheduler()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
app.logger.error(f"Failed to initialize scheduler: {e}")
|
| 68 |
|
| 69 |
@app.route('/')
|
| 70 |
def home():
|
|
|
|
| 73 |
<html>
|
| 74 |
<head>
|
| 75 |
<title>Script Scheduler</title>
|
| 76 |
+
<meta http-equiv="refresh" content="10">
|
| 77 |
<style>
|
| 78 |
+
body { font-family: Arial, sans-serif; padding: 20px; }
|
| 79 |
+
.log-box {
|
| 80 |
+
background: #000;
|
| 81 |
+
color: #0f0;
|
| 82 |
padding: 15px;
|
| 83 |
border-radius: 5px;
|
| 84 |
+
margin-top: 20px;
|
| 85 |
white-space: pre-wrap;
|
|
|
|
|
|
|
| 86 |
}
|
| 87 |
+
.timestamp { color: #888; margin-bottom: 10px; }
|
| 88 |
+
.error { color: #ff4444; }
|
| 89 |
</style>
|
| 90 |
</head>
|
| 91 |
<body>
|
| 92 |
+
<h1>Script Scheduler</h1>
|
| 93 |
+
<p>Next run: {{ scheduler.next_run_time.strftime('%Y-%m-%d %H:%M:%S UTC') if scheduler else 'N/A' }}</p>
|
| 94 |
+
<h2>Latest Execution Logs</h2>
|
| 95 |
+
<div class="log-box">
|
| 96 |
+
{% for log in logs|reverse %}
|
| 97 |
+
<div class="timestamp">{{ log.time }}</div>
|
| 98 |
+
{% if log.output %}
|
| 99 |
+
<div class="output">{{ log.output }}</div>
|
| 100 |
+
{% endif %}
|
| 101 |
+
{% if log.error %}
|
| 102 |
+
<div class="error">{{ log.error }}</div>
|
| 103 |
+
{% endif %}
|
| 104 |
+
<hr>
|
| 105 |
+
{% else %}
|
| 106 |
+
<div>No logs available yet</div>
|
| 107 |
+
{% endfor %}
|
| 108 |
+
</div>
|
| 109 |
</body>
|
| 110 |
</html>
|
| 111 |
+
''', scheduler=scheduler, logs=execution_logs)
|
| 112 |
|
| 113 |
+
@app.route('/force-run')
|
| 114 |
+
def force_run():
|
| 115 |
+
"""Manual trigger endpoint for testing"""
|
| 116 |
+
run_cli_script()
|
| 117 |
+
return "Script executed manually", 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if __name__ == '__main__':
|
| 120 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|