This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.py
211 lines (176 loc) · 7.15 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
199
200
201
202
203
204
205
206
207
208
209
210
211
# extremely old code (2+ years)
import asyncio
import logging
import multiprocessing
import signal
import os
import sys
import time
import requests
from bot import ClusterBot
TOKEN = "tokenHere"
shardsPerCluster = 20 # how many shards you'd like per cluster e.g. cluster 1 = shard 0-14, cluster 2 = shard 15-29, etc.
shardCount = "auto" # keep as "auto" if you want to automatically get the shard count from discord, otherwise set to a number.
log = logging.getLogger("Cluster#Launcher")
log.setLevel(logging.DEBUG)
hdlr = logging.StreamHandler()
hdlr.setFormatter(logging.Formatter("[Launcher] %(message)s"))
log.handlers = [hdlr]
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, *, ipc=False):
log.info("Cluster Launcher Starting")
self.cluster_queue = []
self.clusters = []
self.fut = None
self.loop = loop
self.alive = True
self.keep_alive = None
self.init = time.perf_counter()
self.start_ipc = ipc
self.ipc = None
def get_shard_count(self):
if shardCount != "auto":
return int(shardCount)
else:
data = requests.get('https://discordapp.com/api/v7/gateway/bot', headers={
"Authorization": "Bot "+TOKEN,
"User-Agent": "DiscordBot (https://github.com/Rapptz/discord.py 1.3.0a) Python/3.7 aiohttp/3.6.1"
})
data.raise_for_status()
content = data.json()
log.info(f"Successfully got shard count of {content['shards']} ({data.status_code, data.reason})")
#return 16
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):
if self.start_ipc:
log.info("IPC server starting up")
import ipc # pylint: disable=import-outside-toplevel
self.ipc = multiprocessing.Process(target=ipc.start, daemon=True)
self.ipc.start()
shards = list(range(self.get_shard_count()))
size = [shards[x:x + shardsPerCluster] for x in range(0, len(shards), shardsPerCluster)]
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()
if self.ipc and self.ipc.is_alive():
os.kill(self.ipc.pid, signal.SIGINT)
# return the name of the cluster
def get_cluster_name(self):
return self.clusters[0].name
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())
if self.ipc and not self.ipc.is_alive():
log.critical("IPC websocket server dead, require reboot")
self.ipc = None
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="-",
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("[Cluster] %(message)s"))
self.log.handlers = [hdlr]
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()