Skip to content

Commit

Permalink
Pull out media server and handling into separate container
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Dec 25, 2024
1 parent d695bf1 commit e319892
Show file tree
Hide file tree
Showing 28 changed files with 1,743 additions and 47 deletions.
1 change: 1 addition & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ config :philomena,
tag_file_root: System.fetch_env!("TAG_FILE_ROOT"),
site_domains: System.fetch_env!("SITE_DOMAINS"),
tag_url_root: System.fetch_env!("TAG_URL_ROOT"),
broker_addr: System.fetch_env!("BROKER_ADDR"),
redis_host: System.get_env("REDIS_HOST", "localhost"),
proxy_host: System.get_env("PROXY_HOST"),
camo_host: System.get_env("CAMO_HOST"),
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
- IMAGE_URL_ROOT=/img
- BADGE_URL_ROOT=/badge-img
- TAG_URL_ROOT=/tag-img
- BROKER_ADDR=broker:1500
- OPENSEARCH_URL=http://opensearch:9200
- REDIS_HOST=valkey
- DATABASE_URL=ecto://postgres:postgres@postgres/philomena_dev
Expand All @@ -52,6 +53,7 @@ services:
- app_deps_data:/srv/philomena/deps
- app_native_data:/srv/philomena/priv/native
depends_on:
- broker
- postgres
- opensearch
- valkey
Expand Down Expand Up @@ -89,6 +91,18 @@ services:
- .:/srv/philomena
attach: false

broker:
build:
context: .
dockerfile: ./docker/broker/Dockerfile
attach: false
deploy:
resources:
limits:
cpus: 8
memory: 4gb
pids: 8192

web:
build:
context: .
Expand Down
21 changes: 5 additions & 16 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
FROM elixir:1.17.2-alpine

ADD https://api.github.com/repos/philomena-dev/FFmpeg/git/refs/heads/release/6.1 /tmp/ffmpeg_version.json
RUN (echo "https://github.com/philomena-dev/prebuilt-ffmpeg/raw/master"; cat /etc/apk/repositories) > /tmp/repositories \
&& cp /tmp/repositories /etc/apk/repositories \
&& apk update --allow-untrusted \
&& apk add inotify-tools build-base git ffmpeg ffmpeg-dev npm nodejs file-dev libjpeg-turbo-dev libpng-dev gifsicle optipng libjpeg-turbo-utils librsvg rsvg-convert imagemagick postgresql16-client wget rust cargo --allow-untrusted \
RUN apk add inotify-tools build-base git npm nodejs postgresql16-client wget rust cargo \
&& mix local.hex --force \
&& mix local.rebar --force

ADD https://api.github.com/repos/philomena-dev/cli_intensities/git/refs/heads/master /tmp/cli_intensities_version.json
RUN git clone --depth 1 https://github.com/philomena-dev/cli_intensities /tmp/cli_intensities \
&& cd /tmp/cli_intensities \
&& make -j$(nproc) install

ADD https://api.github.com/repos/philomena-dev/mediatools/git/refs/heads/master /tmp/mediatools_version.json
RUN git clone --depth 1 https://github.com/philomena-dev/mediatools /tmp/mediatools \
&& ln -s /usr/lib/librsvg-2.so.2 /usr/lib/librsvg-2.so \
&& cd /tmp/mediatools \
&& make -j$(nproc) install
COPY native/broker /tmp/broker
RUN cd /tmp/broker \
&& cargo build --release \
&& cp target/release/broker /usr/local/bin/broker

COPY docker/app/run-development /usr/local/bin/run-development
COPY docker/app/run-test /usr/local/bin/run-test
COPY docker/app/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert
COPY docker/app/purge-cache /usr/local/bin/purge-cache
ENV PATH=$PATH:/root/.cargo/bin
EXPOSE 5173
Expand Down
27 changes: 27 additions & 0 deletions docker/broker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM rust:1.83-alpine

ADD https://api.github.com/repos/philomena-dev/FFmpeg/git/refs/heads/release/6.1 /tmp/ffmpeg_version.json
ADD https://api.github.com/repos/philomena-dev/cli_intensities/git/refs/heads/master /tmp/cli_intensities_version.json
ADD https://api.github.com/repos/philomena-dev/mediatools/git/refs/heads/master /tmp/mediatools_version.json

