Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,72 +2,49 @@ 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 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
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 +67,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 +83,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 with a named job
|
31 |
+
scheduler = BackgroundScheduler(daemon=True)
|
32 |
+
scheduler.add_job(
|
33 |
+
run_cli_script,
|
34 |
+
'interval',
|
35 |
+
hours=3,
|
36 |
+
id='main_job',
|
37 |
+
next_run_time=datetime.now() # Initial run time
|
38 |
+
)
|
39 |
+
scheduler.start()
|
40 |
+
run_cli_script() # Initial run
|
|
|
|
|
|
|
41 |
|
42 |
@app.route('/')
|
43 |
def home():
|
44 |
+
# Get the specific job by ID
|
45 |
+
job = scheduler.get_job('main_job')
|
46 |
+
next_run = job.next_run_time.strftime('%Y-%m-%d %H:%M:%S UTC') if job else 'N/A'
|
47 |
+
|
48 |
return render_template_string('''
|
49 |
<!DOCTYPE html>
|
50 |
<html>
|
|
|
67 |
</head>
|
68 |
<body>
|
69 |
<h1>Script Scheduler</h1>
|
70 |
+
<p>Next run: {{ next_run }}</p>
|
71 |
<h2>Latest Execution Logs</h2>
|
72 |
<div class="log-box">
|
73 |
{% for log in logs|reverse %}
|
|
|
83 |
<div>No logs available yet</div>
|
84 |
{% endfor %}
|
85 |
</div>
|
86 |
+
<p><a href="/force-run">Trigger Manual Run</a></p>
|
87 |
</body>
|
88 |
</html>
|
89 |
+
''', next_run=next_run, logs=execution_logs)
|
90 |
|
91 |
@app.route('/force-run')
|
92 |
def force_run():
|
|
|
93 |
run_cli_script()
|
94 |
return "Script executed manually", 200
|
95 |
|
96 |
if __name__ == '__main__':
|
97 |
+
app.run(host='0.0.0.0', port=7860)
|