This code suggested by google AI - to fix the bug that if "squeue" fails on the cluster, THEN the thread may fail - and THEN the generated reports won't show correct statuses.
import threading
import time
from flask import Flask, current_app
app = Flask(name)
def worker_supervisor(task_func, *args, **kwargs):
"""Supervises a task and relaunches it if it fails."""
while True:
try:
# Execute the actual background logic
task_func(*args, **kwargs)
except Exception as e:
# Log the error using Flask's logger or standard logging
print(f"Thread crashed: {e}. Relaunching in 5s...")
time.sleep(5) # Backoff to prevent rapid-fire restart loops
def my_background_task():
"""Your actual business logic."""
while True:
# Do work...
time.sleep(1)
# Potential crash point
raise RuntimeError("Something went wrong!")
@app.before_first_request
def start_threads():
thread = threading.Thread(
target=worker_supervisor,
args=(my_background_task,),
daemon=True
)
thread.start()
This code suggested by google AI - to fix the bug that if "squeue" fails on the cluster, THEN the thread may fail - and THEN the generated reports won't show correct statuses.
import threading
import time
from flask import Flask, current_app
app = Flask(name)
def worker_supervisor(task_func, *args, **kwargs):
"""Supervises a task and relaunches it if it fails."""
while True:
try:
# Execute the actual background logic
task_func(*args, **kwargs)
except Exception as e:
# Log the error using Flask's logger or standard logging
print(f"Thread crashed: {e}. Relaunching in 5s...")
time.sleep(5) # Backoff to prevent rapid-fire restart loops
def my_background_task():
"""Your actual business logic."""
while True:
# Do work...
time.sleep(1)
# Potential crash point
raise RuntimeError("Something went wrong!")
@app.before_first_request
def start_threads():
thread = threading.Thread(
target=worker_supervisor,
args=(my_background_task,),
daemon=True
)
thread.start()