Put rich media processing in a Task
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
FloatingGhost 2022-12-30 20:11:53 +00:00
parent 5d4c291d52
commit bf7ff6a337
5 changed files with 21 additions and 6 deletions

View File

@ -28,7 +28,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
# to fetch the preview. However it should be fine considering
# pagination is restricted to 40 activities at a time
defp fetch_rich_media_for_activities(activities) do
Enum.each(activities, fn activity ->
Enum.map(activities, fn activity ->
spawn(fn ->
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
end)

View File

@ -15,7 +15,7 @@ defmodule Pleroma.Web.RichMedia.Parser do
if Pleroma.Config.get(:env) == :test do
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
def parse(url), do: parse_url(url)
def parse(url), do: parse_with_timeout(url)
else
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
def parse(url) do
@ -27,7 +27,7 @@ defmodule Pleroma.Web.RichMedia.Parser do
defp get_cached_or_parse(url) do
case @cachex.fetch(:rich_media_cache, url, fn ->
case parse_url(url) do
case parse_with_timeout(url) do
{:ok, _} = res ->
{:commit, res}
@ -141,6 +141,21 @@ defmodule Pleroma.Web.RichMedia.Parser do
end
end
def parse_with_timeout(url) do
try do
task =
Task.Supervisor.async_nolink(Pleroma.TaskSupervisor, fn ->
parse_url(url)
end)
Task.await(task, 5000)
catch
:exit, {:timeout, _} ->
Logger.warn("Timeout while fetching rich media for #{url}")
{:error, :timeout}
end
end
defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do

View File

@ -326,7 +326,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
test "fake statuses' preview card is not cached", %{conn: conn} do
clear_config([:rich_media, :enabled], true)
Tesla.Mock.mock(fn
Tesla.Mock.mock_global(fn
%{
method: :get,
url: "https://example.com/twitter-card"

View File

@ -12,7 +12,7 @@ defmodule Pleroma.Web.RichMedia.HelpersTest do
import Tesla.Mock
setup do
mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
:ok
end

View File

@ -8,7 +8,7 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
alias Pleroma.Web.RichMedia.Parser
setup do
Tesla.Mock.mock(fn
Tesla.Mock.mock_global(fn
%{
method: :get,
url: "http://example.com/ogp"