Skip to content

Commit 0372790

Browse files
committed
Celery -> RQ, fix task overflowing, docker prod
Fix check for whether new parsing task should be created Infra update, add prod containers and compose
1 parent 7359451 commit 0372790

File tree

19 files changed

+157
-274
lines changed

19 files changed

+157
-274
lines changed

.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ celerybeat.pid
107107
*.sage.py
108108

109109
# Environments
110-
.envs/.env
110+
.env
111111
.venv
112112
env/
113113
venv/
@@ -150,3 +150,5 @@ LICENSE
150150
README.md
151151
redis-data
152152
redis.conf
153+
staticfiles/
154+
docker/

.idea/backend.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/core/apps.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
from django.apps import AppConfig
2-
from django.core.checks import register
3-
4-
from apps.core.checks import check_redis
52

63

74
class CoreConfig(AppConfig):
85
name = "apps.core"
96
verbose_name = "Core"
10-
11-
def ready(self) -> None:
12-
register(check_redis)
13-
return super().ready()

apps/core/checks.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

apps/core/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import re
22

3-
import redis
4-
from django.conf import settings
5-
6-
7-
def init_redis_client() -> redis.Redis:
8-
return redis.StrictRedis.from_url(settings.REDIS_URL, decode_responses=True)
9-
103

114
def url_prefix(url: str) -> str:
125
return re.match(r"(^https?://(.*))/.*$", url).group(1)

apps/manga/api/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def get_manga(request, manga_id: int):
3333
cache_entry = cache.get(manga.source_url)
3434
if cache_entry and cache_entry != ParsingStatus.parsing.value:
3535
return cache_entry
36-
else:
36+
elif not cache_entry:
3737
cache.set(manga.source_url, ParsingStatus.parsing.value)
38-
run_spider_task(DETAIL_PARSER, url=manga.source_url)
38+
run_spider_task.delay(DETAIL_PARSER, url=manga.source_url)
3939

