reliable queueing for thumbnail generation

This commit is contained in:
byte[] 2020-05-27 19:29:23 -04:00
parent 260526f5ed
commit 3440a2d4a2
7 changed files with 17 additions and 64 deletions

View file

@ -22,7 +22,6 @@ defmodule Philomena.Application do
# Starts a worker by calling: Philomena.Worker.start_link(arg) # Starts a worker by calling: Philomena.Worker.start_link(arg)
# {Philomena.Worker, arg}, # {Philomena.Worker, arg},
Philomena.Servers.ImageProcessor,
Philomena.Servers.UserLinkUpdater, Philomena.Servers.UserLinkUpdater,
Philomena.Servers.PicartoChannelUpdater, Philomena.Servers.PicartoChannelUpdater,
Philomena.Servers.PiczelChannelUpdater, Philomena.Servers.PiczelChannelUpdater,

View file

@ -9,6 +9,7 @@ defmodule Philomena.Images do
alias Philomena.Repo alias Philomena.Repo
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.ThumbnailWorker
alias Philomena.DuplicateReports.DuplicateReport alias Philomena.DuplicateReports.DuplicateReport
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images.Hider alias Philomena.Images.Hider
@ -97,10 +98,7 @@ defmodule Philomena.Images do
|> Repo.isolated_transaction(:serializable) |> Repo.isolated_transaction(:serializable)
|> case do |> case do
{:ok, %{image: image}} = result -> {:ok, %{image: image}} = result ->
spawn(fn ->
repair_image(image) repair_image(image)
end)
reindex_image(image) reindex_image(image)
Tags.reindex_tags(image.added_tags) Tags.reindex_tags(image.added_tags)
UserStatistics.inc_stat(attribution[:user], :uploads) UserStatistics.inc_stat(attribution[:user], :uploads)
@ -181,9 +179,12 @@ defmodule Philomena.Images do
|> where(id: ^image.id) |> where(id: ^image.id)
|> Repo.update_all(set: [thumbnails_generated: false, processed: false]) |> Repo.update_all(set: [thumbnails_generated: false, processed: false])
Philomena.Images.Thumbnailer.generate_thumbnails(image.id) Exq.enqueue(Exq, queue(image.image_mime_type), ThumbnailWorker, [image.id])
end end
defp queue("video/webm"), do: "videos"
defp queue(_mime_type), do: "images"
def update_file(%Image{} = image, attrs) do def update_file(%Image{} = image, attrs) do
image = image =
image image

View file

@ -1,45 +0,0 @@
defmodule Philomena.Servers.ImageProcessor do
use GenServer
def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default)
end
def cast(image_id) do
pid = Process.whereis(:processor)
GenServer.cast(pid, {:enqueue, image_id})
end
def call do
pid = Process.whereis(:processor)
GenServer.call(pid, :wait, :infinity)
end
@impl true
def init([]) do
Process.register(self(), :processor)
{:ok, []}
end
@impl true
def handle_cast({:enqueue, image_id}, []) do
# Ensure that tempfiles get cleaned up by reaping
# the process after it is done
Task.async(fn -> process(image_id) end)
|> Task.await(:infinity)
{:noreply, []}
end
defp process(image_id) do
Philomena.Images.Thumbnailer.generate_thumbnails(image_id)
rescue
_ ->
nil
end
@impl true
def handle_call(:wait, _from, []) do
{:reply, nil, []}
end
end

View file

@ -0,0 +1,7 @@
defmodule Philomena.ThumbnailWorker do
alias Philomena.Images.Thumbnailer
def perform(image_id) do
Thumbnailer.generate_thumbnails(image_id)
end
end

View file

@ -12,10 +12,7 @@ defmodule PhilomenaWeb.Image.FileController do
def update(conn, %{"image" => image_params}) do def update(conn, %{"image" => image_params}) do
case Images.update_file(conn.assigns.image, image_params) do case Images.update_file(conn.assigns.image, image_params) do
{:ok, %{image: image}} -> {:ok, %{image: image}} ->
spawn(fn ->
Images.repair_image(image) Images.repair_image(image)
end)
Images.reindex_image(image) Images.reindex_image(image)
conn conn

View file

@ -8,12 +8,10 @@ defmodule PhilomenaWeb.Image.RepairController do
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
def create(conn, _params) do def create(conn, _params) do
spawn(fn ->
Images.repair_image(conn.assigns.image) Images.repair_image(conn.assigns.image)
end)
conn conn
|> put_flash(:info, "Repair job started.") |> put_flash(:info, "Repair job enqueued.")
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image))
end end
end end

View file

@ -11,7 +11,6 @@
# and so on) as they will fail if something goes wrong. # and so on) as they will fail if something goes wrong.
alias Philomena.{Repo, Forums.Forum, Users.User} alias Philomena.{Repo, Forums.Forum, Users.User}
alias Philomena.Servers.ImageProcessor
alias Philomena.Comments alias Philomena.Comments
alias Philomena.Images alias Philomena.Images
alias Philomena.Topics alias Philomena.Topics
@ -139,7 +138,4 @@ for resource <- resources["forum_posts"] do
end end
end end
# Wait for processor to finish
ImageProcessor.call()
IO.puts "---- Done." IO.puts "---- Done."