From 1c05bdb04b719474964ca5996b13907d5d5eb7c1 Mon Sep 17 00:00:00 2001 From: dnzbk Date: Fri, 2 May 2025 08:52:07 +0300 Subject: [PATCH] Fix: corrupted .lock file handling --- main.py | 31 +++++++++++++++++++++++++++---- manifest.json | 30 ++++++++++++++++++------------ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 857da8b..93a615a 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ # Completion.py script for NZBGet # # Copyright (C) 2014-2017 kloaknet. -# Copyright (C) 2024 Denis +# Copyright (C) 2024-2025 Denis # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,7 +20,7 @@ import os -import urllib.request, urllib.error, urllib.parse +import urllib.request import base64 import json import time @@ -28,7 +28,7 @@ import socket import ssl import traceback -import html.parser +import html import errno from xmlrpc.client import ServerProxy from operator import itemgetter @@ -1531,6 +1531,24 @@ def check_failure_status(rar_msg_ids, failed_limit, nzb_age): return failed_ratio +def handle_corrupted_lock_file(f_name, server_time): + print(f"[WARNING] Corrupted lock file found at {f_name}. Attempting to recreate.") + try: + os.remove(f_name) + except OSError as e: + print(f"[ERROR] Failed to remove corrupted lock file, reason {e}") + return True + try: + with open(f_name, encoding="utf-8", mode="w") as fd: + fd.write(str(server_time)) + print(f"[INFO] New completion.lock file created.") + return False + + except OSError as e: + print(f"[ERROR] recreating lock file after corruption: {e}") + return True + + def lock_file(): """ This function checks if the .lock file is there, if it is created @@ -1556,7 +1574,12 @@ def lock_file(): file_exists = os.path.isfile(f_name) if file_exists: fd = open(f_name, encoding="utf-8") - time_stamp = int(fd.readline()) + time_stamp = 0 + try: + time_stamp = int(fd.readline()) + except ValueError: + return handle_corrupted_lock_file(f_name, server_time) + if VERBOSE: print( "[V] time_stamp from completion.lock file= " + str(time_stamp) diff --git a/manifest.json b/manifest.json index 600e65e..7f85423 100644 --- a/manifest.json +++ b/manifest.json @@ -11,21 +11,27 @@ "about": "Verifies that enough articles are available before starting the download.", "queueEvents": "NZB_ADDED, NZB_DOWNLOADED, NZB_DELETED, NZB_MARKED", "requirements": [ - "This script requires Python 3.8.0 and above to be installed on your system." + "Python 3.8.0 or higher is required to be installed on your system." ], "description": [ - "For NZBGet versions 18+, this script needs to be:", - "- listed in EXTENSION SCRIPTS under Extensions.", - "- listed in CATEGORIES for each CategoryX.Extension if not left empty.", - "- optionally the user can add it as scheduler script to manually specify", - "different scheduler intervals instead of checking each 15 minutes by default.", - "For NZBGet versions prior to 18, this script should be added as:", - "- Scan script: to pause incoming NZBs.", - "- Queue script: to check newly added, and by the script paused NZBs, and resume when OK.", - "- Scheduler script: to regularly check completeness of by the script paused NZBs in the queue.", - "- listed in CATEGORIES for each CategoryX.Extension if not left empty.", + "The extension checks if the data in the NZB file is sufficiently complete at your usenet provider(s),", + "before starting the download.", + "If incomplete it would wait for a certain period and check the completion of the NZB file again.", + "This check is done by requesting the header STAT/HEAD,", + "and is in normal cases done within seconds (like 1 - 5 sec. for a 1 GB file).", + "This method is significantly faster than when NZBGet would report a failure,", + "after actual downloading a (part of) the files that end up incomplete.", + "The script is typically useful for issues related to:", + "- very recent posts", + "- failed downloads, which after a while are just ok (propagation issues),", + "- incomplete posts,", + "- taken down posts (DMCA, etc.),", + "- old posts,", + "- long par repair times,", + "- downloading (parts of) NZB files beyond repair,", + "- unnecessary use of expensive block / slow fill accounts.", "", - "NOTE: To stop the script or remove the .lock file, reload NZBGet via SYSTEM." + "NOTE: To stop the script or delete the .lock file, restart NZBGet via SYSTEM." ], "options": [ {