Skip to content

Commit ddf478b

Browse files
pamfilosdrjova
authored andcommitted
articles: add index command as task, batched
Signed-off-by: pamfilos <[email protected]>
1 parent 162fcb7 commit ddf478b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

scoap3/articles/tasks.py

+19
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,22 @@ def compliance_checks(article_id):
162162
report.save()
163163
logger.info("Compliance checks completed for article %s", article_id)
164164
return f"Compliance checks completed for article {article_id}"
165+
166+
167+
from django.core.paginator import Paginator
168+
from scoap3.articles.models import Article
169+
from django_opensearch_dsl.registries import registry
170+
171+
@shared_task
172+
def index_article_batch(article_ids):
173+
articles = Article.objects.filter(id__in=article_ids)
174+
for article in articles:
175+
registry.update(article)
176+
177+
def index_all_articles(batch_size=100):
178+
all_articles = Article.objects.all().values_list('id', flat=True)
179+
paginator = Paginator(all_articles, batch_size)
180+
181+
for page_number in paginator.page_range:
182+
page = paginator.page(page_number)
183+
index_article_batch.delay(list(page.object_list))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import math
2+
3+
from django.core.files.storage import storages
4+
from django.core.management.base import BaseCommand, CommandParser
5+
6+
from scoap3.articles.tasks import index_all_articles
7+
8+
9+
class Command(BaseCommand):
10+
help = "Article indexing commands"
11+
12+
def add_arguments(self, parser: CommandParser) -> None:
13+
parser.add_argument(
14+
"--from-id",
15+
type=int,
16+
default=None,
17+
required=False,
18+
help="Article id to start the indexing FROM.",
19+
)
20+
21+
parser.add_argument(
22+
"--batch-size",
23+
type=int,
24+
default=1000,
25+
required=False,
26+
help="Batchsize to migrate per task.",
27+
)
28+
29+
def handle(self, *args, **options):
30+
index_all_articles(options['batch_size'])

0 commit comments

Comments
 (0)