++ fix
This commit is contained in:
45
app/main.py
45
app/main.py
@@ -51,6 +51,10 @@ class JobState:
|
||||
last_status: str = "never"
|
||||
last_summary: dict[str, Any] = field(default_factory=dict)
|
||||
running: bool = False
|
||||
current_file: str | None = None
|
||||
files_total: int = 0
|
||||
files_done: int = 0
|
||||
phase: str = ""
|
||||
|
||||
|
||||
job_configs: list[JobConfig] = []
|
||||
@@ -279,6 +283,10 @@ def sync_job(job: JobConfig, trigger: str = "scheduled") -> None:
|
||||
state = job_states[job.name]
|
||||
state.running = True
|
||||
state.last_start = started_at
|
||||
state.current_file = None
|
||||
state.files_total = 0
|
||||
state.files_done = 0
|
||||
state.phase = "elencazione file"
|
||||
|
||||
notify_gotify(
|
||||
title=f"Sync started: {job.name}",
|
||||
@@ -299,6 +307,10 @@ def sync_job(job: JobConfig, trigger: str = "scheduled") -> None:
|
||||
paginator = s3_client.get_paginator("list_objects_v2")
|
||||
|
||||
expected_rel_paths: set[str] = set()
|
||||
pending_downloads: list[tuple[str, str, Path, int, float]] = []
|
||||
|
||||
with status_lock:
|
||||
job_states[job.name].phase = "elencazione file"
|
||||
|
||||
for page in paginator.paginate(Bucket=job.bucket, Prefix=job.prefix):
|
||||
contents = page.get("Contents", [])
|
||||
@@ -324,13 +336,28 @@ def sync_job(job: JobConfig, trigger: str = "scheduled") -> None:
|
||||
s3_ts = obj["LastModified"].timestamp()
|
||||
|
||||
if should_download(local_path, s3_size, s3_ts):
|
||||
s3_client.download_file(job.bucket, key, str(local_path))
|
||||
os.utime(local_path, (s3_ts, s3_ts))
|
||||
downloaded += 1
|
||||
pending_downloads.append((key, rel_key, local_path, s3_size, s3_ts))
|
||||
else:
|
||||
kept += 1
|
||||
|
||||
with status_lock:
|
||||
job_states[job.name].files_total = len(pending_downloads)
|
||||
job_states[job.name].files_done = 0
|
||||
job_states[job.name].phase = "download"
|
||||
|
||||
for key, rel_key, local_path, s3_size, s3_ts in pending_downloads:
|
||||
with status_lock:
|
||||
job_states[job.name].current_file = rel_key
|
||||
s3_client.download_file(job.bucket, key, str(local_path))
|
||||
os.utime(local_path, (s3_ts, s3_ts))
|
||||
downloaded += 1
|
||||
with status_lock:
|
||||
job_states[job.name].files_done = downloaded
|
||||
|
||||
if job.delete_local_extras:
|
||||
with status_lock:
|
||||
job_states[job.name].phase = "pulizia file extra"
|
||||
job_states[job.name].current_file = None
|
||||
for file_path in local_dir.rglob("*"):
|
||||
if not file_path.is_file():
|
||||
continue
|
||||
@@ -354,8 +381,12 @@ def sync_job(job: JobConfig, trigger: str = "scheduled") -> None:
|
||||
state.last_end = ended_at
|
||||
state.last_status = "success"
|
||||
state.last_summary = summary
|
||||
state.current_file = None
|
||||
state.files_total = 0
|
||||
state.files_done = 0
|
||||
state.phase = ""
|
||||
|
||||
add_log("info", "Sync completed", job.name, summary)
|
||||
add_log("info", "Sync completata", job.name, summary)
|
||||
notify_gotify(
|
||||
title=f"Sync completed: {job.name}",
|
||||
message=(
|
||||
@@ -383,8 +414,12 @@ def sync_job(job: JobConfig, trigger: str = "scheduled") -> None:
|
||||
state.last_end = ended_at
|
||||
state.last_status = "failed"
|
||||
state.last_summary = summary
|
||||
state.current_file = None
|
||||
state.files_total = 0
|
||||
state.files_done = 0
|
||||
state.phase = ""
|
||||
|
||||
add_log("error", "Sync failed", job.name, {"error": str(exc)})
|
||||
add_log("error", "Sync fallita", job.name, {"error": str(exc)})
|
||||
notify_gotify(
|
||||
title=f"Sync failed: {job.name}",
|
||||
message=f"error: {exc}",
|
||||
|
||||
Reference in New Issue
Block a user