4040
return MangaOut(
4141
status=ParsingStatus.parsing.value,
@@ -53,9 +53,9 @@ def get_chapters(request, manga_id: int):
5353
cache_entry = cache.get(manga.rss_url)
5454
if cache_entry and cache_entry != ParsingStatus.parsing.value:
5555
return cache_entry
56-
else:
56+
elif not cache_entry:
5757
cache.set(manga.rss_url, ParsingStatus.parsing.value)
58-
run_spider_task(CHAPTER_PARSER, url=manga.rss_url)
58+
run_spider_task.delay(CHAPTER_PARSER, url=manga.rss_url)
5959

6060
return ChapterListOut(
6161
status=ParsingStatus.parsing.value,

apps/parse/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from celery import shared_task
1+
from django_rq import job
22
from scrapy.utils.project import get_project_settings
33
from scrapyscript import Job, Processor
44

55
from apps.parse.parser import PARSERS
66

77

8-
@shared_task
8+
@job
99
def run_spider_task(parser_type: str, catalogue_name: str = "readmanga", url: str = None):
1010
parser_map = PARSERS[catalogue_name]
1111
spider, _ = parser_map[parser_type]
12-
job = Job(spider, url=url)
12+
j = Job(spider, url=url)
1313
p = Processor(settings=get_project_settings())
14-
p.run(job)
14+
p.run(j)

apps/readmanga/images.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from orjson import loads
55
from scrapy.http import HtmlResponse
66

7-
from apps.core.utils import init_redis_client
87
from apps.parse.items import ImagesItem
98
from apps.parse.spider import InjectUrlMixin
109

@@ -18,7 +17,7 @@ class ReadmangaImageSpider(InjectUrlMixin, scrapy.Spider):
1817
"Chapter url, like https://readmanga.live/podniatie_urovnia_v_odinochku__A5664/vol1/0"
1918

2019
def __init__(self, *args, url: str, **kwargs):
21-
super().__init__(*args, **kwargs, start_urls=[url], redis_client=init_redis_client())
20+
super().__init__(*args, **kwargs, start_urls=[url])
2221

2322
def parse(self, response: HtmlResponse, **kwargs):
2423
images = re.search(r"rm_h.initReader\(.*(\[{2}.*]{2}).*\)", response.text)

docker-compose.yml

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,50 @@
1-
# Compose for development using volumes to ensure hot reload
2-
#version: "3"
3-
41
services:
52
web:
63
profiles:
74
- app
85
build:
96
context: .
10-
dockerfile: docker/web/Dockerfile.dev
7+
dockerfile: docker/web/Dockerfile
8+
restart: unless-stopped
119
expose:
1210
- 8000
1311
environment:
1412
- DEBUG
1513
- ALLOWED_HOSTS
14+
- SECRET_KEY
1615
- DATABASE_URL=postgres://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-sora}
17-
- SECRET_KEY=${SECRET_KEY:-DevServer}
1816
# Static
1917
- HOST=0.0.0.0
2018
- PORT=8000
2119
- REDIS_URL=redis://redis:6379
2220
- TYPESENSE_HOST=typesense
2321
ports:
24-
- "${PORT:-8888}:8000"
25-
volumes:
26-
- ./:/app
27-
- sora_poetry:/home/sora/.cache/pypoetry
22+
- "${PORT:-80}:8000"
2823
healthcheck:
2924
test: [ "CMD", "curl", "http://localhost:8000/api/health" ]
3025
interval: 5s
3126
timeout: 5s
3227

33-
celery:
28+
rq:
3429
profiles:
3530
- app
36-
- base
3731
image: backend_web
38-
command: poetry run celery -A manga_reader worker -l INFO
32+
restart: unless-stopped
33+
command: poetry run python manage.py rqworker default
3934
environment:
4035
- DEBUG
4136
- PROXY
37+
- SECRET_KEY
4238
- DATABASE_URL=postgres://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-sora}
43-
- SECRET_KEY=${SECRET_KEY:-DevServer}
4439
- REDIS_URL=redis://redis:6379
4540
- TYPESENSE_HOST=typesense
46-
volumes:
47-
- ./:/app
48-
- sora_poetry:/home/sora/.cache/pypoetry
4941
depends_on:
5042
- web
51-
healthcheck:
52-
test: poetry run celery -A manga_reader status
53-
interval: 5s
54-
timeout: 5s
5543

5644
db:
5745
profiles:
5846
- data
5947
- app
60-
- base
6148
restart: unless-stopped
6249
image: postgres:12
6350
ports:
@@ -78,7 +65,6 @@ services:
7865
profiles:
7966
- data
8067
- app
81-
- base
8268
image: redis
8369
command: redis-server
8470
restart: unless-stopped
@@ -100,7 +86,6 @@ services:
10086
profiles:
10187
- data
10288
- app
103-
- base
10489
image: typesense/typesense:0.23.1
10590
restart: unless-stopped
10691
command: --data-dir /data --api-key=$TYPESENSE_API_KEY --enable-cors
@@ -111,6 +96,5 @@ services:
11196

11297
volumes:
11398
tsdata:
114-
sora_poetry:
11599
sora_redis:
116100
sora_postgres:

docker/web/docker-entrypoint.sh renamed to docker-entrypoint.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ if $(is_debug); then
1414
info "Debug mode" "Installing dependencies"
1515
sudo chown sora:sora ~/.cache/pypoetry/
1616
poetry install
17-
else
18-
info "Production mode" "Collecting static files to /app/staticfiles"
19-
poetry run ./manage.py collectstatic --no-input --clear
2017
fi
2118

2219
info "Waiting for postgres"
@@ -34,9 +31,6 @@ until curl "http://$TYPESENSE_HOST:8108/health"; do
3431
sleep 1
3532
done
3633

37-
#info "Rebuilding index"
38-
#poetry run ./manage.py rebuild_index
39-
4034
# Get core count
4135
core_count=$(grep 'cpu[0-9]+' /proc/stat | wc -l)
4236
# Calculate worker count, max 12

0 commit comments

Comments
 (0)