Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions hackIDE/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from celery import shared_task

import requests

@shared_task
def async_json_post_request(URL, data):
r = requests.post(URL, data=data)
return r.json()
10 changes: 5 additions & 5 deletions hackIDE/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from django.shortcuts import render
from django.http import JsonResponse, HttpResponseForbidden
from models import codes
from .tasks import async_json_post_request

import requests, json, os
import json, os

COMPILE_URL = "https://api.hackerearth.com/v3/code/compile/"
RUN_URL = "https://api.hackerearth.com/v3/code/run/"
Expand Down Expand Up @@ -91,8 +92,8 @@ def compileCode(request):
'lang': lang,
}

r = requests.post(COMPILE_URL, data=compile_data)
return JsonResponse(r.json(), safe=False)
returned_data = async_json_post_request.delay(COMPILE_URL, compile_data).get()
return JsonResponse(returned_data, safe=False)
else:
return HttpResponseForbidden();

Expand Down Expand Up @@ -141,8 +142,7 @@ def runCode(request):
Make call to /run/ endpoint of HackerEarth API
and save code and result in database
"""
r = requests.post(RUN_URL, data=run_data)
r = r.json()
r = async_json_post_request.delay(RUN_URL, run_data).get()
cs = ""
rss = ""
rst = ""
Expand Down
5 changes: 5 additions & 0 deletions hackIDE_project/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
19 changes: 19 additions & 0 deletions hackIDE_project/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import absolute_import
from celery import Celery

import os

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hackIDE_project.settings')

# start celery app
app = Celery('hackIDE')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
10 changes: 9 additions & 1 deletion hackIDE_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
DEBUG = (os.environ.get('HACKIDE_DEBUG') != None)
# DEBUG = (os.environ.get('HACKIDE_DEBUG') or "").lower() == "true"

ALLOWED_HOSTS = ['hackide.herokuapp.com'] if not DEBUG else ['*']
ALLOWED_HOSTS = ['hackide.herokuapp.com', 'localhost', '127.0.0.1'] if not DEBUG else ['*']

# To allow the cross site request over the app
CORS_ORIGIN_ALLOW_ALL = True
Expand Down Expand Up @@ -138,3 +138,11 @@
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# Celery Configuration
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ urllib3==1.7.1
wheel==0.24.0
whitenoise==2.0.6
wsgiref==0.1.2
mongoengine==0.10.6
mongoengine==0.10.6
celery==4.1.0
redis==2.10.6