Update parser.py

This commit is contained in:
vvzvlad 2024-08-26 04:29:19 +03:00
parent fa0e73280d
commit db8d3ccc86

View File

@ -12,45 +12,53 @@ def is_json(myjson):
return True return True
def parse_logs(): def parse_logs():
unsuccessful_attempts = 0 start_time = time.time()
current_retry = 0 while True:
max_retry = 0 unsuccessful_attempts = 0
print("Requesting Docker logs...", flush=True) current_retry = 0
process = subprocess.Popen( max_retry = 0
["docker", "logs", "worker"], print("Requesting Docker logs...", flush=True)
stdout=subprocess.PIPE, process = subprocess.Popen(
stderr=subprocess.STDOUT, ["docker", "logs", "worker"],
text=True stdout=subprocess.PIPE,
) stderr=subprocess.STDOUT,
text=True
)
try: try:
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
if stderr: if stderr:
print(f"Error: {stderr.strip()}", flush=True) print(f"Error: {stderr.strip()}", flush=True)
for line in stdout.splitlines(): for line in stdout.splitlines():
#print(f"Line: {line}", flush=True) #print(f"Line: {line}", flush=True)
if is_json(line): if is_json(line):
data = json.loads(line) data = json.loads(line)
if data.get("level") == "info" or data.get("level") == "error": if data.get("level") == "info" or data.get("level") == "error":
print(f"{data['message']}", flush=True) print(f"{data['message']}", flush=True)
if data.get("msg") == "Send Worker Data to chain" and data.get("message") == "Success": if data.get("msg") == "Send Worker Data to chain" and data.get("message") == "Success":
print(f"Success: {data}", flush=True) print(f"Success: {data}", flush=True)
return True, f"Success after {unsuccessful_attempts} unsuccessful attempts, with current retry {current_retry} out of {max_retry}" return True, f"Success after {unsuccessful_attempts} unsuccessful attempts, with current retry {current_retry} out of {max_retry}"
elif data.get("msg") == "Send Worker Data to chain" and "Failed, retrying..." in data.get("message", ""): elif data.get("msg") == "Send Worker Data to chain" and "Failed, retrying..." in data.get("message", ""):
unsuccessful_attempts += 1 unsuccessful_attempts += 1
retry_info = data["message"].split("Retry ")[1].strip("()") retry_info = data["message"].split("Retry ")[1].strip("()")
current_retry, max_retry = map(int, retry_info.split("/")) current_retry, max_retry = map(int, retry_info.split("/"))
if current_retry == max_retry: if current_retry == max_retry:
print(f"Max Retry Reached: {data}", flush=True) print(f"Max Retry Reached: {data}", flush=True)
return False, "Max Retry Reached" return False, "Max Retry Reached"
finally: except Exception as e:
process.stdout.close() print(f"Exception occurred: {e}", flush=True)
finally:
print("Sleeping before next log request...", flush=True) process.stdout.close()
time.sleep(5)
print("Sleeping before next log request...", flush=True)
time.sleep(30)
if time.time() - start_time > 30 * 60:
print("Timeout reached: 30 minutes elapsed without success.", flush=True)
return False, "Timeout reached: 30 minutes elapsed without success."
return False, "No Success" return False, "No Success"