Skip to content

Commit

Permalink
Merge pull request #1 from dex3r/main
Browse files Browse the repository at this point in the history
Added auto_worker_type endpoint
  • Loading branch information
dex3r authored Sep 9, 2024
2 parents 3f2bcff + 7684665 commit 135503a
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ venv/
ENV/
env.bak/
venv.bak/
.env_docker
env_docker/
env_docker.bak/

# Spyder project settings
.spyderproject
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ services:
- redis

postgres:
image: postgres:15.3-alpine
build:
context: docker/postgres
dockerfile: dockerfile
container_name: postgres
restart: always
environment:
Expand Down
7 changes: 7 additions & 0 deletions docker/postgres/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM postgres:15.3
RUN apt-get update && apt-get install -y postgresql-15-cron

RUN echo "shared_preload_libraries='pg_cron'" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "cron.database_name='postgres'" >> /usr/share/postgresql/postgresql.conf.sample

COPY init-db /docker-entrypoint-initdb.d
19 changes: 19 additions & 0 deletions docker/postgres/init-db/002-pg-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# use same db as the one from env
dbname="postgres"

# create custom config
customconf=/var/lib/postgresql/data/custom-conf.conf
echo "" > $customconf
echo "shared_preload_libraries = 'pg_cron'" >> $customconf
echo "cron.database_name = '$dbname'" >> $customconf
chown postgres $customconf
chgrp postgres $customconf

# include custom config from main config
conf=/var/lib/postgresql/data/postgresql.conf
found=$(grep "include = '$customconf'" $conf)
if [ -z "$found" ]; then
echo "include = '$customconf'" >> $conf
fi
1 change: 1 addition & 0 deletions docker/postgres/init-db/003-main.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION pg_cron;
12 changes: 12 additions & 0 deletions horde/apis/models/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,3 +1549,15 @@ def __init__(self, api):
),
},
)

self.response_model_auto_worker_type = api.model(
"AutoWorkerType",
{
"recommended_worker_type": fields.String(
example="image",
description="The recommended type of worker.",
enum=["image", "text"],
required=True,
),
},
)
1 change: 1 addition & 0 deletions horde/apis/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@
api.add_resource(base.DocsTerms, "/documents/terms")
api.add_resource(base.DocsPrivacy, "/documents/privacy")
api.add_resource(base.DocsSponsors, "/documents/sponsors")
api.add_resource(base.AutoWorkerType, "/auto_worker_type")
53 changes: 53 additions & 0 deletions horde/apis/v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3070,3 +3070,56 @@ def get(self):
if self.args.format == "markdown":
return {"markdown": markdownify(html_template).strip("\n")}, 200
return {"html": html_template}, 200

class AutoWorkerType(Resource):
get_parser = reqparse.RequestParser()
get_parser.add_argument(
"apikey",
type=str,
required=True,
help="A User API key.",
location="headers",
)
get_parser.add_argument(
"Client-Agent",
default="unknown:0:unknown",
type=str,
required=False,
help="The client name and version.",
location="headers",
)

@api.expect(get_parser)
@api.marshal_with(
models.response_model_auto_worker_type,
code=200,
description="Recommended worker type details",
skip_none=True,
)
@api.response(400, "Validation Error", models.response_model_error)
@api.response(401, "Invalid API Key", models.response_model_error)
def get(self):
"""Get the recommended worker type for the user"""
self.args = self.get_parser.parse_args()
self.user = database.find_user_by_api_key(self.args["apikey"])
if not self.user:
raise e.InvalidAPIKey("get auto worker type")

active_workers = database.get_active_workers()
image_workers_count = 0
text_workers_count = 0

for worker in active_workers:
if worker.worker_type == "image":
image_workers_count += 1
elif worker.worker_type == "text":
text_workers_count += 1
else:
logger.warning(f"Unknown worker type {worker.worker_type} for worker {worker.id}")

logger.debug(f"Image workers: {image_workers_count} Text workers: {text_workers_count}")

if image_workers_count > text_workers_count:
return {"recommended_worker_type": "text"}, 200
else:
return {"recommended_worker_type": "image"}, 200

0 comments on commit 135503a

Please sign in to comment.