-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
66 lines (55 loc) · 1.93 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import asyncio
import discord
import requests
from dotenv import load_dotenv
from time import sleep
from random import getrandbits
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
HEALTHCHECK_INTERVAL = int(os.getenv('HEALTHCHECK_INTERVAL'))
HEALTHCHECK_URL = os.getenv('HEALTHCHECK_URL')
class Client(discord.Client):
def __init__(self):
# idempotent loop reinitialization, since using fork() with asyncio
# https://bugs.python.org/issue21998
asyncio.set_event_loop(asyncio.new_event_loop())
super().__init__()
self.loop.create_task(self.healthcheck())
async def on_ready(self):
print(f"Connected as '{self.user}' (id: {self.user.id}).")
async def healthcheck(self):
await self.wait_until_ready()
while not self.is_closed():
if not is_server_healthy():
print('Closing connection.')
await self.close()
await asyncio.sleep(HEALTHCHECK_INTERVAL)
def is_server_healthy():
try:
response = requests.get(
url = HEALTHCHECK_URL,
timeout = HEALTHCHECK_INTERVAL/2,
)
if response.status_code == 200:
print('Healthcheck: success')
return True
except Exception as ex:
print(ex)
print('Healthcheck: failure')
return False
if __name__ == '__main__':
while True:
if is_server_healthy():
# run disord.py's Client() in a separate process
# due to the use of `set_wakeup_fd` which can only
# be called from the main thread of the main interpreter
# https://docs.python.org/3/library/signal.html#signal.set_wakeup_fd
if os.fork() == 0:
# child
client = Client()
client.run(DISCORD_TOKEN)
exit(0)
# parent
os.wait()
sleep(HEALTHCHECK_INTERVAL)