Skip to content

Commit

Permalink
Merge pull request #19 from appunite/fix-use-monitors-to-track-execution
Browse files Browse the repository at this point in the history
Use monitors to track execution status
  • Loading branch information
hauleth committed Oct 11, 2018
2 parents 0153a97 + f575686 commit 740adab
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/imager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule Imager do
{:ok, {:unknown, mime, stream}}

_ ->
Logger.error("Processing image failed")
Instrumenter.Processing.failed(store)

:failed
Expand Down
29 changes: 9 additions & 20 deletions lib/imager/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ defmodule Imager.Runner do
{__MODULE__, pid: self(), cmd: cmd, args: args}
)

ref = Process.monitor(pid)

stream =
Stream.resource(
fn -> pid end,
fn pid ->
receive do
{:out, ^pid, data} ->
{[data], pid}

{:exit, ^pid, status} = msg ->
send(self(), msg)

{:halt, status}
{:out, ^pid, data} -> {[data], pid}
{:DOWN, ^ref, :process, ^pid, status} -> {:halt, status}
end
end,
fn _ -> nil end
Expand All @@ -35,9 +32,11 @@ defmodule Imager.Runner do
def start_link(args), do: GenServer.start_link(__MODULE__, args)

def wait(pid) do
ref = Process.monitor(pid)

receive do
{:exit, ^pid, :success} -> :ok
{:exit, ^pid, _} -> :error
{:DOWN, ^ref, :process, ^pid, :normal} -> :ok
{:DOWN, ^ref, :process, ^pid, _} -> :error
end
end

Expand Down Expand Up @@ -90,16 +89,6 @@ defmodule Imager.Runner do
{:DOWN, ospid, :process, pid, result},
%__MODULE__{pid: pid, ospid: ospid} = state
) do
handle_result(result, state)

{:stop, :normal, state}
end

defp handle_result(:normal, %__MODULE__{output: output}),
do: send(output, {:exit, self(), :success})

defp handle_result({:exit_status, status}, %__MODULE__{output: output}) do
Logger.warn(inspect(:exec.status(status)))
send(output, {:exit, self(), :failure})
{:stop, result, state}
end
end
1 change: 1 addition & 0 deletions lib/imager/tool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ defmodule Imager.Tool do
defp string({"gravity", orientation}), do: {:gravity, orientation}
defp string({"strip", _}), do: {:strip, true}
defp string({"thumbnail", size}), do: {:thumbnail, size}
defp string({"format", format}), do: {:format, format}
defp string(option), do: raise(UnknownOption, option)
end
10 changes: 0 additions & 10 deletions test/imager/runner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ defmodule Imager.RunnerTest do
assert_receive {:out, ^pid, "foo"}
end

test "returns success on exit" do
assert {:ok, pid} = run("true")
assert_receive {:exit, ^pid, :success}
end

test "returns failure on exit" do
assert {:ok, pid} = run("false")
assert_receive {:exit, ^pid, :failure}
end

defp run(command, args \\ []),
do: start_supervised({Subject, pid: self(), cmd: command, args: args})
end

0 comments on commit 740adab

Please sign in to comment.