Skip to content

Commit 0043f50

Browse files
committed
Handle asyncio events within config class
1 parent 77c6568 commit 0043f50

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

bot.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def __init__(self):
6161
self.threads = ThreadManager(self)
6262
self.session = aiohttp.ClientSession(loop=self.loop)
6363
self.config = ConfigManager(self)
64-
self.config_ready = asyncio.Event()
6564
self.selfhosted = bool(self.config.get('mongo_uri'))
6665
if self.selfhosted:
6766
self.db = AsyncIOMotorClient(self.config.mongo_uri).modmail_bot
@@ -178,7 +177,6 @@ async def on_connect(self):
178177
print(Fore.CYAN + 'Connected to gateway.')
179178

180179
await self.config.refresh()
181-
self.config_ready.set()
182180

183181
activity_type = self.config.get('activity_type')
184182
message = self.config.get('activity_message')
@@ -206,7 +204,7 @@ async def on_ready(self):
206204
else:
207205
await self.threads.populate_cache()
208206

209-
await self.config_ready.wait() # Wait until config cache is popluated with stuff from db
207+
await self.config.wait_until_ready() # Wait until config cache is popluated with stuff from db
210208

211209
closures = self.config.closures.copy()
212210

core/config.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import asyncio
12
import os
23
import json
34
import box
45

5-
66
class ConfigManager:
77
"""Class that manages a cached configuration"""
88

@@ -29,8 +29,11 @@ class ConfigManager:
2929
def __init__(self, bot):
3030
self.bot = bot
3131
self.cache = box.Box()
32-
self._modified = True
32+
self.ready_event = asyncio.Event()
3333
self.populate_cache()
34+
35+
def __repr__(self):
36+
return repr(self.cache)
3437

3538
@property
3639
def api(self):
@@ -58,7 +61,6 @@ def populate_cache(self):
5861

5962
async def update(self, data=None):
6063
"""Updates the config with data from the cache"""
61-
self._modified = False
6264
if data is not None:
6365
self.cache.update(data)
6466
await self.api.update_config(self.cache)
@@ -67,6 +69,10 @@ async def refresh(self):
6769
"""Refreshes internal cache with data from database"""
6870
data = await self.api.get_config()
6971
self.cache.update(data)
72+
self.ready_event.set()
73+
74+
async def wait_until_ready(self):
75+
await self.ready_event.wait()
7076

7177
def __getattr__(self, value):
7278
return self.cache[value]

0 commit comments

Comments
 (0)