diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index 97a04cd0..fc74de6b 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -22,7 +22,6 @@ defmodule Philomena.Application do # Starts a worker by calling: Philomena.Worker.start_link(arg) # {Philomena.Worker, arg}, - Philomena.Servers.ImageProcessor, Philomena.Servers.UserLinkUpdater, Philomena.Servers.PicartoChannelUpdater, Philomena.Servers.PiczelChannelUpdater, diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index cc36b17d..5ce28385 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -9,6 +9,7 @@ defmodule Philomena.Images do alias Philomena.Repo alias Philomena.Elasticsearch + alias Philomena.ThumbnailWorker alias Philomena.DuplicateReports.DuplicateReport alias Philomena.Images.Image alias Philomena.Images.Hider @@ -97,10 +98,7 @@ defmodule Philomena.Images do |> Repo.isolated_transaction(:serializable) |> case do {:ok, %{image: image}} = result -> - spawn(fn -> - repair_image(image) - end) - + repair_image(image) reindex_image(image) Tags.reindex_tags(image.added_tags) UserStatistics.inc_stat(attribution[:user], :uploads) @@ -181,9 +179,12 @@ defmodule Philomena.Images do |> where(id: ^image.id) |> 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 + defp queue("video/webm"), do: "videos" + defp queue(_mime_type), do: "images" + def update_file(%Image{} = image, attrs) do image = image diff --git a/lib/philomena/servers/image_processor.ex b/lib/philomena/servers/image_processor.ex deleted file mode 100644 index 68e48557..00000000 --- a/lib/philomena/servers/image_processor.ex +++ /dev/null @@ -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 diff --git a/lib/philomena/workers/thumbnail_worker.ex b/lib/philomena/workers/thumbnail_worker.ex new file mode 100644 index 00000000..b3bd632e --- /dev/null +++ b/lib/philomena/workers/thumbnail_worker.ex @@ -0,0 +1,7 @@ +defmodule Philomena.ThumbnailWorker do + alias Philomena.Images.Thumbnailer + + def perform(image_id) do + Thumbnailer.generate_thumbnails(image_id) + end +end diff --git a/lib/philomena_web/controllers/image/file_controller.ex b/lib/philomena_web/controllers/image/file_controller.ex index 191dda36..32159871 100644 --- a/lib/philomena_web/controllers/image/file_controller.ex +++ b/lib/philomena_web/controllers/image/file_controller.ex @@ -12,10 +12,7 @@ defmodule PhilomenaWeb.Image.FileController do def update(conn, %{"image" => image_params}) do case Images.update_file(conn.assigns.image, image_params) do {:ok, %{image: image}} -> - spawn(fn -> - Images.repair_image(image) - end) - + Images.repair_image(image) Images.reindex_image(image) conn diff --git a/lib/philomena_web/controllers/image/repair_controller.ex b/lib/philomena_web/controllers/image/repair_controller.ex index 9073a765..2961d0ab 100644 --- a/lib/philomena_web/controllers/image/repair_controller.ex +++ b/lib/philomena_web/controllers/image/repair_controller.ex @@ -8,12 +8,10 @@ defmodule PhilomenaWeb.Image.RepairController do plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true def create(conn, _params) do - spawn(fn -> - Images.repair_image(conn.assigns.image) - end) + Images.repair_image(conn.assigns.image) conn - |> put_flash(:info, "Repair job started.") + |> put_flash(:info, "Repair job enqueued.") |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) end end diff --git a/priv/repo/seeds_development.exs b/priv/repo/seeds_development.exs index 9c1fbbc4..7d12bc48 100644 --- a/priv/repo/seeds_development.exs +++ b/priv/repo/seeds_development.exs @@ -11,7 +11,6 @@ # and so on) as they will fail if something goes wrong. alias Philomena.{Repo, Forums.Forum, Users.User} -alias Philomena.Servers.ImageProcessor alias Philomena.Comments alias Philomena.Images alias Philomena.Topics @@ -139,7 +138,4 @@ for resource <- resources["forum_posts"] do end end -# Wait for processor to finish -ImageProcessor.call() - -IO.puts "---- Done." \ No newline at end of file +IO.puts "---- Done."