Add update_genres task

This commit is contained in:
2022-04-09 19:52:28 +03:00
parent 192f9a2ede
commit 078ca87b4f
2 changed files with 50 additions and 3 deletions

View File

@@ -143,11 +143,52 @@ async def update_sequences(ctx) -> bool:
return True
async def update_genres(ctx) -> bool:
loop = asyncio.get_event_loop()
meili = get_meilisearch_client()
index = meili.index("genres")
postgres = await get_postgres_connection()
async with postgres.transaction():
cursor = await postgres.cursor(
"SELECT id, description, meta, "
" array( "
" SELECT DISTINCT lang FROM book_genres "
" LEFT JOIN books ON book = books.id "
" WHERE genres.id = book_genres.genre "
" AND books.is_deleted = 'f' "
" ) as langs, "
" ( "
" SELECT count(*) FROM book_genres "
" LEFT JOIN books ON book = books.id "
" WHERE genres.id = book_genres.genre "
" AND books.is_deleted = 'f' "
" ) as books_count "
"FROM genres;"
)
while rows := await cursor.fetch(1024):
await loop.run_in_executor(
thread_pool, index.add_documents, [dict(row) for row in rows]
)
index.update_searchable_attributes(["description"])
index.update_filterable_attributes(["langs"])
index.update_ranking_rules([*DEFAULT_RANKING_RULES, "books_count:desc"])
await postgres.close()
return True
async def update(ctx: dict, *args, **kwargs) -> bool:
arq_pool: ArqRedis = ctx["arc_pool"]
await arq_pool.enqueue_job("update_books")
await arq_pool.enqueue_job("update_authors")
await arq_pool.enqueue_job("update_sequences")
await arq_pool.enqueue_job("update_genres")
return True

View File

@@ -1,6 +1,12 @@
from arq.cron import cron
from app.services import update, update_books, update_authors, update_sequences
from app.services import (
update,
update_books,
update_authors,
update_sequences,
update_genres,
)
from core.arq_pool import get_redis_settings, get_arq_pool
@@ -9,9 +15,9 @@ async def startup(ctx):
class WorkerSettings:
functions = [update, update_books, update_authors, update_sequences]
functions = [update, update_books, update_authors, update_sequences, update_genres]
on_startup = startup
redis_settings = get_redis_settings()
max_jobs = 3
max_jobs = 2
job_timeout = 15 * 60
cron_jobs = [cron(update, hour={4}, minute=0)]