Skip to content

Commit

Permalink
SpamPrediction: Add model refresh scheduler (#168)
Browse files Browse the repository at this point in the history
Model are refreshed at 17:00 UTC (00:00 WIB)
  • Loading branch information
MrMissx authored Sep 28, 2022
1 parent 9a5abe2 commit cf64fe0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions anjani/plugins/spam_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit cf64fe0

Please sign in to comment.