Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,58 +3,70 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|
| 3 |
import subprocess
|
| 4 |
import threading
|
| 5 |
import pytz
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
|
|
|
| 8 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
execution_logs = []
|
| 10 |
-
MAX_LOG_ENTRIES =
|
|
|
|
| 11 |
|
| 12 |
-
def run_cli_script():
|
| 13 |
-
"""Runs cli.py and streams logs in real-time to both UI and terminal."""
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
utc_now = datetime.utcnow()
|
| 17 |
ist_timezone = pytz.timezone("Asia/Kolkata")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
log_entry = {'time': timestamp, 'output': '', 'error': ''}
|
| 25 |
|
| 26 |
try:
|
| 27 |
process = subprocess.Popen(
|
| 28 |
["python", "cli.py"],
|
| 29 |
stdout=subprocess.PIPE,
|
| 30 |
stderr=subprocess.PIPE,
|
| 31 |
-
text=True
|
| 32 |
-
bufsize=1
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
log_entry['error'] += line
|
| 45 |
-
execution_logs.append({'time': timestamp, 'output': '', 'error': line})
|
| 46 |
-
print(line, end="") # ✅ Print errors to terminal
|
| 47 |
-
if len(execution_logs) > MAX_LOG_ENTRIES:
|
| 48 |
-
execution_logs.pop(0)
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
def start_initial_run():
|
|
|
|
| 56 |
threading.Thread(target=run_cli_script, daemon=True).start()
|
| 57 |
|
|
|
|
|
|
|
| 58 |
scheduler = BackgroundScheduler(daemon=True)
|
| 59 |
scheduler.add_job(
|
| 60 |
run_cli_script,
|
|
@@ -65,17 +77,15 @@ scheduler.add_job(
|
|
| 65 |
)
|
| 66 |
scheduler.start()
|
| 67 |
|
|
|
|
| 68 |
start_initial_run()
|
| 69 |
|
|
|
|
| 70 |
@app.route('/')
|
| 71 |
def home():
|
| 72 |
"""Main UI displaying logs and next run time."""
|
| 73 |
job = scheduler.get_job('main_job')
|
| 74 |
-
if job
|
| 75 |
-
ist_time = job.next_run_time.astimezone(pytz.timezone("Asia/Kolkata"))
|
| 76 |
-
next_run = ist_time.strftime('%Y-%m-%d %H:%M:%S IST')
|
| 77 |
-
else:
|
| 78 |
-
next_run = 'N/A'
|
| 79 |
|
| 80 |
return render_template_string('''
|
| 81 |
<!DOCTYPE html>
|
|
@@ -129,24 +139,30 @@ def home():
|
|
| 129 |
</html>
|
| 130 |
''', next_run=next_run)
|
| 131 |
|
|
|
|
| 132 |
@app.route('/logs')
|
| 133 |
def logs():
|
| 134 |
"""Returns logs as JSON for AJAX polling."""
|
| 135 |
return jsonify({'logs': execution_logs})
|
| 136 |
|
|
|
|
| 137 |
@app.route('/force-run')
|
| 138 |
def force_run():
|
| 139 |
"""Manually trigger the script execution."""
|
| 140 |
threading.Thread(target=run_cli_script, daemon=True).start()
|
|
|
|
| 141 |
return "Script executed manually", 200
|
| 142 |
|
|
|
|
| 143 |
@app.route('/run-check')
|
| 144 |
def run_check():
|
| 145 |
-
"""Check if the scheduler is still running."""
|
| 146 |
if not scheduler.running:
|
| 147 |
-
|
| 148 |
scheduler.start()
|
|
|
|
| 149 |
return "Scheduler is running", 200
|
| 150 |
|
|
|
|
| 151 |
if __name__ == '__main__':
|
| 152 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import threading
|
| 5 |
import pytz
|
| 6 |
+
import logging
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
+
# Initialize Flask app
|
| 10 |
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Setup logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 14 |
+
|
| 15 |
+
# Execution logs
|
| 16 |
execution_logs = []
|
| 17 |
+
MAX_LOG_ENTRIES = 50
|
| 18 |
+
log_lock = threading.Lock() # Prevents race conditions when modifying logs
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def add_log_entry(entry):
|
| 22 |
+
"""Safely add log entries while maintaining MAX_LOG_ENTRIES limit."""
|
| 23 |
+
with log_lock:
|
| 24 |
+
execution_logs.append(entry)
|
| 25 |
+
if len(execution_logs) > MAX_LOG_ENTRIES:
|
| 26 |
+
execution_logs.pop(0)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_ist_time():
|
| 30 |
+
"""Get the current time in IST (Indian Standard Time)."""
|
| 31 |
utc_now = datetime.utcnow()
|
| 32 |
ist_timezone = pytz.timezone("Asia/Kolkata")
|
| 33 |
+
return utc_now.replace(tzinfo=pytz.utc).astimezone(ist_timezone)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def run_cli_script():
|
| 37 |
+
"""Runs cli.py and streams logs in real-time to both UI and terminal."""
|
| 38 |
+
timestamp = get_ist_time().strftime("%Y-%m-%d %H:%M:%S IST")
|
|
|
|
| 39 |
|
| 40 |
try:
|
| 41 |
process = subprocess.Popen(
|
| 42 |
["python", "cli.py"],
|
| 43 |
stdout=subprocess.PIPE,
|
| 44 |
stderr=subprocess.PIPE,
|
| 45 |
+
text=True
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
+
stdout, stderr = process.communicate() # Wait for process to complete
|
| 49 |
+
|
| 50 |
+
if stdout:
|
| 51 |
+
add_log_entry({'time': timestamp, 'output': stdout, 'error': ''})
|
| 52 |
+
logging.info(stdout.strip())
|
| 53 |
+
|
| 54 |
+
if stderr:
|
| 55 |
+
add_log_entry({'time': timestamp, 'output': '', 'error': stderr})
|
| 56 |
+
logging.error(stderr.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
error_msg = str(e)
|
| 60 |
+
add_log_entry({'time': timestamp, 'output': '', 'error': error_msg})
|
| 61 |
+
logging.error(f"Error: {error_msg}")
|
| 62 |
+
|
| 63 |
|
| 64 |
def start_initial_run():
|
| 65 |
+
"""Runs the CLI script immediately upon startup in a separate thread."""
|
| 66 |
threading.Thread(target=run_cli_script, daemon=True).start()
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# Initialize scheduler
|
| 70 |
scheduler = BackgroundScheduler(daemon=True)
|
| 71 |
scheduler.add_job(
|
| 72 |
run_cli_script,
|
|
|
|
| 77 |
)
|
| 78 |
scheduler.start()
|
| 79 |
|
| 80 |
+
# Ensure script runs once on startup
|
| 81 |
start_initial_run()
|
| 82 |
|
| 83 |
+
|
| 84 |
@app.route('/')
|
| 85 |
def home():
|
| 86 |
"""Main UI displaying logs and next run time."""
|
| 87 |
job = scheduler.get_job('main_job')
|
| 88 |
+
next_run = get_ist_time().strftime('%Y-%m-%d %H:%M:%S IST') if job else 'N/A'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return render_template_string('''
|
| 91 |
<!DOCTYPE html>
|
|
|
|
| 139 |
</html>
|
| 140 |
''', next_run=next_run)
|
| 141 |
|
| 142 |
+
|
| 143 |
@app.route('/logs')
|
| 144 |
def logs():
|
| 145 |
"""Returns logs as JSON for AJAX polling."""
|
| 146 |
return jsonify({'logs': execution_logs})
|
| 147 |
|
| 148 |
+
|
| 149 |
@app.route('/force-run')
|
| 150 |
def force_run():
|
| 151 |
"""Manually trigger the script execution."""
|
| 152 |
threading.Thread(target=run_cli_script, daemon=True).start()
|
| 153 |
+
logging.info("Manual script execution triggered")
|
| 154 |
return "Script executed manually", 200
|
| 155 |
|
| 156 |
+
|
| 157 |
@app.route('/run-check')
|
| 158 |
def run_check():
|
| 159 |
+
"""Check if the scheduler is still running and restart if necessary."""
|
| 160 |
if not scheduler.running:
|
| 161 |
+
logging.warning("Scheduler was stopped! Restarting...")
|
| 162 |
scheduler.start()
|
| 163 |
+
return "Scheduler restarted", 200
|
| 164 |
return "Scheduler is running", 200
|
| 165 |
|
| 166 |
+
|
| 167 |
if __name__ == '__main__':
|
| 168 |
app.run(host='0.0.0.0', port=7860)
|