Audit: ThreadPoolExecutor avec parallel borne (evite saturation DB/PSMP)
This commit is contained in:
parent
48efb07b49
commit
3c4244597c
@ -181,8 +181,8 @@ async def audit_global(request: Request, db=Depends(get_db)):
|
|||||||
if not hostnames:
|
if not hostnames:
|
||||||
return RedirectResponse(url="/audit?msg=no_hosts", status_code=303)
|
return RedirectResponse(url="/audit?msg=no_hosts", status_code=303)
|
||||||
|
|
||||||
# Lancer en arrière-plan
|
# Lancer en arrière-plan avec parallelisme configure
|
||||||
job_id = start_audit_job(hostnames)
|
job_id = start_audit_job(hostnames, parallel=parallel)
|
||||||
return RedirectResponse(url=f"/audit/realtime/progress/{job_id}", status_code=303)
|
return RedirectResponse(url=f"/audit/realtime/progress/{job_id}", status_code=303)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -323,8 +323,9 @@ import time as _time
|
|||||||
_audit_jobs = {}
|
_audit_jobs = {}
|
||||||
|
|
||||||
|
|
||||||
def start_audit_job(hostnames):
|
def start_audit_job(hostnames, parallel=3):
|
||||||
"""Lance un audit en arriere-plan. Retourne le job_id."""
|
"""Lance un audit en arriere-plan avec pool de threads borne. Retourne le job_id."""
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
job_id = str(uuid.uuid4())[:8]
|
job_id = str(uuid.uuid4())[:8]
|
||||||
job = {
|
job = {
|
||||||
"id": job_id,
|
"id": job_id,
|
||||||
@ -334,19 +335,16 @@ def start_audit_job(hostnames):
|
|||||||
"servers": {},
|
"servers": {},
|
||||||
"results": [],
|
"results": [],
|
||||||
"finished": False,
|
"finished": False,
|
||||||
|
"parallel": parallel,
|
||||||
}
|
}
|
||||||
for hn in hostnames:
|
for hn in hostnames:
|
||||||
job["servers"][hn] = {"hostname": hn, "stage": "pending", "detail": "En attente", "status": None}
|
job["servers"][hn] = {"hostname": hn, "stage": "pending", "detail": "En attente", "status": None}
|
||||||
_audit_jobs[job_id] = job
|
_audit_jobs[job_id] = job
|
||||||
|
|
||||||
def _run():
|
def _run():
|
||||||
threads = []
|
with ThreadPoolExecutor(max_workers=max(1, int(parallel))) as pool:
|
||||||
for hn in hostnames:
|
for hn in hostnames:
|
||||||
t = threading.Thread(target=_audit_one, args=(job, hn.strip()), daemon=True)
|
pool.submit(_audit_one, job, hn.strip())
|
||||||
threads.append(t)
|
|
||||||
t.start()
|
|
||||||
for t in threads:
|
|
||||||
t.join()
|
|
||||||
job["finished"] = True
|
job["finished"] = True
|
||||||
job["finished_at"] = _time.time()
|
job["finished_at"] = _time.time()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user