2020-06-02 00:51:06 +00:00
|
|
|
# © 2020 io mintz <io@mintz.cc>
|
|
|
|
#
|
|
|
|
# Emote Manager is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Emote Manager is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with Emote Manager. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
from multiprocessing.shared_memory import ShareableList
|
2020-06-02 00:51:06 +00:00
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
from bot_bin.stats import BotBinStats
|
|
|
|
|
2020-10-08 02:48:25 +00:00
|
|
|
# The resource tracker unlinks shared memory on shutdown and assumes all processes that share memory
|
|
|
|
# share a parent python process. But this is not necessarily the case: the user might decide to run
|
|
|
|
# each cluster as a systemd unit for example. We don't want a crash of a single cluster to unlink
|
|
|
|
# the shared memory segment, so we stub out the resource tracking process here,
|
|
|
|
# as there's no built in way to disable it.
|
|
|
|
# See https://bugs.python.org/issue38119
|
|
|
|
from multiprocessing import resource_tracker
|
|
|
|
resource_tracker.ensure_running = lambda: None
|
|
|
|
resource_tracker.main = lambda: None
|
|
|
|
|
2020-06-02 00:51:06 +00:00
|
|
|
class Stats(BotBinStats):
|
2020-06-02 03:37:18 +00:00
|
|
|
def __init__(self, bot):
|
|
|
|
super().__init__(bot)
|
2020-10-08 02:48:25 +00:00
|
|
|
|
|
|
|
# Use our user ID as part of the shm name
|
|
|
|
# to allow running multiple instances of the bot on the same machine.
|
2020-10-08 13:34:56 +00:00
|
|
|
# The shard count is incorporated to prevent the following situation:
|
|
|
|
# - Shard count is increased in the launcher script / unit file from 30 to 33
|
|
|
|
# - All clusters are restarted
|
|
|
|
# - Clusters begin using the old 30-element-long list to store their shard counts,
|
|
|
|
# but because some clusters that haven't restarted yet are still using the old shlist.
|
|
|
|
shm_name = f'emote-manager-{self.bot.user_id}-{self.bot.shard_count}'
|
2020-10-08 02:48:25 +00:00
|
|
|
if self.is_opener():
|
|
|
|
seq = [0] * self.bot.shard_count
|
|
|
|
try:
|
|
|
|
self.shlist = ShareableList(seq, name=shm_name)
|
|
|
|
except FileExistsError:
|
|
|
|
# The file was probably left open from a previous run of the bot,
|
|
|
|
# so let's re-attach and wipe the old data.
|
|
|
|
self.shlist = ShareableList(name=shm_name)
|
|
|
|
# apparently ShareableLists don't support slicing with None values of `stop`
|
|
|
|
self.shlist[:self.bot.shard_count] = seq
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.shlist = ShareableList(name=shm_name)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# looks like we're the first cluster to start up
|
|
|
|
seq = [0] * self.bot.shard_count
|
|
|
|
self.shlist = ShareableList(seq, name=shm_name)
|
|
|
|
|
2020-07-16 23:23:58 +00:00
|
|
|
self.count()
|
2020-06-02 00:51:06 +00:00
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
def is_opener(self):
|
2020-10-08 02:48:25 +00:00
|
|
|
"""return whether this is the cluster that should open the shared memory"""
|
2020-06-02 03:37:18 +00:00
|
|
|
return 0 in self.bot.shard_ids
|
2020-06-02 00:51:06 +00:00
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
def is_reporter(self):
|
2020-10-08 02:48:25 +00:00
|
|
|
"""return whether this is the cluster that should report stats to the bot lists"""
|
2020-06-02 03:37:18 +00:00
|
|
|
return self.bot.shard_count - 1 in self.bot.shard_ids
|
2020-06-02 00:51:06 +00:00
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
def cog_unload(self):
|
|
|
|
self.shlist.shm.close()
|
|
|
|
if self.is_opener():
|
|
|
|
self.shlist.shm.unlink()
|
2020-06-02 00:51:06 +00:00
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_ready(self):
|
2020-07-16 23:23:58 +00:00
|
|
|
self.count()
|
2020-10-08 02:48:37 +00:00
|
|
|
all_shards_ready = all(self.shlist)
|
|
|
|
if self.is_reporter() and all_shards_ready:
|
2020-07-16 23:23:58 +00:00
|
|
|
await self.send()
|
|
|
|
|
|
|
|
def count(self):
|
2020-06-03 21:11:26 +00:00
|
|
|
for shard_id in self.bot.shard_ids:
|
|
|
|
self.shlist[shard_id] = 0
|
|
|
|
|
2020-06-02 03:37:18 +00:00
|
|
|
for guild in self.bot.guilds:
|
|
|
|
self.shlist[guild.shard_id] += 1
|
2020-06-02 00:51:06 +00:00
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_join(self, guild):
|
2020-06-02 03:37:18 +00:00
|
|
|
self.shlist[guild.shard_id] += 1
|
2020-06-02 00:51:06 +00:00
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_remove(self, guild):
|
2020-06-02 03:37:18 +00:00
|
|
|
self.shlist[guild.shard_id] -= 1
|
2020-06-02 00:51:06 +00:00
|
|
|
|
|
|
|
async def guild_count(self):
|
2020-06-02 03:37:18 +00:00
|
|
|
return sum(self.shlist)
|
2020-06-02 00:51:06 +00:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Stats(bot))
|