Skip to content

Commit 750acf5

Browse files
committed
Use seperate container for running playwright
1 parent 0e9c6b4 commit 750acf5

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ RUN --mount=type=cache,target=$UV_CACHE_DIR \
3333
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
3434
uv sync --frozen --no-install-project --all-groups
3535

36-
RUN playwright install \
37-
&& playwright install-deps
38-
3936
# To avoid some SyntaxWarnings ("is" with a literal), still needed on 20241024:
4037
ENV AZUREROOT=/usr/local/lib/python3.11/site-packages/azure/storage/
4138
RUN perl -pi -e 's/ is 0 / == 0 /' ${AZUREROOT}blob/_upload_chunking.py

api/tasks.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def generate_url(url, export_id, user, title):
5555
export = Export.objects.get(id=export_id)
5656
user = User.objects.get(id=user)
5757
token = Token.objects.filter(user=user).last()
58+
logger.info(f"Starting export: {export.pk}")
5859

5960
footer_template = """
6061
<div class="footer" style="width: 100%;font-size: 8px;color: #FEFEFE; bottom: 10px; position: absolute;">
@@ -86,19 +87,7 @@ def generate_url(url, export_id, user, title):
8687
try:
8788
with tempfile.TemporaryDirectory() as tmp_dir:
8889
with sync_playwright() as p:
89-
browser = p.chromium.launch(
90-
headless=True,
91-
args=[
92-
# https://github.com/microsoft/playwright-python/issues/1453
93-
# Usually required when running AWS lambda
94-
# "--single-process",
95-
"--no-zygote",
96-
"--no-sandbox",
97-
"--disable-setuid-sandbox",
98-
"--disable-dev-shm-usage",
99-
],
100-
devtools=False,
101-
)
90+
browser = p.chromium.connect(settings.PLAYWRIGHT_SERVER_URL)
10291
storage_state = build_storage_state(
10392
tmp_dir,
10493
user,
@@ -108,7 +97,8 @@ def generate_url(url, export_id, user, title):
10897
page = context.new_page()
10998
if settings.DEBUG_PLAYWRIGHT:
11099
DebugPlaywright.debug(page)
111-
timeout = 300000
100+
# FIXME: Use of Timeout correct?
101+
timeout = 300_000 # 5 min
112102
page.goto(url, timeout=timeout)
113103
time.sleep(5)
114104
page.wait_for_selector("#pdf-preview-ready", state="attached", timeout=timeout)
@@ -143,3 +133,4 @@ def generate_url(url, export_id, user, title):
143133
)
144134
export.status = Export.ExportStatus.ERRORED
145135
export.save(update_fields=["status"])
136+
logger.info(f"End export: {export.pk}")

docker-compose.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ x-server: &base_server_setup
1919
GO_ENVIRONMENT: ${GO_ENVIRONMENT:-development}
2020
API_FQDN: ${API_FQDN:-http://localhost:8000}
2121
FRONTEND_URL: ${FRONTEND_URL:-http://localhost:3000}
22+
PLAYWRIGHT_SERVER_URL: ${PLAYWRIGHT_SERVER_URL:-ws://playwright:3000/}
2223
GO_WEB_INTERNAL_URL: ${GO_WEB_INTERNAL_URL:-http://host.docker.internal:3000}
2324
DJANGO_ADDITIONAL_ALLOWED_HOSTS: ${DJANGO_ADDITIONAL_ALLOWED_HOSTS:-host.docker.internal}
2425
DEBUG_EMAIL: ${DEBUG_EMAIL:-true}
@@ -57,7 +58,7 @@ x-server: &base_server_setup
5758
- db
5859
- redis
5960
- elasticsearch
60-
61+
- playwright
6162

6263
services:
6364
db:
@@ -76,7 +77,15 @@ services:
7677
volumes:
7778
- redis-data:/data
7879

79-
# NOTE: Used Only for local development
80+
# NOTE: Make sure this matches with pyproject playwright dependency
81+
playwright:
82+
image: mcr.microsoft.com/playwright:v1.50.0-noble
83+
working_dir: /home/pwuser
84+
user: pwuser
85+
command: bash -c "npx -y [email protected] run-server --port 3000 --host 0.0.0.0"
86+
extra_hosts:
87+
- "host.docker.internal:host-gateway"
88+
8089
elasticsearch:
8190
image: docker.elastic.co/elasticsearch/elasticsearch:7.0.0
8291
container_name: elasticsearch
@@ -96,7 +105,6 @@ services:
96105
ports:
97106
- 9200:9200
98107

99-
# NOTE: Used Only for local development
100108
kibana:
101109
image: 'docker.elastic.co/kibana/kibana:7.0.0'
102110
container_name: kibana

main/runserver.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ python manage.py collectstatic --noinput -l
99
#python manage.py make_permissions
1010

1111
# Add server name(s) to django settings and nginx - later maybe only nginx would be enough, and ALLOWED_HOSTS could be "*"
12+
# TODO: Allow API_FQDN with https
1213
if [ "$GO_ENVIRONMENT"x = productionx ]; then
1314
sed -i 's/\$NGINX_SERVER_NAME/'$API_FQDN' api.go.ifrc.org/g' /etc/nginx/sites-available/nginx.conf
1415
sed -i 's/CHANGE_ME_BEFORE_START/prod/' /etc/nginx/sites-available/nginx.conf

main/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
API_FQDN=str, # https://goadmin.ifrc.org
3434
FRONTEND_URL=str, # https://go.ifrc.org
3535
GO_WEB_INTERNAL_URL=(str, None), # http://host.docker.internal
36+
PLAYWRIGHT_SERVER_URL=str, # ws://playwright:3000/
3637
# Database
3738
DJANGO_DB_NAME=str,
3839
DJANGO_DB_USER=str,
@@ -173,6 +174,8 @@ def parse_domain(*env_keys: str) -> str:
173174
GO_WEB_INTERNAL_URL = parse_domain("GO_WEB_INTERNAL_URL", "FRONTEND_URL")
174175
FRONTEND_URL = urlparse(GO_WEB_URL).hostname # FIXME: Deprecated. Slowly remove this from codebase
175176

177+
PLAYWRIGHT_SERVER_URL = env("PLAYWRIGHT_SERVER_URL")
178+
176179
INTERNAL_IPS = ["127.0.0.1"]
177180
if env("DOCKER_HOST_IP"):
178181
INTERNAL_IPS.append(env("DOCKER_HOST_IP"))

0 commit comments

Comments
 (0)