Update preview image generation to only query ids

Previously we were querying the full book objects just to get a list of
id's, which is much slower and also takes a lot more memory, which can
cause the process to be killed on memory-limited machines with a large
number of books.

Instead, since we're just dispatching jobs here, we can just ask for the
id's, which is faster and much more practical memory-wise.

The map is a little annoying, I didn't see a way to directly get just a
list of the value of one field, so we have to get a list of
dictionairies with one key and then pull that key out. Whatevs.
This commit is contained in:
Joel Bradshaw 2022-06-05 20:03:36 +00:00 committed by Joel Bradshaw
parent 7905be7de2
commit 482005f304

View file

@ -56,12 +56,12 @@ class Command(BaseCommand):
self.stdout.write(" OK 🖼")
# Books
books = models.Book.objects.select_subclasses().filter()
book_ids = [values["id"] for values in models.Book.objects.select_subclasses().filter().values('id')]
self.stdout.write(
" → Book preview images ({}): ".format(len(books)), ending=""
" → Book preview images ({}): ".format(len(book_ids)), ending=""
)
for book in books:
preview_images.generate_edition_preview_image_task.delay(book.id)
for book_id in book_ids:
preview_images.generate_edition_preview_image_task.delay(book_id)
self.stdout.write(".", ending="")
self.stdout.write(" OK 🖼")