Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
added support for pypy3
Browse files Browse the repository at this point in the history
  • Loading branch information
alimaktabi committed Sep 16, 2024
1 parent 2d895e6 commit 37a3d00
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Use an official Python runtime as a parent image
FROM python:3.10.11
FROM pypy:3.10

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
Expand All @@ -11,7 +10,6 @@ WORKDIR /usr/src/app
RUN apt update && apt install gcc



# RUN apt-get update && \
# apt-get install -y --no-install-recommends \
# build-essential \
Expand All @@ -27,6 +25,7 @@ RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

RUN ln -s /usr/local/bin/pypy3 /usr/local/bin/python


# Copy project
Expand All @@ -35,4 +34,4 @@ COPY ./start.sh .
COPY ./celery.sh .


RUN python manage.py collectstatic --noinput
RUN pypy3 manage.py collectstatic --noinput
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: "wits"
services:

celery:
platform: linux/arm64
build: .
command: celery -A witswin worker --beat --concurrency 1 -l INFO
volumes:
Expand All @@ -20,11 +21,12 @@ services:
- base

app:
platform: linux/arm64
extra_hosts:
- "host.docker.internal:host-gateway"

build: .
command: python manage.py runserver 0.0.0.0:4444
command: pypy manage.py runserver 0.0.0.0:4444
environment:
- FIELD_KEY=${FIELD_KEY}
- SECRET_KEY=${SECRET_KEY}
Expand Down Expand Up @@ -58,6 +60,12 @@ services:
- base
ports:
- 5432:5432

healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5

redis:
image: redis:6.2-alpine
Expand Down
12 changes: 4 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
bip-utils==2.3.0
django==5
anchorpy==0.15.0
djangorestframework==3.15.2
djangorestframework-camel-case==1.4.2
dj-database-url==0.5.0
Expand All @@ -14,26 +13,23 @@ django-polymorphic==3.1.0
django-rest-polymorphic==0.1.10
drf-yasg==1.20.0
ed25519==1.5
psycopg2-binary==2.9.3
gunicorn==20.1.0
locust==2.26.0
redis==4.6
python-dotenv==0.20.0
python-memcached==1.59
sentry-sdk==1.5.12
uWSGI==2.0.22
# sentry-sdk==1.5.12
web3==6.9.0
whitenoise==6.1.0
solders==0.14.3
coverage~=7.3.2
pytz~=2023.3.post1
requests~=2.31.0
celery~=5.4.0
celery-redbeat==2.2.0
django-safedelete~=1.3.3
tweepy~=4.14.0
# django-safedelete~=1.3.3
pillow==10.4.0
channels[daphne]==4.1
channels_redis==4.2
django-cloudflare-images~=0.6.0
python-decouple==3.8
python-decouple==3.8
psycopg[binary]==3.2.2
48 changes: 36 additions & 12 deletions src/quiz/signals.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
from celery import current_app
from django.utils import timezone
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django_celery_beat.models import PeriodicTask, CrontabSchedule, ClockedSchedule
from quiz.models import Competition
from quiz.tasks import setup_competition_to_start
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

Expand All @@ -12,20 +13,18 @@
def clean_competition_task(sender, instance: Competition, **kwargs):
channel_layer = get_channel_layer()

current_app.control.revoke(f"start_competition_{instance.pk}", terminate=True) # type: ignore
current_app.control.revoke(f"start_competition_{instance.pk}", terminate=True) # type: ignore

async_to_sync(channel_layer.group_send)( # type: ignore
f"quiz_list",
{"type": "delete_competition", "data": instance.pk},
)



@receiver(post_save, sender=Competition)
def trigger_competition_starter_task(sender, instance: Competition, created, **kwargs):

channel_layer = get_channel_layer()

async_to_sync(channel_layer.group_send)( # type: ignore
f"quiz_list",
{"type": "update_competition_data", "data": instance.pk},
Expand All @@ -39,11 +38,36 @@ def trigger_competition_starter_task(sender, instance: Competition, created, **k
if start_time < timezone.now():
return

# This assumes the task is scheduled with a unique name using the competition ID.
# current_app.control.revoke(f"start_competition_{instance.pk}", terminate=True)
# Check if the task already exists and delete the old task if necessary
existing_task_name = f"start_competition_{instance.pk}"

try:
old_task = PeriodicTask.objects.get(name=existing_task_name)
old_task.delete()
except PeriodicTask.DoesNotExist:
pass

# Create a new crontab schedule
clocked_schedule, created = ClockedSchedule.objects.get_or_create(
clocked_time=start_time
- timezone.timedelta(seconds=10) # or use start_time directly
)

# Now create a new PeriodicTask with the new schedule
PeriodicTask.objects.create(
clocked=clocked_schedule, # Use ClockedSchedule for one-time execution
name=existing_task_name, # Unique task name
task="quiz.tasks.setup_competition_to_start", # The task to be executed
args=json.dumps([instance.pk]), # Pass the instance ID as an argument
one_off=True, # Ensure it's a one-time task
)


# This assumes the task is scheduled with a unique name using the competition ID.
# current_app.control.revoke(f"start_competition_{instance.pk}", terminate=True)

setup_competition_to_start.apply_async(
args=[instance.pk],
eta=start_time - timezone.timedelta(seconds=10),
task_id=f"start_competition_{instance.pk}",
) # type: ignore
# setup_competition_to_start.apply_async(
# args=[instance.pk],
# eta=start_time - timezone.timedelta(seconds=10),
# task_id=f"start_competition_{instance.pk}",
# ) # type: ignore
2 changes: 1 addition & 1 deletion src/witswin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("POSTGRES_DB", "wits"),
"USER": os.environ.get("POSTGRES_USER", "postgres"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD", "postgres"),
Expand Down

0 comments on commit 37a3d00

Please sign in to comment.