From cf64fe026a9c44f7007bafa9887d59cfe17fc397 Mon Sep 17 00:00:00 2001 From: Gaung Ramadhan Date: Wed, 28 Sep 2022 17:48:49 +0700 Subject: [PATCH] SpamPrediction: Add model refresh scheduler (#168) Model are refreshed at 17:00 UTC (00:00 WIB) --- anjani/plugins/spam_prediction.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/anjani/plugins/spam_prediction.py b/anjani/plugins/spam_prediction.py index 93ab7a7c9..295a25df3 100644 --- a/anjani/plugins/spam_prediction.py +++ b/anjani/plugins/spam_prediction.py @@ -18,6 +18,7 @@ import pickle import re import unicodedata +from datetime import datetime, time, timedelta from functools import partial from hashlib import md5, sha256 from pathlib import Path @@ -99,6 +100,7 @@ async def on_load(self) -> None: self.setting_db = self.bot.db.get_collection("SPAM_PREDICT_SETTING") await self.__load_model() + self.bot.loop.create_task(self.__refresh_model()) async def on_chat_migrate(self, message: Message) -> None: await self.db.update_one( @@ -115,6 +117,18 @@ async def on_plugin_restore(self, chat_id: int, data: MutableMapping[str, Any]) {"chat_id": chat_id}, {"$set": data[self.name]}, upsert=True ) + async def __refresh_model(self) -> None: + scheduled_time = time(hour=17) # Run at 00:00 WIB + while True: + now = datetime.utcnow() + date = now.date() + if now.time() > scheduled_time: + date = now.date() + timedelta(days=1) + then = datetime.combine(date, scheduled_time) + self.log.debug("Next model refresh at %s UTC", then) + await asyncio.sleep((then - now).total_seconds()) + await self.__load_model() + async def __load_model(self) -> None: self.log.info("Downloading spam prediction model!") async with self.bot.http.post(