Add queues to prevent re-processing tasks
This commit is contained in:
parent
4a69655bfb
commit
be3d5387a5
5 changed files with 30 additions and 0 deletions
|
@ -12,6 +12,8 @@ from jarvis_core.db.models import Ban, Unban
|
||||||
|
|
||||||
from jarvis_tasks.util import runat
|
from jarvis_tasks.util import runat
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
|
||||||
|
|
||||||
async def _unban(bot: int, guild: Guild, user: User, ban: Ban, logger: Logger) -> None:
|
async def _unban(bot: int, guild: Guild, user: User, ban: Ban, logger: Logger) -> None:
|
||||||
if guild and user:
|
if guild and user:
|
||||||
|
@ -30,6 +32,7 @@ async def _unban(bot: int, guild: Guild, user: User, ban: Ban, logger: Logger) -
|
||||||
admin=bot,
|
admin=bot,
|
||||||
reason="Ban expired",
|
reason="Ban expired",
|
||||||
).commit()
|
).commit()
|
||||||
|
queue.remove(ban.id)
|
||||||
|
|
||||||
|
|
||||||
async def unban(bot: Snake, logger: Logger) -> None:
|
async def unban(bot: Snake, logger: Logger) -> None:
|
||||||
|
@ -44,11 +47,14 @@ async def unban(bot: Snake, logger: Logger) -> None:
|
||||||
max_ts = datetime.now(tz=timezone.utc) + timedelta(minutes=9)
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(minutes=9)
|
||||||
bans = Ban.find(q(type="temp", active=True, duration__lte=max_ts))
|
bans = Ban.find(q(type="temp", active=True, duration__lte=max_ts))
|
||||||
async for ban in bans:
|
async for ban in bans:
|
||||||
|
if ban.id in queue:
|
||||||
|
continue
|
||||||
guild = await bot.fetch_guild(ban.guild)
|
guild = await bot.fetch_guild(ban.guild)
|
||||||
user = await bot.fetch_user(ban.user)
|
user = await bot.fetch_user(ban.user)
|
||||||
coro = _unban(bot.user.id, guild, user, ban, logger)
|
coro = _unban(bot.user.id, guild, user, ban, logger)
|
||||||
when = ban.created_at + timedelta(hours=ban.duration)
|
when = ban.created_at + timedelta(hours=ban.duration)
|
||||||
asyncio.create_task(runat(when, coro, logger))
|
asyncio.create_task(runat(when, coro, logger))
|
||||||
|
queue.append(ban.id)
|
||||||
|
|
||||||
# Check ever 10 minutes
|
# Check ever 10 minutes
|
||||||
await asyncio.sleep(600)
|
await asyncio.sleep(600)
|
||||||
|
|
|
@ -11,6 +11,8 @@ from jarvis_core.db.models import Lock
|
||||||
|
|
||||||
from jarvis_tasks.util import runat
|
from jarvis_tasks.util import runat
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
|
||||||
|
|
||||||
async def _unlock(channel: GuildChannel, lock: Lock, logger: Logger) -> None:
|
async def _unlock(channel: GuildChannel, lock: Lock, logger: Logger) -> None:
|
||||||
logger.debug(f"Deactivating lock {lock.id}")
|
logger.debug(f"Deactivating lock {lock.id}")
|
||||||
|
@ -31,6 +33,7 @@ async def _unlock(channel: GuildChannel, lock: Lock, logger: Logger) -> None:
|
||||||
logger.debug("Locked channel deleted, ignoring error")
|
logger.debug("Locked channel deleted, ignoring error")
|
||||||
lock.active = False
|
lock.active = False
|
||||||
await lock.commit()
|
await lock.commit()
|
||||||
|
queue.remove(lock.id)
|
||||||
|
|
||||||
|
|
||||||
async def unlock(bot: Snake, logger: Logger) -> None:
|
async def unlock(bot: Snake, logger: Logger) -> None:
|
||||||
|
@ -45,10 +48,13 @@ async def unlock(bot: Snake, logger: Logger) -> None:
|
||||||
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=55)
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=55)
|
||||||
locks = Lock.find(q(active=True, created_at__lte=max_ts))
|
locks = Lock.find(q(active=True, created_at__lte=max_ts))
|
||||||
async for lock in locks:
|
async for lock in locks:
|
||||||
|
if lock.id in queue:
|
||||||
|
continue
|
||||||
guild = await bot.fetch_guild(lock.guild)
|
guild = await bot.fetch_guild(lock.guild)
|
||||||
channel = await guild.fetch_channel(lock.channel)
|
channel = await guild.fetch_channel(lock.channel)
|
||||||
coro = _unlock(channel, lock, logger)
|
coro = _unlock(channel, lock, logger)
|
||||||
when = lock.created_at + timedelta(minutes=lock.duration)
|
when = lock.created_at + timedelta(minutes=lock.duration)
|
||||||
asyncio.create_task(runat(when, coro, logger))
|
asyncio.create_task(runat(when, coro, logger))
|
||||||
|
queue.append(lock.id)
|
||||||
|
|
||||||
await asyncio.sleep(delay=60)
|
await asyncio.sleep(delay=60)
|
||||||
|
|
|
@ -11,6 +11,8 @@ from jarvis_core.db.models import Lockdown
|
||||||
|
|
||||||
from jarvis_tasks.util import runat
|
from jarvis_tasks.util import runat
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
|
||||||
|
|
||||||
async def _lift(role: Role, lock: Lockdown, logger: Logger) -> None:
|
async def _lift(role: Role, lock: Lockdown, logger: Logger) -> None:
|
||||||
logger.debug(f"Lifting lockdown {lock.id}")
|
logger.debug(f"Lifting lockdown {lock.id}")
|
||||||
|
@ -18,6 +20,7 @@ async def _lift(role: Role, lock: Lockdown, logger: Logger) -> None:
|
||||||
await role.edit(permissions=original_perms)
|
await role.edit(permissions=original_perms)
|
||||||
lock.active = False
|
lock.active = False
|
||||||
await lock.commit()
|
await lock.commit()
|
||||||
|
queue.remove(lock.id)
|
||||||
|
|
||||||
|
|
||||||
async def lift(bot: Snake, logger: Logger) -> None:
|
async def lift(bot: Snake, logger: Logger) -> None:
|
||||||
|
@ -32,10 +35,13 @@ async def lift(bot: Snake, logger: Logger) -> None:
|
||||||
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=55)
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=55)
|
||||||
locks = Lockdown.find(q(active=True, created_at__lte=max_ts))
|
locks = Lockdown.find(q(active=True, created_at__lte=max_ts))
|
||||||
async for lock in locks:
|
async for lock in locks:
|
||||||
|
if lock.id in queue:
|
||||||
|
continue
|
||||||
guild = await bot.fetch_guild(lock.guild)
|
guild = await bot.fetch_guild(lock.guild)
|
||||||
role = await guild.fetch_role(guild.id)
|
role = await guild.fetch_role(guild.id)
|
||||||
coro = _lift(role, lock, logger)
|
coro = _lift(role, lock, logger)
|
||||||
when = lock.created_at + timedelta(minutes=lock.duration)
|
when = lock.created_at + timedelta(minutes=lock.duration)
|
||||||
asyncio.create_task(runat(when, coro, logger))
|
asyncio.create_task(runat(when, coro, logger))
|
||||||
|
queue.append(lock.id)
|
||||||
|
|
||||||
await asyncio.sleep(delay=60)
|
await asyncio.sleep(delay=60)
|
||||||
|
|
|
@ -14,6 +14,8 @@ from jarvis_core.util import build_embed
|
||||||
|
|
||||||
from jarvis_tasks.util import runat
|
from jarvis_tasks.util import runat
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
|
||||||
|
|
||||||
async def _remind(
|
async def _remind(
|
||||||
user: User,
|
user: User,
|
||||||
|
@ -52,6 +54,7 @@ async def _remind(
|
||||||
logger.warning(f"Reminder {reminder.id} failed, no way to contact user.")
|
logger.warning(f"Reminder {reminder.id} failed, no way to contact user.")
|
||||||
if delete:
|
if delete:
|
||||||
await reminder.delete()
|
await reminder.delete()
|
||||||
|
queue.remove(reminder.id)
|
||||||
|
|
||||||
|
|
||||||
async def remind(bot: Snake, logger: Logger) -> None:
|
async def remind(bot: Snake, logger: Logger) -> None:
|
||||||
|
@ -66,6 +69,8 @@ async def remind(bot: Snake, logger: Logger) -> None:
|
||||||
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=5)
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(seconds=5)
|
||||||
reminders = Reminder.find(q(remind_at__lte=max_ts, active=True))
|
reminders = Reminder.find(q(remind_at__lte=max_ts, active=True))
|
||||||
async for reminder in reminders:
|
async for reminder in reminders:
|
||||||
|
if reminder.id in queue:
|
||||||
|
continue
|
||||||
user = await bot.fetch_user(reminder.user)
|
user = await bot.fetch_user(reminder.user)
|
||||||
if not user:
|
if not user:
|
||||||
logger.warning(f"Failed to get user with ID {reminder.user}")
|
logger.warning(f"Failed to get user with ID {reminder.user}")
|
||||||
|
@ -84,6 +89,7 @@ async def remind(bot: Snake, logger: Logger) -> None:
|
||||||
channel = await bot.fetch_channel(reminder.channel)
|
channel = await bot.fetch_channel(reminder.channel)
|
||||||
coro = _remind(user, reminder, embed, logger, channel)
|
coro = _remind(user, reminder, embed, logger, channel)
|
||||||
asyncio.create_task(runat(reminder.remind_at, coro, logger))
|
asyncio.create_task(runat(reminder.remind_at, coro, logger))
|
||||||
|
queue.append(reminder.id)
|
||||||
|
|
||||||
# Check every 5 seconds
|
# Check every 5 seconds
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
|
@ -9,11 +9,14 @@ from jarvis_core.db.models import Warning
|
||||||
|
|
||||||
from jarvis_tasks.util import runat
|
from jarvis_tasks.util import runat
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
|
||||||
|
|
||||||
async def _unwarn(warn: Warning, logger: Logger) -> None:
|
async def _unwarn(warn: Warning, logger: Logger) -> None:
|
||||||
logger.debug(f"Deactivating warning {warn.id}")
|
logger.debug(f"Deactivating warning {warn.id}")
|
||||||
warn.active = False
|
warn.active = False
|
||||||
await warn.commit()
|
await warn.commit()
|
||||||
|
queue.remove(warn.id)
|
||||||
|
|
||||||
|
|
||||||
async def unwarn(bot: Snake, logger: Logger) -> None:
|
async def unwarn(bot: Snake, logger: Logger) -> None:
|
||||||
|
@ -28,8 +31,11 @@ async def unwarn(bot: Snake, logger: Logger) -> None:
|
||||||
max_ts = datetime.now(tz=timezone.utc) + timedelta(minutes=55)
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(minutes=55)
|
||||||
warns = Warning.find(q(active=True, created_at__lte=max_ts))
|
warns = Warning.find(q(active=True, created_at__lte=max_ts))
|
||||||
async for warn in warns:
|
async for warn in warns:
|
||||||
|
if warn.id in queue:
|
||||||
|
continue
|
||||||
coro = _unwarn(warn, logger)
|
coro = _unwarn(warn, logger)
|
||||||
when = warn.created_at + timedelta(hours=warn.duration)
|
when = warn.created_at + timedelta(hours=warn.duration)
|
||||||
asyncio.create_task(runat(when, coro, logger))
|
asyncio.create_task(runat(when, coro, logger))
|
||||||
|
queue.append(warn.id)
|
||||||
# Check every hour
|
# Check every hour
|
||||||
await asyncio.sleep(3600)
|
await asyncio.sleep(3600)
|
||||||
|
|
Loading…
Add table
Reference in a new issue