2024-08-26 04:26:47 +03:00
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
def is_json(myjson):
|
|
|
|
try:
|
|
|
|
json_object = json.loads(myjson)
|
|
|
|
except ValueError as e:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2024-08-28 02:22:54 +03:00
|
|
|
def parse_logs(timeout):
|
2024-08-26 04:29:19 +03:00
|
|
|
start_time = time.time()
|
|
|
|
while True:
|
|
|
|
unsuccessful_attempts = 0
|
|
|
|
current_retry = 0
|
|
|
|
max_retry = 0
|
|
|
|
print("Requesting Docker logs...", flush=True)
|
|
|
|
process = subprocess.Popen(
|
|
|
|
["docker", "logs", "worker"],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
text=True
|
|
|
|
)
|
2024-08-26 04:26:47 +03:00
|
|
|
|
2024-08-26 04:29:19 +03:00
|
|
|
try:
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
if stderr:
|
|
|
|
print(f"Error: {stderr.strip()}", flush=True)
|
|
|
|
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
#print(f"Line: {line}", flush=True)
|
|
|
|
if is_json(line):
|
|
|
|
data = json.loads(line)
|
|
|
|
|
|
|
|
if data.get("level") == "info" or data.get("level") == "error":
|
|
|
|
print(f"{data['message']}", flush=True)
|
2024-08-26 04:26:47 +03:00
|
|
|
|
2024-08-26 04:29:19 +03:00
|
|
|
if data.get("msg") == "Send Worker Data to chain" and data.get("message") == "Success":
|
|
|
|
print(f"Success: {data}", flush=True)
|
|
|
|
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", ""):
|
|
|
|
unsuccessful_attempts += 1
|
|
|
|
retry_info = data["message"].split("Retry ")[1].strip("()")
|
|
|
|
current_retry, max_retry = map(int, retry_info.split("/"))
|
|
|
|
if current_retry == max_retry:
|
|
|
|
print(f"Max Retry Reached: {data}", flush=True)
|
|
|
|
return False, "Max Retry Reached"
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Exception occurred: {e}", flush=True)
|
2024-08-26 04:26:47 +03:00
|
|
|
|
2024-08-26 04:29:19 +03:00
|
|
|
print("Sleeping before next log request...", flush=True)
|
|
|
|
time.sleep(30)
|
2024-08-26 04:26:47 +03:00
|
|
|
|
2024-08-28 02:24:48 +03:00
|
|
|
if time.time() - start_time > timeout * 60:
|
2024-08-28 02:22:54 +03:00
|
|
|
print(f"Timeout reached: {timeout} minutes elapsed without success.", flush=True)
|
|
|
|
return False, f"Timeout reached: {timeout} minutes elapsed without success."
|
2024-08-26 04:26:47 +03:00
|
|
|
|
|
|
|
return False, "No Success"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-08-28 02:22:54 +03:00
|
|
|
print("Parsing logs...")
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
timeout = eval(sys.argv[1])
|
|
|
|
else:
|
|
|
|
timeout = 30
|
|
|
|
result = parse_logs(timeout)
|
2024-08-26 04:26:47 +03:00
|
|
|
print(result[1])
|
|
|
|
if result[0] == False:
|
|
|
|
print("Exiting 1...")
|
2024-08-27 03:56:27 +03:00
|
|
|
sys.exit(1)
|
2024-08-26 04:26:47 +03:00
|
|
|
else:
|
|
|
|
print("Exiting 0...")
|
2024-08-27 03:56:27 +03:00
|
|
|
sys.exit(0)
|
2024-08-26 04:26:47 +03:00
|
|
|
|