implement a functioning scheduler, get rid of threads
This commit is contained in:
parent
54be2516fe
commit
bee10a2e89
5 changed files with 44 additions and 89 deletions
32
pgbotlib/cron.py
Normal file
32
pgbotlib/cron.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import time
|
||||
import random
|
||||
|
||||
import yaml
|
||||
import aiocron
|
||||
import telethon
|
||||
import pgbotlib.response
|
||||
|
||||
|
||||
class Cron:
|
||||
def __init__(self,
|
||||
config: dict,
|
||||
client: telethon.TelegramClient,
|
||||
responder: pgbotlib.response.Responder) -> None:
|
||||
with open(config['schedule'], 'r', encoding='utf-8') as data:
|
||||
self.sched = yaml.safe_load(data.read())
|
||||
self.responder = responder
|
||||
self.client = client
|
||||
|
||||
def __mkjob(self, job: dict) -> callable:
|
||||
tokens = frozenset(job['tokens'].split(','))
|
||||
async def send_message() -> None:
|
||||
if 'rand' in job:
|
||||
time.sleep(random.randint(0, job['rand']) * 60)
|
||||
message = self.responder.get_response(tokens)
|
||||
message = self.responder.api_match(message, '')
|
||||
await self.client.send_message(job['chat'], message)
|
||||
return send_message
|
||||
|
||||
def plan(self) -> None:
|
||||
for job in self.sched:
|
||||
aiocron.crontab(job['cron'], func=self.__mkjob(job))
|
Loading…
Add table
Add a link
Reference in a new issue