From 8761a77e18bdd03445191c7b014c59a73f309437 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 31 Aug 2020 13:27:49 +0200 Subject: [PATCH 1/3] Don't call `await client.close()`. --- .gitignore | 2 +- setup-scripts/log-checker.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index e16e2123..3445a5b8 100644 --- a/.gitignore +++ b/.gitignore @@ -78,4 +78,4 @@ docker/data # Cache files __pycache__ /.idea/ -/venv/ +/venv* diff --git a/setup-scripts/log-checker.py b/setup-scripts/log-checker.py index 2cd2c27c..ae45e6ef 100755 --- a/setup-scripts/log-checker.py +++ b/setup-scripts/log-checker.py @@ -28,14 +28,13 @@ client = discord.Client() # exit_after kills the script if it hasn't exited on its own after `delay` seconds async def exit_after(delay): await asyncio.sleep(delay) - exit(0) + sys.exit(0) @client.event async def on_ready(): await run_checks() - asyncio.create_task(exit_after(30)) - await client.close() + asyncio.create_task(exit_after(3)) async def run_checks(): @@ -71,12 +70,6 @@ async def check_docker_logs(): if len(sys.argv) > 2: container_name = sys.argv[2] - # Get the container id for siad. - cmd = 'docker ps -q --filter name=^{}$'.format(container_name) - print("[DEBUG] will run `{}`".format(cmd)) - stream = os.popen(cmd) - image_id = stream.read().strip() - # Get the number of hours to look back in the logs or use 1 as default. check_hours = DEFAULT_CHECK_INTERVAL if len(sys.argv) > 3: @@ -87,8 +80,8 @@ async def check_docker_logs(): time_string = "{}h".format(check_hours) # Read the logs. - print("[DEBUG] Will run `docker logs --since {} {}`".format(time_string, image_id)) - proc = Popen(["docker", "logs", "--since", time_string, image_id], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True) + print("[DEBUG] Will run `docker logs --since {} {}`".format(time_string, container_name)) + proc = Popen(["docker", "logs", "--since", time_string, container_name], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True) std_out, std_err = proc.communicate() if len(std_err) > 0: @@ -109,7 +102,7 @@ async def check_docker_logs(): return # If there are any critical errors. upload the whole log file. - if "Critical" in std_out or "panic" in std_out: + if 'Critical' in std_out or 'panic' in std_out: upload_name = "{}-{}-{}-{}-{}:{}:{}.log".format(container_name, time.year, time.month, time.day, time.hour, time.minute, time.second) await send_msg(client, "Critical error found in log!", file=discord.File(io.BytesIO(std_out.encode()), filename=upload_name), force_notify=True) return @@ -119,4 +112,5 @@ async def check_docker_logs(): pretty_now = now.strftime("%I:%M%p") await send_msg(client, "No critical warnings in log from `{}` to `{}`".format(pretty_before, pretty_now)) + client.run(bot_token) From 2fc6d30c01a62f29163d251d3aba0b1861e1ce4c Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 31 Aug 2020 13:38:45 +0200 Subject: [PATCH 2/3] Use `os._exit` instead of `sys.exit` - we don't want to raise an exception. --- setup-scripts/funds-checker.py | 5 +++-- setup-scripts/log-checker.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/setup-scripts/funds-checker.py b/setup-scripts/funds-checker.py index 39c31dbb..4e05edd6 100755 --- a/setup-scripts/funds-checker.py +++ b/setup-scripts/funds-checker.py @@ -5,7 +5,7 @@ health-checker runs simple health checks on a portal node using the siad API and dispatches messages to a Discord channel. """ -import discord, traceback, asyncio +import discord, traceback, asyncio, os from bot_utils import setup, send_msg, siad, sc_precision bot_token = setup() @@ -14,7 +14,7 @@ client = discord.Client() async def exit_after(delay): await asyncio.sleep(delay) - exit(0) + os._exit(0) @client.event @@ -75,4 +75,5 @@ async def check_health(): # Send an informational heartbeat if all checks passed. await send_msg(client, "Health checks passed:\n{} \n{}".format(balance_msg, alloc_msg)) + client.run(bot_token) diff --git a/setup-scripts/log-checker.py b/setup-scripts/log-checker.py index ae45e6ef..6d6a32ad 100755 --- a/setup-scripts/log-checker.py +++ b/setup-scripts/log-checker.py @@ -28,7 +28,7 @@ client = discord.Client() # exit_after kills the script if it hasn't exited on its own after `delay` seconds async def exit_after(delay): await asyncio.sleep(delay) - sys.exit(0) + os._exit(0) @client.event From 25cf5625b4f248de237d3afce8c42478ab148677 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 31 Aug 2020 13:39:07 +0200 Subject: [PATCH 3/3] 3 seconds timeout. --- setup-scripts/funds-checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup-scripts/funds-checker.py b/setup-scripts/funds-checker.py index 4e05edd6..70d710ed 100755 --- a/setup-scripts/funds-checker.py +++ b/setup-scripts/funds-checker.py @@ -20,7 +20,7 @@ async def exit_after(delay): @client.event async def on_ready(): await run_checks() - asyncio.create_task(exit_after(30)) + asyncio.create_task(exit_after(3)) await client.close()