|
|
@ -3,16 +3,31 @@ defmodule Asciinema.PngGenerator.A2png do
|
|
|
|
use GenServer
|
|
|
|
use GenServer
|
|
|
|
alias Asciinema.Asciicast
|
|
|
|
alias Asciinema.Asciicast
|
|
|
|
|
|
|
|
|
|
|
|
@result_timeout 30000
|
|
|
|
@pool_name :worker
|
|
|
|
@acquire_timeout 5000
|
|
|
|
@acquire_timeout 5000
|
|
|
|
|
|
|
|
@a2png_timeout 30000
|
|
|
|
|
|
|
|
@result_timeout 35000
|
|
|
|
|
|
|
|
|
|
|
|
def generate(%Asciicast{} = asciicast) do
|
|
|
|
def generate(%Asciicast{} = asciicast) do
|
|
|
|
{:ok, tmp_dir_path} = Briefly.create(directory: true)
|
|
|
|
{:ok, tmp_dir_path} = Briefly.create(directory: true)
|
|
|
|
|
|
|
|
|
|
|
|
:poolboy.transaction(
|
|
|
|
try do
|
|
|
|
:worker,
|
|
|
|
:poolboy.transaction(
|
|
|
|
&GenServer.call(&1, {:gen_png, asciicast, tmp_dir_path}, @result_timeout), @acquire_timeout
|
|
|
|
@pool_name,
|
|
|
|
)
|
|
|
|
(fn pid ->
|
|
|
|
|
|
|
|
try do
|
|
|
|
|
|
|
|
GenServer.call(pid, {:generate, asciicast, tmp_dir_path}, @result_timeout)
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
|
|
|
:exit, {:timeout, _} ->
|
|
|
|
|
|
|
|
{:error, :timeout}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end),
|
|
|
|
|
|
|
|
@acquire_timeout
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
|
|
|
:exit, {:timeout, _} ->
|
|
|
|
|
|
|
|
{:error, :busy}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# GenServer API
|
|
|
|
# GenServer API
|
|
|
@ -25,11 +40,18 @@ defmodule Asciinema.PngGenerator.A2png do
|
|
|
|
{:ok, nil}
|
|
|
|
{:ok, nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def handle_call({:gen_png, asciicast, tmp_dir_path}, _from, state) do
|
|
|
|
def handle_call({:generate, asciicast, tmp_dir_path}, _from, state) do
|
|
|
|
{:reply, do_gen(asciicast, tmp_dir_path), state}
|
|
|
|
{:reply, do_generate(asciicast, tmp_dir_path), state}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def poolboy_config do
|
|
|
|
|
|
|
|
[{:name, {:local, @pool_name}},
|
|
|
|
|
|
|
|
{:worker_module, __MODULE__},
|
|
|
|
|
|
|
|
{:size, pool_size()},
|
|
|
|
|
|
|
|
{:max_overflow, 0}]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp do_gen(asciicast, tmp_dir_path) do
|
|
|
|
defp do_generate(asciicast, tmp_dir_path) do
|
|
|
|
path = Asciicast.json_store_path(asciicast)
|
|
|
|
path = Asciicast.json_store_path(asciicast)
|
|
|
|
json_path = Path.join(tmp_dir_path, "tmp.json")
|
|
|
|
json_path = Path.join(tmp_dir_path, "tmp.json")
|
|
|
|
png_path = Path.join(tmp_dir_path, "tmp.png")
|
|
|
|
png_path = Path.join(tmp_dir_path, "tmp.png")
|
|
|
@ -37,7 +59,8 @@ defmodule Asciinema.PngGenerator.A2png do
|
|
|
|
|
|
|
|
|
|
|
|
with {:ok, file} <- file_store().open(path),
|
|
|
|
with {:ok, file} <- file_store().open(path),
|
|
|
|
{:ok, _} <- :file.copy(file, json_path),
|
|
|
|
{:ok, _} <- :file.copy(file, json_path),
|
|
|
|
%{status: 0} <- Porcelain.exec(bin_path(), [json_path, png_path, snapshot_at]) do
|
|
|
|
process <- Porcelain.spawn(bin_path(), [json_path, png_path, snapshot_at]),
|
|
|
|
|
|
|
|
{:ok, %{status: 0}} <- Porcelain.Process.await(process, @a2png_timeout) do
|
|
|
|
{:ok, png_path}
|
|
|
|
{:ok, png_path}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
otherwise ->
|
|
|
|
otherwise ->
|
|
|
@ -45,10 +68,14 @@ defmodule Asciinema.PngGenerator.A2png do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def bin_path do
|
|
|
|
defp bin_path do
|
|
|
|
Keyword.get(Application.get_env(:asciinema, __MODULE__), :bin_path)
|
|
|
|
Keyword.get(Application.get_env(:asciinema, __MODULE__), :bin_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp pool_size do
|
|
|
|
|
|
|
|
Keyword.get(Application.get_env(:asciinema, __MODULE__), :pool_size)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp file_store do
|
|
|
|
defp file_store do
|
|
|
|
Application.get_env(:asciinema, :file_store)
|
|
|
|
Application.get_env(:asciinema, :file_store)
|
|
|
|
end
|
|
|
|
end
|
|
|
|