Skip to content

Commit

Permalink
fix database issues with child processes
Browse files Browse the repository at this point in the history
move docker files to separate folder
  • Loading branch information
ArdaxHz committed Jan 6, 2025
1 parent c7b309f commit 382b1f2
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "run.py" ]
ENTRYPOINT [ "python", "run.py" ]
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion docker-compose.yml → docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ services:
- ./config.ini:/app/config.ini
- ./logs:/app/logs
- extensions:/app/publoader/extensions
- ./entrypoint.sh:/app/entrypoint.sh
entrypoint: [ "/app/entrypoint.sh" ]
command: [ "python", "run.py" ]

publoader-extensions:
image: ardax/publoader-extensions:latest
container_name: publoader-extensions
restart: unless-stopped
restart: "no"
labels:
- "com.centurylinklabs.watchtower.enable=true"
volumes:
Expand Down
11 changes: 11 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

echo "Installing Python dependencies from requirements.txt files..."

# Recursively install all requirements.txt files in the extensions directory
find /app -name "requirements.txt" -exec pip install -r {} \;

echo "Python dependencies installed."

# Execute the main command passed to the container
exec python run.py "$@"
11 changes: 11 additions & 0 deletions docker/post-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

echo "Monitoring for updates to publoader-extensions..."

while true; do
sleep 30
if docker ps --filter "label=com.centurylinklabs.watchtower.enable=true" | grep -q publoader-extensions; then
echo "publoader-extensions updated. Restarting publoader..."
docker compose restart publoader
fi
done
File renamed without changes.
26 changes: 16 additions & 10 deletions publoader/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ class DatabaseConnector(metaclass=Singleton):
def __init__(self):
self.database_uri = config["Credentials"]["mongodb_uri"]
self.database_name = config["Credentials"]["mongodb_db_name"]
self.database = self.connect_db()
self.database_connection = self.database[self.database_name]
self.database_connection = None

def connect_db(self):
client = pymongo.MongoClient(self.database_uri)
return client
if self.database_connection is None:
client = pymongo.MongoClient(self.database_uri)
self.database_connection = client[self.database_name]
return self.database_connection


database = DatabaseConnector()
database_connection = database.database_connection
image_filestream = gridfs.GridFS(database_connection, "images")


def get_database_connection():
return database.connect_db()


image_filestream = gridfs.GridFS(get_database_connection(), "images")


def convert_model_dict(chapter):
Expand Down Expand Up @@ -66,7 +72,7 @@ def update_database(chapter: Union[list, Union[Chapter, dict]], **kwargs):
return

try:
result = database_connection["uploaded"].bulk_write(
result = get_database_connection()["uploaded"].bulk_write(
[
UpdateOne(
{"md_chapter_id": {"$eq": chap["md_chapter_id"]}},
Expand All @@ -91,7 +97,7 @@ def update_database(chapter: Union[list, Union[Chapter, dict]], **kwargs):
)

try:
database_connection["uploaded_ids"].bulk_write(
get_database_connection()["uploaded_ids"].bulk_write(
[
UpdateOne(
{"chapter_id": {"$eq": chap["chapter_id"]}},
Expand Down Expand Up @@ -173,7 +179,7 @@ def update_expired_chapter_database(
)

try:
result = database_connection["to_delete"].bulk_write(
result = get_database_connection()["to_delete"].bulk_write(
[
UpdateOne(
{"md_chapter_id": {"$eq": chap["md_chapter_id"]}},
Expand All @@ -197,7 +203,7 @@ def update_expired_chapter_database(
f"Added {result.upserted_count} chapters to delete: {result.upserted_ids}"
)
try:
deleted_result = database_connection["uploaded"].bulk_write(
deleted_result = get_database_connection()["uploaded"].bulk_write(
[
DeleteOne(
{"md_chapter_id": {"$eq": chap["md_chapter_id"]}},
Expand Down
2 changes: 1 addition & 1 deletion publoader/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def main(self):
else None
)

description = "\n" + description
description = "\n" + description if description else None

embed = self.make_embed(title=title, description=description)
webhook.add_embed(embed)
Expand Down
6 changes: 3 additions & 3 deletions publoader/workers/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import pymongo

from publoader.http import http_client
from publoader.models.database import (
database_connection,
)
from publoader.models.database import get_database_connection
from publoader.utils.utils import root_path
from publoader.webhook import PubloaderQueueWebhook
from publoader.workers import worker as watcher_worker
Expand Down Expand Up @@ -84,6 +82,8 @@ def main(
queue_webhook = PubloaderQueueWebhook(
worker_type=worker_type, colour=webhook_colour
)
database_connection = get_database_connection()

worker_module = open_worker_module(worker_type)

# Turn-on the worker thread.
Expand Down

0 comments on commit 382b1f2

Please # to comment.