Skip to content

Instantly share code, notes, and snippets.

@sescobb27
Created December 6, 2019 14:51
Show Gist options
  • Save sescobb27/acebc07667161dc68ed76eb3f09c144f to your computer and use it in GitHub Desktop.
Save sescobb27/acebc07667161dc68ed76eb3f09c144f to your computer and use it in GitHub Desktop.
defmodule PortRunner do
use GenServer, restart: :temporary
defmodule State do
defstruct [:command, :exit_status, :port, arguments: []]
end
@spec start_link(State.t() | map() | keyword()) :: GenServer.on_start()
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
def init(args) do
state =
State
|> struct!(args)
{:ok, state, {:continue, :run_command}}
end
def handle_continue(
:run_command,
%{
command: command,
arguments: arguments
} = state
) do
port =
Port.open({:spawn_executable, command}, [
:binary,
:use_stdio,
:exit_status,
:stderr_to_stdout,
{:line, 255},
args: arguments
])
{:noreply, %{state | port: port}}
end
def handle_info({port, {:data, data}}, %{port: port} = state) do
IO.inspect({:data, data},
label: "HANDLE_INFO_DATA >>>>>>>>>>>>>>",
limit: :infinity
)
{:noreply, state}
end
def handle_info({port, {:exit_status, code}}, %{port: port} = state) do
IO.inspect({:exit_status, code},
label: "HANDLE_INFO_EXIT >>>>>>>>>>>>>>",
limit: :infinity
)
{:noreply, %{state | exit_status: code}}
end
def handle_info(msg, state) do
IO.inspect(msg, label: "HANDLE_INFO_ANY >>>>>>>>>>>>>>", limit: :infinity)
{:noreply, state}
end
end
test_base =
Path.join([
File.cwd!(),
"apps",
"manager",
"test",
"fixtures",
"videos",
"baseline.mp4"
])
test_diff =
Path.join([
File.cwd!(),
"apps",
"manager",
"test",
"fixtures",
"videos",
"different_FAIL.mp4"
])
PortRunner.start_link(
command: System.find_executable("ffmpeg"),
arguments: [
"-i",
test_base,
"-i",
test_diff,
"-filter_complex",
"blend=all_mode=difference,hue=s=0,blackdetect=pic_th=0.85",
"-f",
"NULL",
"-"
]
)
test_same =
Path.join([
File.cwd!(),
"apps",
"manager",
"test",
"fixtures",
"videos",
"same_PASS.mp4"
])
PortRunner.start_link(
command: System.find_executable("ffmpeg"),
arguments: [
"-i",
test_base,
"-i",
test_same,
"-filter_complex",
"blend=all_mode=difference,hue=s=0,blackdetect=pic_th=0.85",
"-f",
"NULL",
"-"
]
)
PortRunner.start_link(
command: System.find_executable("ruby"),
arguments: ["-e", "puts 'hello'; $stdout.flush; puts 'world'; $stdout.flush; puts 'break\nline'"]
)
PortRunner.start_link(
command: System.find_executable("ruby"),
arguments: ["-e", "puts 'hello'; $stdout.flush; sleep(1); puts 'world'; $stdout.flush; puts 'break\nline'"]
)
PortRunner.start_link(
command: System.find_executable("ruby"),
arguments: ["-e", "puts 'hello'; puts 'world'; puts 'break\nline'"]
)
PortRunner.start_link(
command: System.find_executable("ruby"),
arguments: ["-e", "puts 'h'*600; puts 'world'; puts 'break\nline'"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment