From 3c7d9e193cb1be8a43cc8a6b8f4eb5885edb6dc2 Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 1 Nov 2024 01:52:53 -0300 Subject: [PATCH] feat(bot): :sparkles: Move background tasks to separate image. --- Dockerfile | 12 +++++++++--- run.py => bot.py | 0 k8s/bot.yml | 4 ++-- k8s/citus.yml | 2 +- k8s/tasks.yml | 17 +++++++++++++++++ payphone/cogs/calls/cog.py | 20 -------------------- payphone/cogs/mods/cog.py | 6 +----- payphone/core/config.py | 2 +- pyproject.toml | 1 + tasks.py | 37 +++++++++++++++++++++++++++++++++++++ uv.lock | 2 ++ 11 files changed, 71 insertions(+), 32 deletions(-) rename run.py => bot.py (100%) create mode 100644 k8s/tasks.yml create mode 100644 tasks.py diff --git a/Dockerfile b/Dockerfile index 69274fe..679000e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] diff --git a/run.py b/bot.py similarity index 100% rename from run.py rename to bot.py diff --git a/k8s/bot.yml b/k8s/bot.yml index 3ce74f5..09ec3e2 100644 --- a/k8s/bot.yml +++ b/k8s/bot.yml @@ -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: @@ -41,7 +41,7 @@ spec: cpu: "250m" memory: "256Mi" imagePullSecrets: - - name: regcred + - name: regcred volumeClaimTemplates: - metadata: name: bot-storage diff --git a/k8s/citus.yml b/k8s/citus.yml index e7a4ea3..79bea27 100644 --- a/k8s/citus.yml +++ b/k8s/citus.yml @@ -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 diff --git a/k8s/tasks.yml b/k8s/tasks.yml new file mode 100644 index 0000000..660e824 --- /dev/null +++ b/k8s/tasks.yml @@ -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 diff --git a/payphone/cogs/calls/cog.py b/payphone/cogs/calls/cog.py index 0c98bce..5bdca7b 100644 --- a/payphone/cogs/calls/cog.py +++ b/payphone/cogs/calls/cog.py @@ -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"}) diff --git a/payphone/cogs/mods/cog.py b/payphone/cogs/mods/cog.py index 54121ed..e633578 100644 --- a/payphone/cogs/mods/cog.py +++ b/payphone/cogs/mods/cog.py @@ -242,8 +242,6 @@ class Mods(base.Cog): - `[]`: 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() diff --git a/payphone/core/config.py b/payphone/core/config.py index 2fa3279..21e7792 100644 --- a/payphone/core/config.py +++ b/payphone/core/config.py @@ -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") diff --git a/pyproject.toml b/pyproject.toml index 2b7da20..38dc137 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..481c5d1 --- /dev/null +++ b/tasks.py @@ -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() diff --git a/uv.lock b/uv.lock index ca4ab84..a614cef 100644 --- a/uv.lock +++ b/uv.lock @@ -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" },