Skip to content

Commit 79ae914

Browse files
Changing number of workers
1 parent 304c833 commit 79ae914

File tree

6 files changed

+50
-32
lines changed

6 files changed

+50
-32
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
FROM python:3.9
1+
FROM python:3.9-slim
22
RUN mkdir -p /usr/src/app
33
WORKDIR /usr/src/app
44
COPY . .
55
RUN pip3 install --upgrade pip
66
RUN pip install -r requirements.txt
7-
CMD gunicorn app:app -b 0.0.0.0:8000 --workers 4
7+
CMD gunicorn app:app -b 0.0.0.0:8000 --workers 2 --worker-class gevent --timeout 60 --max-requests 1000 --max-requests-jitter 200

app.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from graphene import Schema
77
from src.schema import Query, Mutation
88
from src.utils.team_loader import TeamLoader
9-
import os
109
import signal
1110
import sys
1211
from dotenv import load_dotenv
@@ -15,39 +14,53 @@
1514

1615
app = Flask(__name__)
1716

17+
1818
@app.before_request
1919
def start_timer():
2020
g.start = time.time()
21-
21+
2222
if request.path == "/graphql" and request.method == "POST":
2323
try:
2424
# Try to extract the GraphQL query name for better logging
2525
query_data = request.get_json()
26-
if query_data and 'query' in query_data:
27-
g.query = query_data['query'].split("{", 1)[0].strip()
28-
logging.info(f"[{time.strftime('%H:%M:%S')}] --> GraphQL {g.query} started")
26+
if query_data and "query" in query_data:
27+
g.query = query_data["query"].split("{", 1)[0].strip()
28+
logging.info(
29+
f"[{time.strftime('%H:%M:%S')}] --> GraphQL {g.query} started"
30+
)
2931
except:
3032
pass
31-
32-
logging.info(f"[{time.strftime('%H:%M:%S')}] --> {request.method} {request.path} started")
33+
34+
logging.info(
35+
f"[{time.strftime('%H:%M:%S')}] --> {request.method} {request.path} started"
36+
)
37+
3338

3439
@app.after_request
3540
def log_response_time(response):
36-
if hasattr(g, 'start'):
41+
if hasattr(g, "start"):
3742
duration = time.time() - g.start
38-
43+
3944
if duration > 5.0: # Flag slow requests
40-
if hasattr(g, 'query'):
41-
logging.warning(f"[{time.strftime('%H:%M:%S')}] <-- SLOW GraphQL {g.query} ({duration:.2f}s)")
45+
if hasattr(g, "query"):
46+
logging.warning(
47+
f"[{time.strftime('%H:%M:%S')}] <-- SLOW GraphQL {g.query} ({duration:.2f}s)"
48+
)
4249
else:
43-
logging.warning(f"[{time.strftime('%H:%M:%S')}] <-- SLOW {request.method} {request.path} ({duration:.2f}s)")
50+
logging.warning(
51+
f"[{time.strftime('%H:%M:%S')}] <-- SLOW {request.method} {request.path} ({duration:.2f}s)"
52+
)
4453
else:
45-
if hasattr(g, 'query'):
46-
logging.info(f"[{time.strftime('%H:%M:%S')}] <-- GraphQL {g.query} finished in {duration:.2f}s")
54+
if hasattr(g, "query"):
55+
logging.info(
56+
f"[{time.strftime('%H:%M:%S')}] <-- GraphQL {g.query} finished in {duration:.2f}s"
57+
)
4758
else:
48-
logging.info(f"[{time.strftime('%H:%M:%S')}] <-- {request.method} {request.path} finished in {duration:.2f}s")
59+
logging.info(
60+
f"[{time.strftime('%H:%M:%S')}] <-- {request.method} {request.path} finished in {duration:.2f}s"
61+
)
4962
return response
50-
63+
5164

5265
# Configure logging
5366
logging.basicConfig(
@@ -58,18 +71,23 @@ def log_response_time(response):
5871

5972
schema = Schema(query=Query, mutation=Mutation)
6073

74+
6175
def create_context():
62-
return {
63-
"team_loader": TeamLoader()
64-
}
76+
return {"team_loader": TeamLoader()}
77+
6578

6679
app.add_url_rule(
67-
"/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True, get_context=create_context)
80+
"/graphql",
81+
view_func=GraphQLView.as_view(
82+
"graphql", schema=schema, graphiql=True, get_context=create_context
83+
),
6884
)
6985

86+
7087
def signal_handler(sig, frame):
7188
sys.exit(0)
7289

90+
7391
signal.signal(signal.SIGINT, signal_handler)
7492
signal.signal(signal.SIGTERM, signal_handler)
7593

src/models/game.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bson.objectid import ObjectId
2-
from src.database import db
32

43

54
class Game:

src/models/team.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bson.objectid import ObjectId
2-
from src.database import db
32

43

54
class Team:

src/models/youtube_video.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bson.objectid import ObjectId
2-
from src.database import db
32

43

54
class YoutubeVideo:
@@ -15,7 +14,9 @@ class YoutubeVideo:
1514
- `published_at` The date and time the video was published.
1615
"""
1716

18-
def __init__(self, title, description, thumbnail, b64_thumbnail, url, published_at, id=None):
17+
def __init__(
18+
self, title, description, thumbnail, b64_thumbnail, url, published_at, id=None
19+
):
1920
self.id = id if id else str(ObjectId())
2021
self.title = title
2122
self.description = description

src/scrapers/youtube_stats.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from dotenv import load_dotenv
44
from src.models.youtube_video import YoutubeVideo
55
from src.services.youtube_video_service import YoutubeVideoService
6-
from src.database import db
76
import base64
87
import os
98
import html
109

1110
load_dotenv()
1211
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
1312

13+
1414
def fetch_videos():
1515
"""
1616
Fetches the latest videos from the YouTube API and stores them in the database.
@@ -24,6 +24,7 @@ def fetch_videos():
2424
continue
2525
process_video_item(item)
2626

27+
2728
def process_video_item(item):
2829
"""
2930
Extracts the required data from a video item and
@@ -33,7 +34,7 @@ def process_video_item(item):
3334
video_id = item["id"]["videoId"]
3435

3536
# video metadata
36-
snippet = item["snippet"]
37+
snippet = item["snippet"]
3738
title = html.unescape(snippet.get("title"))
3839
description = html.unescape(snippet.get("description"))
3940

@@ -47,15 +48,15 @@ def process_video_item(item):
4748
try:
4849
response = requests.get(thumbnail)
4950
response.raise_for_status()
50-
encoded_thumbnail = base64.b64encode(response.content).decode('utf-8')
51+
encoded_thumbnail = base64.b64encode(response.content).decode("utf-8")
5152
except Exception as e:
5253
print(f"Error fetching thumbnail: {e}")
53-
54+
5455
published_at = snippet.get("publishedAt")
5556
video_url = f"https://www.youtube.com/watch?v={video_id}"
5657

5758
video_data = {
58-
"id": video_id, # use video id for easy retrieval
59+
"id": video_id, # use video id for easy retrieval
5960
"title": title,
6061
"description": description,
6162
"thumbnail": thumbnail,
@@ -74,4 +75,4 @@ def process_video_data(video_data):
7475
if existing_video:
7576
return
7677

77-
YoutubeVideoService.create_video(video_data)
78+
YoutubeVideoService.create_video(video_data)

0 commit comments

Comments
 (0)