feat(bot): ✨ Move background tasks to separate image.

This commit is contained in:
Nyeki.py
2024-11-01 01:52:53 -03:00
parent cd578e43bd
commit 3c7d9e193c
11 changed files with 71 additions and 32 deletions
+9 -3
View File
@@ -1,4 +1,4 @@
FROM python:3.12
FROM python:3.12 AS base
WORKDIR /app
COPY . .
@@ -6,6 +6,12 @@ COPY . .
RUN pip install uv
RUN uv sync --no-dev
ENTRYPOINT [ "uv" ]
CMD [ "run", "--no-dev", "python3", "-u", "run.py" ]
FROM base AS shard
ENTRYPOINT [ "uv", "run", "--no-dev", "bot.py" ]
FROM base AS tasks
ENTRYPOINT [ "uv", "run", "--no-dev", "tasks.py" ]
View File
+1 -1
View File
@@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: bot
image: git.nyeki.dev/payphone/payphone:latest
image: git.nyeki.dev/payphone/payphone-shard:latest
imagePullPolicy: "Always"
envFrom:
- configMapRef:
+1 -1
View File
@@ -64,4 +64,4 @@ spec:
storageClassName: "standard" # Update based on your storage class
resources:
requests:
storage: 5Gi # Size for each pod's storage
storage: 20Gi # Size for each pod's storage
+17
View File
@@ -0,0 +1,17 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: tasks
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: tasks
image: git.nyeki.dev/payphone/payphone-tasks:latest
command: ["uv", "run", "--no-dev", "tasks.py", "flush"]
restartPolicy: OnFailure
imagePullSecrets:
- name: regcred
-20
View File
@@ -238,11 +238,6 @@ class Calls(base.Cog):
f"**TIP**: {secrets.choice(constants.TIPS).replace("{prefix}", ctx.clean_prefix)}\n"
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
+ msg_phone_answered
)
@@ -254,11 +249,6 @@ class Calls(base.Cog):
f"**TIP**: {secrets.choice(constants.TIPS).replace("{prefix}", ctx.clean_prefix)}\n"
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
)
@commands.command(aliases=["s"])
@@ -436,11 +426,6 @@ class Calls(base.Cog):
f"**TIP**: {secrets.choice(constants.TIPS).replace("{prefix}", ctx.clean_prefix)}\n"
f"☎️ You skipped the call.\n"
)
+ (
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
+ (
msg_phone_answered
if msg_phone_answered
@@ -460,11 +445,6 @@ class Calls(base.Cog):
f"☎️ You skipped the call.\n"
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
)
@commands.command(aliases=["hang", "h"], extras={"help_name": "Hang Up"})
+1 -5
View File
@@ -242,8 +242,6 @@ class Mods(base.Cog):
- `[<reason>]`: The reason of the shutdown.
"""
self.bot.scheduled_shutdown.value = True
if seconds > 0:
oncall_channel_ids = list(
await self.bot.redis.smembers(constants.CHANNELS_ONCALL_SET)
@@ -321,9 +319,7 @@ class Mods(base.Cog):
await flush_messages.flush()
await ctx.send("⚡ Messages were flushed, payphone will shut down.")
self.bot.shutdown.value = True
await ctx.send("⚡ Messages were flushed, payphone is ready to shut down.")
@commands.command()
@commands.is_owner()
+1 -1
View File
@@ -35,6 +35,6 @@ TORTOISE_ORM = {
},
}
SUGGESTIONS_CHANNEL_ID = int(os.getenv("SUGGESTIONS_CHANNEL_ID"))
SUGGESTIONS_CHANNEL_ID = int(os.getenv("SUGGESTIONS_CHANNEL_ID", "0"))
SERVER_INVITE = os.getenv("SERVER_INVITE")
+1
View File
@@ -7,6 +7,7 @@ requires-python = ">=3.12"
dependencies = [
"aerich==0.7.1",
"aiohttp>=3.10.10",
"click>=8.1.7",
"colorthief>=0.2.1",
"httpx>=0.27.2",
"orjson>=3.10.7",
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import functools
import click
from procs import flush_messages
from payphone.db import setup
from payphone.core import logging
logging.setup("Tasks")
def coroutine(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return asyncio.run(f(*args, **kwargs))
return wrapper
@click.group()
@coroutine
async def main():
await setup.init()
@main.command()
@coroutine
async def flush():
await flush_messages.flush()
if __name__ == "__main__":
main()
Generated
+2
View File
@@ -392,6 +392,7 @@ source = { virtual = "." }
dependencies = [
{ name = "aerich" },
{ name = "aiohttp" },
{ name = "click" },
{ name = "colorthief" },
{ name = "httpx" },
{ name = "orjson" },
@@ -406,6 +407,7 @@ dependencies = [
requires-dist = [
{ name = "aerich", specifier = "==0.7.1" },
{ name = "aiohttp", specifier = ">=3.10.10" },
{ name = "click", specifier = ">=8.1.7" },
{ name = "colorthief", specifier = ">=0.2.1" },
{ name = "httpx", specifier = ">=0.27.2" },
{ name = "orjson", specifier = ">=3.10.7" },