usable celery dev setup

commit 5438243b4874879196294a4072f245ebf22afd5d
Author: eneller <erikneller@gmx.de>
Date:   Sat Apr 5 00:12:55 2025 +0200

    minor celery

commit 3a03c498a7e6acff1afe350ce1ae196375d19029
Author: eneller <erikneller@gmx.de>
Date:   Fri Apr 4 23:40:23 2025 +0200

    begin celery
This commit is contained in:
eneller
2025-04-05 00:15:11 +02:00
parent 20de18fa13
commit 54eda4f9ea
8 changed files with 83 additions and 14 deletions

View File

@@ -1,17 +1,16 @@
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, FileResponse, HttpResponseBadRequest
from django.conf import settings
from celery import shared_task
from epub2go.convert import get_all_books, Book, GBConvert, allbooks_url
from epub2go.convert import get_all_books, Book, allbooks_url
import os
from urllib.parse import urlparse
import logging
from epub2go_web.tasks import getEpub
logger = logging.getLogger(__name__) #TODO configure logging
converter = GBConvert(downloaddir=settings.MEDIA_ROOT)
books = sorted(get_all_books(), key= lambda b: b.title)# TODO get from pickle
gbnetloc = urlparse(allbooks_url).netloc
@@ -28,7 +27,8 @@ def index(request: HttpRequest):
if targetParam:
if validateUrl(targetParam):
# download file
fpath = getEpub(targetParam)
result = getEpub.delay(targetParam)
fpath = result.get(timeout=60)
fname = os.path.basename(fpath)
file = open(fpath, 'rb')
response = FileResponse(file)
@@ -46,9 +46,3 @@ def validateUrl(param)->bool :
if(netloc == gbnetloc): return True
return False
# TODO make this async and show some indication of progress/loading
#@shared_task
def getEpub(book_url):
# TODO check for existing file and age
return converter.download(book_url)

View File

@@ -0,0 +1,5 @@
# 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
__all__ = ('celery_app',)

23
src/epub2go_web/celery.py Normal file
View File

@@ -0,0 +1,23 @@
# Start Celery workers from src/ using `celery -A epub2go_web worker --loglevel=INFO`
import os
from celery import Celery
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epub2go_web.settings')
app = Celery('epub2go_web')
# 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 apps.
app.autodiscover_tasks()
@app.task(bind=True, ignore_result=True)
def debug_task(self):
print(f'Request: {self.request!r}')

View File

@@ -69,6 +69,19 @@ TEMPLATES = [
WSGI_APPLICATION = "epub2go_web.wsgi.application"
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Celery settings
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"
CELERY_TASK_ALWAYS_EAGER = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

11
src/epub2go_web/tasks.py Normal file
View File

@@ -0,0 +1,11 @@
from celery import shared_task
from django.conf import settings
from epub2go.convert import GBConvert
converter = GBConvert(downloaddir=settings.MEDIA_ROOT)
@shared_task
def getEpub(book_url):
# TODO check for existing file and age
return converter.download(book_url)