-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
198 lines (169 loc) · 7.01 KB
/
launcher.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import asyncio
import logging
import multiprocessing
import os
import signal
import sys
import time
import requests
from bot import ClusterBot
from essentials.multi_server import get_pre
from essentials.settings import SETTINGS
TOKEN = SETTINGS.bot_token
log = logging.getLogger("Cluster#Launcher")
log.setLevel(logging.DEBUG)
hdlr = logging.StreamHandler()
hdlr.setFormatter(logging.Formatter("[%(asctime)s %(name)s/%(levelname)s] %(message)s"))
fhdlr = logging.FileHandler("cluster-Launcher.log", encoding='utf-8')
fhdlr.setFormatter(logging.Formatter("[%(asctime)s %(name)s/%(levelname)s] %(message)s"))
log.handlers = [hdlr, fhdlr]
CLUSTER_NAMES = (
'Alpha', 'Beta', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel',
'India', 'Juliett', 'Kilo', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec',
'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whisky', 'X-ray', 'Yankee', 'Zulu'
)
NAMES = iter(CLUSTER_NAMES)
class Launcher:
def __init__(self, loop):
log.info("Hello, world!")
self.cluster_queue = []
self.clusters = []
self.fut = None
self.loop = loop
self.alive = True
self.keep_alive = None
self.init = time.perf_counter()
def get_shard_count(self):
if SETTINGS.mode == "development":
return 1
data = requests.get('https://discordapp.com/api/v9/gateway/bot', headers={
"Authorization": "Bot "+TOKEN,
"User-Agent": "DiscordBot (https://github.com/Rapptz/discord.py 2.2.2) Python/3.8 aiohttp/3.7.4"
})
data.raise_for_status()
content = data.json()
log.info(f"Successfully got shard count of {content['shards']} ({data.status_code, data.reason})")
# return 2
return content['shards']
def start(self):
self.fut = asyncio.ensure_future(self.startup(), loop=self.loop)
try:
self.loop.run_forever()
except KeyboardInterrupt:
self.loop.run_until_complete(self.shutdown())
finally:
self.cleanup()
def cleanup(self):
self.loop.stop()
if sys.platform == 'win32':
print("press ^C again")
self.loop.close()
def task_complete(self, task):
if task.exception():
task.print_stack()
self.keep_alive = self.loop.create_task(self.rebooter())
self.keep_alive.add_done_callback(self.task_complete)
async def startup(self):
shards = list(range(self.get_shard_count()))
size = [shards[x:x + 4] for x in range(0, len(shards), 4)]
log.info(f"Preparing {len(size)} clusters")
for shard_ids in size:
self.cluster_queue.append(Cluster(self, next(NAMES), shard_ids, len(shards)))
await self.start_cluster()
self.keep_alive = self.loop.create_task(self.rebooter())
self.keep_alive.add_done_callback(self.task_complete)
log.info(f"Startup completed in {time.perf_counter()-self.init}s")
async def shutdown(self):
log.info("Shutting down clusters")
self.alive = False
if self.keep_alive:
self.keep_alive.cancel()
for cluster in self.clusters:
cluster.stop()
self.cleanup()
async def rebooter(self):
while self.alive:
# log.info("Cycle!")
if not self.clusters:
log.warning("All clusters appear to be dead")
asyncio.ensure_future(self.shutdown())
to_remove = []
for cluster in self.clusters:
if not cluster.process.is_alive():
if cluster.process.exitcode != 0:
# ignore safe exits
log.info(f"Cluster#{cluster.name} exited with code {cluster.process.exitcode}")
log.info(f"Restarting cluster#{cluster.name}")
await cluster.start()
else:
log.info(f"Cluster#{cluster.name} found dead")
to_remove.append(cluster)
cluster.stop() # ensure stopped
for rem in to_remove:
self.clusters.remove(rem)
await asyncio.sleep(5)
async def start_cluster(self):
if self.cluster_queue:
cluster = self.cluster_queue.pop(0)
log.info(f"Starting Cluster#{cluster.name}")
await cluster.start()
log.info("Done!")
self.clusters.append(cluster)
await self.start_cluster()
else:
log.info("All clusters launched")
class Cluster:
def __init__(self, launcher, name, shard_ids, max_shards):
self.launcher = launcher
self.process = None
self.kwargs = dict(
token=TOKEN,
command_prefix=get_pre,
case_insensitive=True,
pm_help=False,
# status=discord.Status.online,
owner_id=SETTINGS.owner_id,
fetch_offline_members=False,
max_messages=15000,
shard_ids=shard_ids,
shard_count=max_shards,
cluster_name=name
)
self.name = name
self.log = logging.getLogger(f"Cluster#{name}")
self.log.setLevel(logging.DEBUG)
hdlr = logging.StreamHandler()
hdlr.setFormatter(logging.Formatter("[%(asctime)s %(name)s/%(levelname)s] %(message)s"))
fhdlr = logging.FileHandler("cluster-Launcher.log", encoding='utf-8')
fhdlr.setFormatter(logging.Formatter("[%(asctime)s %(name)s/%(levelname)s] %(message)s"))
self.log.handlers = [hdlr, fhdlr]
self.log.info(f"Initialized with shard ids {shard_ids}, total shards {max_shards}")
def wait_close(self):
return self.process.join()
async def start(self, *, force=False):
if self.process and self.process.is_alive():
if not force:
self.log.warning("Start called with already running cluster, pass `force=True` to override")
return
self.log.info("Terminating existing process")
self.process.terminate()
self.process.close()
stdout, stdin = multiprocessing.Pipe()
kw = self.kwargs
kw['pipe'] = stdin
self.process = multiprocessing.Process(target=ClusterBot, kwargs=kw, daemon=True)
self.process.start()
self.log.info(f"Process started with PID {self.process.pid}")
if await self.launcher.loop.run_in_executor(None, stdout.recv) == 1:
stdout.close()
self.log.info("Process started successfully")
return True
def stop(self, sign=signal.SIGINT):
self.log.info(f"Shutting down with signal {sign!r}")
try:
os.kill(self.process.pid, sign)
except ProcessLookupError:
pass
if __name__ == "__main__":
loop = asyncio.get_event_loop()
Launcher(loop).start()