Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,72 +2,42 @@ 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 |
-
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
|
| 35 |
)
|
| 36 |
-
|
| 37 |
log_entry['output'] = result.stdout
|
| 38 |
-
|
| 39 |
-
log_entry['error'] = result.stderr
|
| 40 |
-
|
| 41 |
-
app.logger.info("Script execution completed")
|
| 42 |
-
|
| 43 |
except Exception as e:
|
| 44 |
-
|
| 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 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 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():
|
|
|
|
|
|
|
|
|
|
| 71 |
return render_template_string('''
|
| 72 |
<!DOCTYPE html>
|
| 73 |
<html>
|
|
@@ -90,7 +60,7 @@ def home():
|
|
| 90 |
</head>
|
| 91 |
<body>
|
| 92 |
<h1>Script Scheduler</h1>
|
| 93 |
-
<p>Next run: {{
|
| 94 |
<h2>Latest Execution Logs</h2>
|
| 95 |
<div class="log-box">
|
| 96 |
{% for log in logs|reverse %}
|
|
@@ -106,15 +76,15 @@ def home():
|
|
| 106 |
<div>No logs available yet</div>
|
| 107 |
{% endfor %}
|
| 108 |
</div>
|
|
|
|
| 109 |
</body>
|
| 110 |
</html>
|
| 111 |
-
''',
|
| 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
|
|
|
|
| 2 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 3 |
import subprocess
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
execution_logs = []
|
| 8 |
MAX_LOG_ENTRIES = 20
|
| 9 |
|
| 10 |
def run_cli_script():
|
|
|
|
| 11 |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 12 |
+
log_entry = {'time': timestamp, 'output': '', 'error': ''}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
|
|
|
| 15 |
result = subprocess.run(
|
| 16 |
["python", "cli.py"],
|
| 17 |
capture_output=True,
|
| 18 |
text=True,
|
| 19 |
+
timeout=300
|
| 20 |
)
|
|
|
|
| 21 |
log_entry['output'] = result.stdout
|
| 22 |
+
log_entry['error'] = result.stderr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
+
log_entry['error'] = str(e)
|
|
|
|
|
|
|
|
|
|
| 25 |
finally:
|
|
|
|
| 26 |
execution_logs.append(log_entry)
|
| 27 |
if len(execution_logs) > MAX_LOG_ENTRIES:
|
| 28 |
execution_logs.pop(0)
|
| 29 |
|
| 30 |
+
# Initialize scheduler properly
|
| 31 |
+
scheduler = BackgroundScheduler(daemon=True)
|
| 32 |
+
scheduler.add_job(run_cli_script, 'interval', hours=3, id='main_job')
|
| 33 |
+
scheduler.start()
|
| 34 |
+
run_cli_script() # Initial run
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
@app.route('/')
|
| 37 |
def home():
|
| 38 |
+
job = scheduler.get_job('main_job')
|
| 39 |
+
next_run = job.next_run_time.strftime('%Y-%m-%d %H:%M:%S UTC') if job else 'N/A'
|
| 40 |
+
|
| 41 |
return render_template_string('''
|
| 42 |
<!DOCTYPE html>
|
| 43 |
<html>
|
|
|
|
| 60 |
</head>
|
| 61 |
<body>
|
| 62 |
<h1>Script Scheduler</h1>
|
| 63 |
+
<p>Next run: {{ next_run }}</p>
|
| 64 |
<h2>Latest Execution Logs</h2>
|
| 65 |
<div class="log-box">
|
| 66 |
{% for log in logs|reverse %}
|
|
|
|
| 76 |
<div>No logs available yet</div>
|
| 77 |
{% endfor %}
|
| 78 |
</div>
|
| 79 |
+
<p><a href="/force-run">Trigger Manual Run</a></p>
|
| 80 |
</body>
|
| 81 |
</html>
|
| 82 |
+
''', next_run=next_run, logs=execution_logs)
|
| 83 |
|
| 84 |
@app.route('/force-run')
|
| 85 |
def force_run():
|
|
|
|
| 86 |
run_cli_script()
|
| 87 |
return "Script executed manually", 200
|
| 88 |
|
| 89 |
if __name__ == '__main__':
|
| 90 |
+
app.run(host='0.0.0.0', port=7860)
|