Skip to content

Commit d5bea15

Browse files
committed
Address comments.
1 parent 03a0759 commit d5bea15

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static
2+
.idea
3+
.env
4+
db.sqlite3
5+
*.iml

sample-apps/python/django_frontend_service/django_frontend_service/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
## SPDX-License-Identifier: Apache-2.0
33
import os
44
from pathlib import Path
5+
from dotenv import load_dotenv
56

7+
load_dotenv()
68
# Build paths inside the project like this: BASE_DIR / 'subdir'.
79
BASE_DIR = Path(__file__).resolve().parent.parent
810

sample-apps/python/django_frontend_service/frontend_service_app/views.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
## SPDX-License-Identifier: Apache-2.0
13
from django.http import HttpResponse, JsonResponse
24
import boto3
35
import logging
46
import requests
57
import schedule
68
import time
7-
from threading import Thread
9+
import threading
810
from opentelemetry import trace
9-
## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
10-
## SPDX-License-Identifier: Apache-2.0
1111
from opentelemetry.trace.span import format_trace_id
1212

1313
logger = logging.getLogger(__name__)
1414

1515
should_send_local_root_client_call = False
16+
lock = threading.Lock()
1617
def run_local_root_client_call_recurring_service():
1718
def runnable_task():
1819
global should_send_local_root_client_call
19-
if should_send_local_root_client_call:
20-
should_send_local_root_client_call = False
21-
try:
22-
response = requests.get("http://local-root-client-call")
23-
# Handle the response if needed
24-
except Exception as e:
25-
# Handle exceptions
26-
pass
20+
with lock:
21+
if should_send_local_root_client_call:
22+
should_send_local_root_client_call = False
23+
try:
24+
response = requests.get("http://local-root-client-call")
25+
# Handle the response if needed
26+
except Exception as e:
27+
# Handle exceptions
28+
pass
2729

2830
# Schedule the task to run every 1 second
2931
schedule.every(1).seconds.do(runnable_task)
@@ -34,7 +36,7 @@ def run_scheduler():
3436
schedule.run_pending()
3537
time.sleep(0.1) # Sleep to prevent high CPU usage
3638

37-
thread = Thread(target=run_scheduler)
39+
thread = threading.Thread(target=run_scheduler)
3840
thread.daemon = True # Daemonize the thread so it exits when the main thread exits
3941
thread.start()
4042

@@ -68,8 +70,7 @@ def http_call(request):
6870

6971
def downstream_service(request):
7072
ip = request.GET.get('ip', '')
71-
print("ip!!!!!!!!!!!!!")
72-
print(ip)
73+
ip = ip.replace("/", "")
7374
url = f"http://{ip}:8001/health-check"
7475
try:
7576
response = requests.get(url)
@@ -86,13 +87,14 @@ def async_service(request):
8687
# Log the request
8788
logger.info("Client-call received")
8889
# Set the condition to trigger the recurring service
89-
should_send_local_root_client_call = True
90+
with lock:
91+
should_send_local_root_client_call = True
9092
# Return a dummy traceId in the response
9193
return JsonResponse({'traceId': '1-00000000-000000000000000000000000'})
9294

9395
def get_xray_trace_id():
9496
span = trace.get_current_span()
9597
trace_id = format_trace_id(span.get_span_context().trace_id)
96-
xray_trace_id = "1-" + trace_id[:8] + "-" + trace_id[8:]
98+
xray_trace_id = f"1-{trace_id[:8]}-{trace_id[8:]}"
9799

98-
return "traceId : " + xray_trace_id
100+
return JsonResponse({"traceId": xray_trace_id})

sample-apps/python/django_frontend_service/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ requests~=2.31.0
33
boto3~=1.34.3
44
requests~=2.31.0
55
schedule~=1.2.1
6+
python-dotenv~=1.0.0
67
opentelemetry-api~=1.22.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static
2+
.idea
3+
.env
4+
db.sqlite3
5+
*.iml

sample-apps/python/docker-compose.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ services:
1010
container_name: django_frontend_service
1111
command: sh -c "python3 manage.py migrate --noinput && python3 manage.py collectstatic --noinput && python3 manage.py runserver 0.0.0.0:8000 --noreload"
1212
restart: always
13-
volumes:
14-
- $HOME/.aws/credentials:/home/app/.aws/credentials:ro
1513
environment:
1614
DJANGO_SETTINGS_MODULE: "django_frontend_service.settings"
1715
ports:

0 commit comments

Comments
 (0)