Skip to content

Commit

Permalink
Containerize application
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Mar 11, 2021
1 parent 892a678 commit 8a9b4b2
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 98 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dockerfile
config.py
.env
.vscode/
.github/
logs/
21 changes: 21 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Docker Image CI

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Publish Image
uses: matootie/[email protected]
with:
accessToken: ${{ secrets.GHCR_PAT }}
containerRegistry: true
tag: |
latest
${{ github.sha }}
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.8

WORKDIR /app
COPY pyproject.toml poetry.lock ./

RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev

COPY . .
RUN mkdir logs
CMD ["python", "docker-launcher.py"]
15 changes: 4 additions & 11 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from discord.ext import commands

import cogs
import config
import helpers


uvloop.install()

DEFAULT_DISABLED_MESSAGE = (
"The bot's currently disabled. It may be refreshing for some quick updates, or down for another reason. "
"Try again later and check the #status channel in the official server for more details."
Expand All @@ -36,10 +38,9 @@ def __init__(self, **kwargs):
super().__init__(**kwargs, color=color)

def __init__(self, **kwargs):
self.pipe = kwargs.pop("pipe")
self.cluster_name = kwargs.pop("cluster_name")
self.cluster_idx = kwargs.pop("cluster_idx")
self.config = config
self.config = kwargs.pop("config")
self.ready = False
self.menus = {}

Expand Down Expand Up @@ -120,11 +121,6 @@ async def do_startup_tasks(self):

async def on_ready(self):
self.log.info(f"[Cluster#{self.cluster_name}] Ready called.")
try:
self.pipe.send(1)
self.pipe.close()
except OSError:
pass

async def on_shard_ready(self, shard_id):
self.log.info(f"[Cluster#{self.cluster_name}] Shard {shard_id} ready")
Expand Down Expand Up @@ -164,6 +160,3 @@ async def reload_modules(self):
self.reload_extension(f"cogs.{i}")

await self.do_startup_tasks()


uvloop.install()
1 change: 0 additions & 1 deletion cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"noevent",
"pokemon",
"redis",
"sentry",
"shop",
"spawning",
"sprites",
Expand Down
4 changes: 2 additions & 2 deletions cogs/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, bot):
self.post_count.start()
self.update_status.start()

if self.bot.cluster_idx == 0:
if self.bot.cluster_idx == 0 and self.bot.config.DBL_TOKEN is not None:
self.post_dbl.start()
self.remind_votes.start()

Expand Down Expand Up @@ -452,7 +452,7 @@ def cog_unload(self):
self.post_count.cancel()
self.update_status.cancel()

if self.bot.cluster_idx == 0:
if self.bot.cluster_idx == 0 and self.bot.config.DBL_TOKEN is not None:
self.post_dbl.cancel()
self.remind_votes.cancel()

Expand Down
39 changes: 0 additions & 39 deletions cogs/sentry.py

This file was deleted.

1 change: 0 additions & 1 deletion config.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
BOT_TOKEN = None
SECRET_KEY = None
REDIS_CONF = {}
SENTRY_URL = None

# DBL
DBL_TOKEN = None
Expand Down
50 changes: 50 additions & 0 deletions docker-launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
from collections import namedtuple

import discord

from bot import ClusterBot

Config = namedtuple(
"Config",
[
"DATABASE_URI",
"DATABASE_NAME",
"BOT_TOKEN",
"SECRET_KEY",
"REDIS_CONF",
"DBL_TOKEN",
],
)

if __name__ == "__main__":
config = Config(
DATABASE_URI=os.environ["DATABASE_URI"],
DATABASE_NAME=os.environ["DATABASE_NAME"],
BOT_TOKEN=os.environ["BOT_TOKEN"],
SECRET_KEY=os.environ["SECRET_KEY"],
REDIS_CONF={
"address": os.environ["REDIS_URI"],
"password": os.getenv("REDIS_PASSWORD"),
},
DBL_TOKEN=os.getenv("DBL_TOKEN"),
)

num_shards = os.getenv("NUM_SHARDS", 1)
num_clusters = os.getenv("NUM_CLUSTERS", 1)
cluster_idx = os.getenv("CLUSTER_IDX", 0)

shard_ids = list(range(cluster_idx, num_shards, num_clusters))

ClusterBot(
token=config.BOT_TOKEN,
shard_ids=shard_ids,
shard_count=num_shards,
cluster_name=str(cluster_idx),
cluster_idx=cluster_idx,
case_insensitive=True,
fetch_offline_members=False,
allowed_mentions=discord.AllowedMentions(everyone=False, roles=False),
intents=discord.Intents.default(),
config=config,
)
10 changes: 2 additions & 8 deletions launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def __init__(self, launcher, name, shard_ids, max_shards):
fetch_offline_members=False,
allowed_mentions=discord.AllowedMentions(everyone=False, roles=False),
intents=intents,
config=config,
)
self.name = name
self.log = logging.getLogger(f"Cluster#{name}")
Expand Down Expand Up @@ -194,19 +195,12 @@ async def start(self, *, force=False):
self.process.terminate()
self.process.close()

stdout, stdin = multiprocessing.Pipe()
kw = self.kwargs
kw["pipe"] = stdin
self.process = multiprocessing.Process(
target=ClusterBot, kwargs=kw, daemon=True
target=ClusterBot, kwargs=self.kwargs, daemon=True
)
self.process.start()
self.log.info(f"Process started with PID {self.process.pid}")

if await self.launcher.loop.run_in_executor(None, stdout.recv) == 1:
stdout.close()
self.log.info("Process started successfully")

return True

def stop(self, sign=signal.SIGINT):
Expand Down
38 changes: 3 additions & 35 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ stripe = "^2.51.0"
durations_nlp = "^1.0.1"
aioredis = "^1.3.1"
uvloop = "^0.14.0"
sentry-sdk = "^0.19.2"
Quart-CORS = "^0.3.0"
discord-ext-menus = {git = "https://github.com/Rapptz/discord-ext-menus.git"}
aioredis-lock = "^0.1.0"
Expand Down

0 comments on commit 8a9b4b2

Please sign in to comment.