mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
reliable queueing for thumbnail generation
This commit is contained in:
parent
260526f5ed
commit
3440a2d4a2
7 changed files with 17 additions and 64 deletions
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
7
lib/philomena/workers/thumbnail_worker.ex
Normal file
7
lib/philomena/workers/thumbnail_worker.ex
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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."
|
||||
IO.puts "---- Done."
|
||||
|
|
Loading…
Reference in a new issue