Kaydet (Commit) c7eab392 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

better quiting and container cleaning, cronjobs

üst 7ee354f0
......@@ -15,6 +15,7 @@ gunicorn = "*"
whitenoise = "*"
docker = "*"
evality = "==0.2.1"
django-cron = "*"
[scripts]
start = "gunicorn aspava.wsgi:application --bind localhost:8000"
......
{
"_meta": {
"hash": {
"sha256": "f7d5f6bc4fbca2893bd56d1a4941e33a77186a659547c40ffcc69188771f1deb"
"sha256": "99500a84e640f527b14abf69b834ae7c8dc17600ae81655272680cf080d79540"
},
"pipfile-spec": 6,
"requires": {
......@@ -53,6 +53,19 @@
"index": "pypi",
"version": "==0.0.8"
},
"django-common-helpers": {
"hashes": [
"sha256:2d56be6fa261d829a6a224f189bf276267b9082a17d613fe5f015dd4d65c17b4"
],
"version": "==0.9.2"
},
"django-cron": {
"hashes": [
"sha256:08d22708c8b2ecab8cda989019a66c7e1e2424c59d822796fd45abf7731d261d"
],
"index": "pypi",
"version": "==0.5.1"
},
"django-ranged-response": {
"hashes": [
"sha256:f71fff352a37316b9bead717fc76e4ddd6c9b99c4680cdf4783b9755af1cf985"
......
......@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
......@@ -19,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'e+4_$yoyv)m*_xs!secifabey&he#18p&e_n06=%7qdu%2)8!4'
SECRET_KEY = "e+4_$yoyv)m*_xs!secifabey&he#18p&e_n06=%7qdu%2)8!4"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
......@@ -30,57 +31,58 @@ ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social',
'bootstrap4',
'django_ace',
'captcha'
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"social",
"bootstrap4",
"django_ace",
"captcha",
"django_cron",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = 'aspava.urls'
ROOT_URLCONF = "aspava.urls"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
]
},
},
}
]
WSGI_APPLICATION = 'aspava.wsgi.application'
WSGI_APPLICATION = "aspava.wsgi.application"
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
......@@ -94,9 +96,9 @@ AUTH_PASSWORD_VALIDATORS = []
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"
TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"
USE_I18N = True
......@@ -108,15 +110,18 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_URL = "/static/"
# Settings
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
AUTH_USER_MODEL = 'social.SocialUser'
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
AUTH_USER_MODEL = "social.SocialUser"
# Extra Settings
MAX_POST_LENGTH = 144 * 2
AVATAR_BASE = 'img/avatars/avatar_{}.png'
AVATAR_BASE = "img/avatars/avatar_{}.png"
DESC = "Apparently, this user prefers to keep an air of mystery about them."
# Crons
CRON_CLASSES = ["social.cronjobs.RenewEvalityCron"]
......@@ -19,6 +19,6 @@ from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('social.urls')),
path('admin/', admin.site.urls),
path("", include("social.urls")),
path("admin/", admin.site.urls),
] + static(settings.STATIC_URL)
......@@ -2,12 +2,9 @@
import atexit
import os
import sys
from social.utils import quit_evality
@atexit.register
def quit():
from social.views import evality
evality.quit()
atexit.register(quit_evality)
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aspava.settings")
try:
......
from social.cronjobs.renew_evality import RenewEvalityCron
from django_cron import CronJobBase, Schedule
from social.utils import quit_evality
class RenewEvalityCron(CronJobBase):
RUN_EVERY_MINS = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'social.cronjobs.RenewEvalityCron'
def do(self):
quit_evality()
from social.utils.quitter import quit_evality
import docker.errors
from contextlib import suppress
def quit_evality():
from social.views import evality, docker_client
evality.quit()
for container in docker_client.containers.list():
if "evality:latest" in container.image.tags:
with suppress(docker.errors.DockerException):
container.kill()
import docker
import docker.errors
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
......@@ -29,7 +30,7 @@ def run_snippet(request):
global evality
try:
result = evality.run_cmd(snippet.text, snippet.author.id)
except docker.exceptions.DockerException:
except docker.errors.DockerException:
evality.quit()
result['id'] = snippet.id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment