Stop after 3 successful deliveries instead of time-based expiry
Replaces the 7-day timer with a success counter. Cron auto-removes itself once 3 faxes are confirmed COMPLETED. ntfy notifications include delivery progress (e.g. "Delivered: 2/3"). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
521e7b03e0
commit
f13e2036a5
1 changed files with 31 additions and 17 deletions
48
autofax.py
48
autofax.py
|
|
@ -235,31 +235,37 @@ def update_previous_statuses(entries: list[dict]) -> list[dict]:
|
|||
return entries
|
||||
|
||||
|
||||
def check_expiry():
|
||||
"""If past the expiry date, remove cron and exit."""
|
||||
expires_file = config.REPORTS_DIR / ".expires"
|
||||
if not expires_file.exists():
|
||||
return
|
||||
try:
|
||||
expiry = datetime.fromisoformat(expires_file.read_text().strip())
|
||||
if datetime.now() >= expiry:
|
||||
print("AutoFax expired. Removing cron job.")
|
||||
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True)
|
||||
lines = [l for l in result.stdout.splitlines() if "autofax" not in l]
|
||||
subprocess.run(["crontab", "-"], input="\n".join(lines) + "\n", text=True)
|
||||
notify("AutoFax Expired", "7-day schedule complete. Cron job removed.")
|
||||
sys.exit(0)
|
||||
except Exception:
|
||||
pass
|
||||
REQUIRED_SUCCESSES = 3
|
||||
|
||||
|
||||
def count_successes(entries: list[dict]) -> int:
|
||||
return sum(1 for e in entries if e["status"] == "COMPLETED")
|
||||
|
||||
|
||||
def check_done(entries: list[dict]):
|
||||
"""If we have enough successful deliveries, remove cron and exit."""
|
||||
successes = count_successes(entries)
|
||||
if successes >= REQUIRED_SUCCESSES:
|
||||
print(f"AutoFax complete: {successes} successful deliveries. Removing cron job.")
|
||||
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True)
|
||||
lines = [l for l in result.stdout.splitlines() if "autofax" not in l]
|
||||
subprocess.run(["crontab", "-"], input="\n".join(lines) + "\n", text=True)
|
||||
notify(
|
||||
"AutoFax Complete",
|
||||
f"{successes} faxes delivered successfully to {entries[-1].get('to', 'N/A')}. Cron job removed.",
|
||||
)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def main():
|
||||
check_expiry()
|
||||
pdf_path = find_claim_pdf()
|
||||
|
||||
entries = load_log()
|
||||
entries = update_previous_statuses(entries)
|
||||
|
||||
# Check if we already hit the target after updating statuses
|
||||
check_done(entries)
|
||||
|
||||
print(f"Sending fax: {pdf_path.name} -> {config.FAX_TO_NUMBER}")
|
||||
result = send_fax(pdf_path)
|
||||
entries.append(result)
|
||||
|
|
@ -277,6 +283,7 @@ def main():
|
|||
report_path = generate_report(entries)
|
||||
|
||||
# Notify via ntfy
|
||||
successes = count_successes(entries)
|
||||
status_labels = {
|
||||
"COMPLETED": "Fax Delivered",
|
||||
"IN_PROGRESS": "Fax Sending",
|
||||
|
|
@ -284,14 +291,21 @@ def main():
|
|||
}
|
||||
subject = status_labels.get(result["status"], "Fax Failed")
|
||||
msg = f"To: {result['to']}\nStatus: {result['status']}\nFile: {result['file']}"
|
||||
msg += f"\nDelivered: {successes}/{REQUIRED_SUCCESSES}"
|
||||
if result.get("error"):
|
||||
msg += f"\nError: {result['error']}"
|
||||
priority = "high" if result["status"] not in ("QUEUED", "IN_PROGRESS", "COMPLETED") else "default"
|
||||
notify(subject, msg, priority)
|
||||
|
||||
print(f"Status: {result['status']}")
|
||||
print(f"Delivered: {successes}/{REQUIRED_SUCCESSES}")
|
||||
print(f"Report: {report_path}")
|
||||
|
||||
# Check if this delivery hit the target
|
||||
save_log(entries)
|
||||
generate_report(entries)
|
||||
check_done(entries)
|
||||
|
||||
if result["status"] == "send_failed":
|
||||
sys.exit(1)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue