feat(bot): ✨ Migrate tasks to separate containers and readd support for automatic call hangup and account deletion.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
import asyncio
|
||||
|
||||
import functools
|
||||
@@ -7,11 +9,14 @@ import click
|
||||
from procs import flush_messages
|
||||
|
||||
from payphone.db import setup, models
|
||||
from payphone.core import logging
|
||||
from payphone.bot import bot
|
||||
from payphone.core import logging, config, constants
|
||||
|
||||
|
||||
logging.setup("Tasks")
|
||||
|
||||
log = logging.logger
|
||||
|
||||
|
||||
def coroutine(f):
|
||||
@functools.wraps(f)
|
||||
@@ -43,9 +48,60 @@ async def delete_accounts():
|
||||
"""Delete all accounts that were scheduled for deletion. Runs every 1 hour."""
|
||||
|
||||
while True:
|
||||
await models.User.filter(is_deleting=True).delete()
|
||||
total = await models.User.filter(is_deleting=True).delete()
|
||||
|
||||
log.info("Deleted %s accounts scheduled for deletion.", total)
|
||||
|
||||
await asyncio.sleep(60 * 60)
|
||||
|
||||
|
||||
@main.command()
|
||||
@coroutine
|
||||
async def hangup_inactive_calls():
|
||||
"""Hangs up inactive calls."""
|
||||
|
||||
await bot.login(config.TOKEN)
|
||||
|
||||
await bot.redis.ping()
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(5)
|
||||
|
||||
for call in await models.Call.filter(ended_at=None).prefetch_related(
|
||||
"channels"
|
||||
):
|
||||
if call.started_at > datetime.now(timezone.utc) - timedelta(minutes=1):
|
||||
continue
|
||||
|
||||
if (
|
||||
await bot.redis.exists(
|
||||
constants.CALL_MESSAGES_TEMPLATE % call.id,
|
||||
constants.CALL_IS_ACTIVE % call.id,
|
||||
)
|
||||
) != 2:
|
||||
call.ended_at = datetime.now(timezone.utc)
|
||||
await call.save()
|
||||
|
||||
log.info("Hung up call %s.", call.id)
|
||||
|
||||
try:
|
||||
channels = [
|
||||
await bot.get_or_fetch_channel(channel.id)
|
||||
for channel in call.channels
|
||||
]
|
||||
|
||||
for channel in channels:
|
||||
try:
|
||||
await channel.send(
|
||||
"☎️ The call was hung up because nobody replied."
|
||||
)
|
||||
|
||||
finally:
|
||||
pass
|
||||
|
||||
finally:
|
||||
continue
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user