Don't call `await client.close()`.

This commit is contained in:
Ivaylo Novakov 2020-08-31 13:27:49 +02:00
parent ff8c4c2d78
commit 8761a77e18
No known key found for this signature in database
GPG Key ID: 06B9354AB08BE9C6
2 changed files with 7 additions and 13 deletions

2
.gitignore vendored
View File

@ -78,4 +78,4 @@ docker/data
# Cache files # Cache files
__pycache__ __pycache__
/.idea/ /.idea/
/venv/ /venv*

View File

@ -28,14 +28,13 @@ client = discord.Client()
# exit_after kills the script if it hasn't exited on its own after `delay` seconds # exit_after kills the script if it hasn't exited on its own after `delay` seconds
async def exit_after(delay): async def exit_after(delay):
await asyncio.sleep(delay) await asyncio.sleep(delay)
exit(0) sys.exit(0)
@client.event @client.event
async def on_ready(): async def on_ready():
await run_checks() await run_checks()
asyncio.create_task(exit_after(30)) asyncio.create_task(exit_after(3))
await client.close()
async def run_checks(): async def run_checks():
@ -71,12 +70,6 @@ async def check_docker_logs():
if len(sys.argv) > 2: if len(sys.argv) > 2:
container_name = 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. # Get the number of hours to look back in the logs or use 1 as default.
check_hours = DEFAULT_CHECK_INTERVAL check_hours = DEFAULT_CHECK_INTERVAL
if len(sys.argv) > 3: if len(sys.argv) > 3:
@ -87,8 +80,8 @@ async def check_docker_logs():
time_string = "{}h".format(check_hours) time_string = "{}h".format(check_hours)
# Read the logs. # Read the logs.
print("[DEBUG] Will run `docker logs --since {} {}`".format(time_string, image_id)) print("[DEBUG] Will run `docker logs --since {} {}`".format(time_string, container_name))
proc = Popen(["docker", "logs", "--since", time_string, image_id], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True) proc = Popen(["docker", "logs", "--since", time_string, container_name], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
std_out, std_err = proc.communicate() std_out, std_err = proc.communicate()
if len(std_err) > 0: if len(std_err) > 0:
@ -109,7 +102,7 @@ async def check_docker_logs():
return return
# If there are any critical errors. upload the whole log file. # 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) 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) await send_msg(client, "Critical error found in log!", file=discord.File(io.BytesIO(std_out.encode()), filename=upload_name), force_notify=True)
return return
@ -119,4 +112,5 @@ async def check_docker_logs():
pretty_now = now.strftime("%I:%M%p") pretty_now = now.strftime("%I:%M%p")
await send_msg(client, "No critical warnings in log from `{}` to `{}`".format(pretty_before, pretty_now)) await send_msg(client, "No critical warnings in log from `{}` to `{}`".format(pretty_before, pretty_now))
client.run(bot_token) client.run(bot_token)