feat(shard): 🎉 Initialize the rewrite.
This commit is contained in:
@@ -1,107 +1,26 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
import asyncio
|
||||
|
||||
import functools
|
||||
import typer
|
||||
|
||||
import click
|
||||
|
||||
from procs import flush_messages
|
||||
|
||||
from payphone.db import setup, models
|
||||
from payphone.bot import bot
|
||||
from payphone.core import logging, config, constants
|
||||
from payphone import tasks
|
||||
|
||||
|
||||
logging.setup("Tasks")
|
||||
|
||||
log = logging.logger
|
||||
app = typer.Typer(name="Tasks")
|
||||
|
||||
|
||||
def coroutine(f):
|
||||
@functools.wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
return asyncio.run(f(*args, **kwargs))
|
||||
@app.command()
|
||||
def connect() -> None:
|
||||
"""Connect to other people."""
|
||||
|
||||
return wrapper
|
||||
asyncio.run(tasks.connect.main())
|
||||
|
||||
|
||||
@click.group()
|
||||
@coroutine
|
||||
async def main():
|
||||
await setup.init()
|
||||
@app.command()
|
||||
def expire():
|
||||
"""Expire calls."""
|
||||
|
||||
|
||||
@main.command()
|
||||
@coroutine
|
||||
async def flush():
|
||||
"""Flush all messages in Redis to the database. Runs every 5 seconds."""
|
||||
|
||||
while True:
|
||||
await flush_messages.flush()
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
@main.command()
|
||||
@coroutine
|
||||
async def delete_accounts():
|
||||
"""Delete all accounts that were scheduled for deletion. Runs every 1 hour."""
|
||||
|
||||
while True:
|
||||
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
|
||||
asyncio.run(tasks.expire.main())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
app()
|
||||
|
||||
Reference in New Issue
Block a user