RUN (echo "https://github.com/philomena-dev/prebuilt-ffmpeg/raw/master"; cat /etc/apk/repositories) > /tmp/repositories \
&& cp /tmp/repositories /etc/apk/repositories \
&& apk update --allow-untrusted \
&& apk add build-base git ffmpeg ffmpeg-dev file-dev libjpeg-turbo-dev libpng-dev \
gifsicle optipng libjpeg-turbo-utils librsvg rsvg-convert imagemagick --allow-untrusted \
&& git clone --depth 1 https://github.com/philomena-dev/cli_intensities /tmp/cli_intensities \
&& cd /tmp/cli_intensities \
&& make -j$(nproc) install \
&& git clone --depth 1 https://github.com/philomena-dev/mediatools /tmp/mediatools \
&& ln -s /usr/lib/librsvg-2.so.2 /usr/lib/librsvg-2.so \
&& cd /tmp/mediatools \
&& make -j$(nproc) install

COPY native/broker /tmp/broker
RUN cd /tmp/broker \
&& cargo build --release \
&& cp target/release/broker_server /usr/local/bin/broker_server

COPY docker/broker/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert
ENV RUST_LOG=trace
CMD ["/usr/local/bin/broker_server", "0.0.0.0:1500"]
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/philomena_media/analyzers/gif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Gif do

alias PhilomenaMedia.Analyzers.Analyzer
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker

@behaviour Analyzer

Expand All @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Gif do
end

defp stats(file) do
case System.cmd("mediastat", [file]) do
case Broker.cmd("mediastat", [file]) do
{output, 0} ->
[_size, frames, width, height, num, den] =
output
Expand Down
3 changes: 2 additions & 1 deletion lib/philomena_media/analyzers/jpeg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Jpeg do

alias PhilomenaMedia.Analyzers.Analyzer
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker

@behaviour Analyzer

Expand All @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Jpeg do
end

defp stats(file) do
case System.cmd("mediastat", [file]) do
case Broker.cmd("mediastat", [file]) do
{output, 0} ->
[_size, _frames, width, height, num, den] =
output
Expand Down
3 changes: 2 additions & 1 deletion lib/philomena_media/analyzers/png.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Png do

alias PhilomenaMedia.Analyzers.Analyzer
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker

@behaviour Analyzer

Expand All @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Png do
end

defp stats(file) do
case System.cmd("mediastat", [file]) do
case Broker.cmd("mediastat", [file]) do
{output, 0} ->
[_size, frames, width, height, num, den] =
output
Expand Down
3 changes: 2 additions & 1 deletion lib/philomena_media/analyzers/svg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Svg do

alias PhilomenaMedia.Analyzers.Analyzer
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker

@behaviour Analyzer

Expand All @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Svg do
end

defp stats(file) do
case System.cmd("svgstat", [file]) do
case Broker.cmd("svgstat", [file]) do
{output, 0} ->
[_size, _frames, width, height, _num, _den] =
output
Expand Down
3 changes: 2 additions & 1 deletion lib/philomena_media/analyzers/webm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Webm do

alias PhilomenaMedia.Analyzers.Analyzer
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker

@behaviour Analyzer

Expand All @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Webm do
end

defp stats(file) do
case System.cmd("mediastat", [file]) do
case Broker.cmd("mediastat", [file]) do
{output, 0} ->
[_size, frames, width, height, num, den] =
output
Expand Down
13 changes: 13 additions & 0 deletions lib/philomena_media/broker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule PhilomenaMedia.Broker do
@doc """
Out-of-process replacement for `System.cmd/2` that calls the requested
command elsewhere, translating file accesses, and returns the result.
"""
def cmd(command, args) do
System.cmd("broker", [broker_addr(), "execute-command", "--", command] ++ args)
end

defp broker_addr do
Application.get_env(:philomena, :broker_addr)
end
end
4 changes: 3 additions & 1 deletion lib/philomena_media/gif_preview.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule PhilomenaMedia.GifPreview do
GIF preview generation for video files.
"""

alias PhilomenaMedia.Broker

@type duration :: float()
@type dimensions :: {pos_integer(), pos_integer()}

Expand Down Expand Up @@ -46,7 +48,7 @@ defmodule PhilomenaMedia.GifPreview do
end)

{_output, 0} =
System.cmd(
Broker.cmd(
"ffmpeg",
commands(video, gif, clamp(duration), dimensions, num_images, target_framerate)
)
Expand Down
4 changes: 3 additions & 1 deletion lib/philomena_media/intensities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule PhilomenaMedia.Intensities do
of image dimensions, with poor precision and a poor-to-fair accuracy.
"""

alias PhilomenaMedia.Broker

