Skip to content

Wrap vustruct_flask squeue thread using a paradigm that logs exceptions and restarts #134

@ChrisMoth

Description

@ChrisMoth

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions