sharding (core): Add base sharding support.

Add `meta.args` for command line argument access.
Add command line argument support for shard number.
Add shard count to config file.
Add `meta.sharding` exposing shard properties.
Add shard number to logging methods.
Add shard number to data appid.
This commit is contained in:
2021-12-22 11:28:43 +02:00
parent d498673020
commit 20697c4823
10 changed files with 73 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
from .logger import log, logger
from .client import client
from .config import conf
from .logger import log, logger
from .args import args
from . import sharding

19
bot/meta/args.py Normal file
View File

@@ -0,0 +1,19 @@
import argparse
from constants import CONFIG_FILE
# ------------------------------
# Parsed commandline arguments
# ------------------------------
parser = argparse.ArgumentParser()
parser.add_argument('--conf',
dest='config',
default=CONFIG_FILE,
help="Path to configuration file.")
parser.add_argument('--shard',
dest='shard',
default=None,
type=int,
help="Shard number to run, if applicable.")
args = parser.parse_args()

View File

@@ -1,16 +1,19 @@
from discord import Intents
from cmdClient.cmdClient import cmdClient
from .config import Conf
from .config import conf
from .sharding import shard_number, shard_count
from constants import CONFIG_FILE
# Initialise config
conf = Conf(CONFIG_FILE)
# Initialise client
owners = [int(owner) for owner in conf.bot.getlist('owners')]
intents = Intents.all()
intents.presences = False
client = cmdClient(prefix=conf.bot['prefix'], owners=owners, intents=intents)
client = cmdClient(
prefix=conf.bot['prefix'],
owners=owners,
intents=intents,
shard_id=shard_number,
shard_count=shard_count
)
client.conf = conf

View File

@@ -1,9 +1,6 @@
import configparser as cfgp
conf = None # type: Conf
CONF_FILE = "bot/bot.conf"
from .args import args
class Conf:
@@ -57,3 +54,6 @@ class Conf:
def write(self):
with open(self.configfile, 'w') as conffile:
self.config.write(conffile)
conf = Conf(args.config)

View File

@@ -9,11 +9,18 @@ from utils.lib import mail, split_text
from .client import client
from .config import conf
from . import sharding
# Setup the logger
logger = logging.getLogger()
log_fmt = logging.Formatter(fmt='[{asctime}][{levelname:^8}] {message}', datefmt='%d/%m | %H:%M:%S', style='{')
log_fmt = logging.Formatter(
fmt=('[{asctime}][{levelname:^8}]' +
'[SHARD {}]'.format(sharding.shard_number) +
'{message}'),
datefmt='%d/%m | %H:%M:%S',
style='{'
)
# term_handler = logging.StreamHandler(sys.stdout)
# term_handler.setFormatter(log_fmt)
# logger.addHandler(term_handler)
@@ -77,7 +84,11 @@ async def live_log(message, context, level):
log_chid = conf.bot.getint('log_channel')
# Generate the log messages
header = "[{}][{}]".format(logging.getLevelName(level), str(context))
if sharding.sharded:
header = f"[{logging.getLevelName(level)}][SHARD {sharding.shard_number}][{context}]"
else:
header = f"[{logging.getLevelName(level)}][{context}]"
if len(message) > 1900:
blocks = split_text(message, blocksize=1900, code=False)
else:

9
bot/meta/sharding.py Normal file
View File

@@ -0,0 +1,9 @@
from .args import args
from .config import conf
shard_number = args.shard or 0
shard_count = conf.bot.getint('shard_count', 1)
sharded = (shard_count > 0)