@type t :: %__MODULE__{
nw: float(),
ne: float(),
Expand Down Expand Up @@ -50,7 +52,7 @@ defmodule PhilomenaMedia.Intensities do
"""
@spec file(Path.t()) :: {:ok, t()} | :error
def file(input) do
System.cmd("image-intensities", [input])
Broker.cmd("image-intensities", [input])
|> case do
{output, 0} ->
[nw, ne, sw, se] =
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena_media/mime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule PhilomenaMedia.Mime do
"""
@spec file(Path.t()) :: {:ok, t()} | {:unsupported_mime, t()} | :error
def file(path) do
System.cmd("file", ["-b", "--mime-type", path])
PhilomenaMedia.Broker.cmd("file", ["-b", "--mime-type", path])
|> case do
{output, 0} ->
true_mime(String.trim(output))
Expand Down
13 changes: 7 additions & 6 deletions lib/philomena_media/processors/gif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Gif do

alias PhilomenaMedia.Intensities
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker
alias PhilomenaMedia.Processors.Processor
alias PhilomenaMedia.Processors

Expand Down Expand Up @@ -46,15 +47,15 @@ defmodule PhilomenaMedia.Processors.Gif do
defp optimize(file) do
optimized = Briefly.create!(extname: ".gif")

{_output, 0} = System.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized])
{_output, 0} = Broker.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized])

optimized
end

defp preview(duration, file) do
preview = Briefly.create!(extname: ".png")

{_output, 0} = System.cmd("mediathumb", [file, to_string(duration / 2), preview])
{_output, 0} = Broker.cmd("mediathumb", [file, to_string(duration / 2), preview])

preview
end
Expand All @@ -63,7 +64,7 @@ defmodule PhilomenaMedia.Processors.Gif do
palette = Briefly.create!(extname: ".png")

{_output, 0} =
System.cmd("ffmpeg", [
Broker.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
Expand All @@ -88,7 +89,7 @@ defmodule PhilomenaMedia.Processors.Gif do
filter_graph = "[0:v]#{scale_filter}[x];[x][1:v]#{palette_filter}"

{_output, 0} =
System.cmd("ffmpeg", [
Broker.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
Expand All @@ -109,7 +110,7 @@ defmodule PhilomenaMedia.Processors.Gif do
mp4 = Briefly.create!(extname: ".mp4")

{_output, 0} =
System.cmd("ffmpeg", [
Broker.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
Expand All @@ -127,7 +128,7 @@ defmodule PhilomenaMedia.Processors.Gif do
])

{_output, 0} =
System.cmd("ffmpeg", [
Broker.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
Expand Down
13 changes: 7 additions & 6 deletions lib/philomena_media/processors/jpeg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do

alias PhilomenaMedia.Intensities
alias PhilomenaMedia.Analyzers.Result
alias PhilomenaMedia.Broker
alias PhilomenaMedia.Processors.Processor
alias PhilomenaMedia.Processors

Expand Down Expand Up @@ -42,7 +43,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do

defp requires_lossy_transformation?(file) do
with {output, 0} <-
System.cmd("identify", ["-format", "%[orientation]\t%[profile:icc]", file]),
Broker.cmd("identify", ["-format", "%[orientation]\t%[profile:icc]", file]),
[orientation, profile] <- String.split(output, "\t") do
orientation not in ["Undefined", "TopLeft"] or profile != ""
else
Expand All @@ -60,7 +61,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do
true ->
# Transcode: strip EXIF, embedded profile and reorient image
{_output, 0} =
System.cmd("convert", [
Broker.cmd("convert", [
file,
"-profile",
srgb_profile(),
Expand All @@ -71,7 +72,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do

_ ->
# Transmux only: Strip EXIF without touching orientation
validate_return(System.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file]))
validate_return(Broker.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file]))
end

stripped
Expand All @@ -80,7 +81,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do
defp optimize(file) do
optimized = Briefly.create!(extname: ".jpg")

validate_return(System.cmd("jpegtran", ["-optimize", "-outfile", optimized, file]))
validate_return(Broker.cmd("jpegtran", ["-optimize", "-outfile", optimized, file]))

optimized
end
Expand All @@ -90,7 +91,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"

{_output, 0} =
System.cmd("ffmpeg", [
Broker.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
Expand All @@ -103,7 +104,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do
scaled
])

{_output, 0} = System.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled])
{_output, 0} = Broker.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled])

[{:copy, scaled, "#{thumb_name}.jpg"}]
end
Expand Down
Loading

0 comments on commit e319892

Please sign in to comment.