perf(bot): ⚡ Use one process per shard.
Start one process per shard and initialize a new one if any of them fails.
This commit is contained in:
+1
-1
@@ -6,7 +6,7 @@ services:
|
|||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
- IS_DEBUG=false
|
IS_DEBUG: "false"
|
||||||
depends_on:
|
depends_on:
|
||||||
- database
|
- database
|
||||||
- redis
|
- redis
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ requires-python = ">=3.12"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"aerich==0.7.1",
|
"aerich==0.7.1",
|
||||||
"aiohttp>=3.10.10",
|
"aiohttp>=3.10.10",
|
||||||
|
"click>=8.1.7",
|
||||||
"colorthief>=0.2.1",
|
"colorthief>=0.2.1",
|
||||||
"httpx>=0.27.2",
|
"httpx>=0.27.2",
|
||||||
"orjson>=3.10.7",
|
"orjson>=3.10.7",
|
||||||
|
|||||||
@@ -1,5 +1,51 @@
|
|||||||
from payphone import bot
|
def shard(shard_id: int, shard_count: int) -> None:
|
||||||
|
"""Starts a new shard.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
shard_id (int): The shard's ID.
|
||||||
|
shard_count (int): The total amount of shards.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from payphone.bot import bot
|
||||||
from payphone.core import config
|
from payphone.core import config
|
||||||
|
|
||||||
|
bot.shard_ids = [shard_id]
|
||||||
|
bot.shard_count = shard_count
|
||||||
|
|
||||||
bot.bot.run(config.TOKEN)
|
bot.run(config.TOKEN)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import multiprocessing as mp
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from payphone.core import config
|
||||||
|
|
||||||
|
response = httpx.get(
|
||||||
|
"https://discord.com/api/v10/gateway/bot",
|
||||||
|
headers={"Authorization": f"Bot {config.TOKEN}"},
|
||||||
|
)
|
||||||
|
|
||||||
|
info = response.json()
|
||||||
|
|
||||||
|
shard_count = info["shards"]
|
||||||
|
|
||||||
|
ps: list[mp.Process] = [
|
||||||
|
mp.Process(target=shard, args=(i, shard_count)) for i in range(shard_count)
|
||||||
|
]
|
||||||
|
|
||||||
|
for p in ps:
|
||||||
|
p.start()
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for i in range(shard_count):
|
||||||
|
if not ps[i].is_alive():
|
||||||
|
ps[i] = mp.Process(target=shard, args=(i, shard_count))
|
||||||
|
ps[i].start()
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|||||||
@@ -371,6 +371,7 @@ source = { virtual = "." }
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "aerich" },
|
{ name = "aerich" },
|
||||||
{ name = "aiohttp" },
|
{ name = "aiohttp" },
|
||||||
|
{ name = "click" },
|
||||||
{ name = "colorthief" },
|
{ name = "colorthief" },
|
||||||
{ name = "httpx" },
|
{ name = "httpx" },
|
||||||
{ name = "orjson" },
|
{ name = "orjson" },
|
||||||
@@ -384,6 +385,7 @@ dependencies = [
|
|||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "aerich", specifier = "==0.7.1" },
|
{ name = "aerich", specifier = "==0.7.1" },
|
||||||
{ name = "aiohttp", specifier = ">=3.10.10" },
|
{ name = "aiohttp", specifier = ">=3.10.10" },
|
||||||
|
{ name = "click", specifier = ">=8.1.7" },
|
||||||
{ name = "colorthief", specifier = ">=0.2.1" },
|
{ name = "colorthief", specifier = ">=0.2.1" },
|
||||||
{ name = "httpx", specifier = ">=0.27.2" },
|
{ name = "httpx", specifier = ">=0.27.2" },
|
||||||
{ name = "orjson", specifier = ">=3.10.7" },
|
{ name = "orjson", specifier = ">=3.10.7" },
|
||||||
|
|||||||
Reference in New Issue
Block a user