summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Every <devstopfix@gmail.com>2019-07-03 21:42:44 +0100
committerJames Every <devstopfix@gmail.com>2019-07-03 22:06:00 +0100
commit724f8a06cde05ddb04b3a410d0326c0372148588 (patch)
tree4fd32802c727892b4c217229b89290c184abb024
parentPrototype using erlexec (diff)
chore: format
-rw-r--r--config/config.exs13
-rw-r--r--lib/gen_magic.ex4
-rw-r--r--lib/gen_magic/apprentice_server.ex22
-rw-r--r--lib/gen_magic/configuration.ex6
-rw-r--r--mix.exs2
5 files changed, 33 insertions, 14 deletions
diff --git a/config/config.exs b/config/config.exs
new file mode 100644
index 0000000..3bf1711
--- /dev/null
+++ b/config/config.exs
@@ -0,0 +1,13 @@
+# This file is responsible for configuring your application
+# and its dependencies with the aid of the Mix.Config module.
+use Mix.Config
+
+config :gen_magic,
+ worker_name: "apprentice",
+ worker_timeout: 5000,
+ recycle_threshold: 10,
+ database_patterns: [
+ "/usr/local/share/misc/magic.mgc",
+ "/usr/share/file/magic.mgc",
+ "/usr/share/misc/magic.mgc"
+ ]
diff --git a/lib/gen_magic.ex b/lib/gen_magic.ex
index 3711dfa..93b3545 100644
--- a/lib/gen_magic.ex
+++ b/lib/gen_magic.ex
@@ -14,14 +14,14 @@ defmodule GenMagic do
:ok = GenServer.stop(pid)
result
end
-
+
def perform_infinite(path) do
{:ok, pid} = __MODULE__.ApprenticeServer.start_link()
perform_infinite(path, pid)
end
defp perform_infinite(path, pid, count \\ 0) do
- IO.inspect [count, GenServer.call(pid, {:perform, path})]
+ IO.inspect([count, GenServer.call(pid, {:perform, path})])
perform_infinite(path, pid, count + 1)
end
end
diff --git a/lib/gen_magic/apprentice_server.ex b/lib/gen_magic/apprentice_server.ex
index 5aee590..815d14f 100644
--- a/lib/gen_magic/apprentice_server.ex
+++ b/lib/gen_magic/apprentice_server.ex
@@ -31,8 +31,10 @@ defmodule GenMagic.ApprenticeServer do
case {run(path, state), state.count + 1} do
{{:error, :worker_failure} = reply, _} ->
{:reply, reply, stop(state)}
+
{reply, ^max_count} ->
{:reply, reply, stop(state)}
+
{reply, count} ->
{:reply, reply, %{state | count: count}}
end
@@ -55,8 +57,9 @@ defmodule GenMagic.ApprenticeServer do
receive do
{:stdout, ^ospid, "ok\n"} -> {:ok, state}
{:stdout, ^ospid, "ok\r\n"} -> {:ok, state}
- after worker_timeout ->
- {:error, :worker_failure}
+ after
+ worker_timeout ->
+ {:error, :worker_failure}
end
end
@@ -72,15 +75,19 @@ defmodule GenMagic.ApprenticeServer do
receive do
{stream, ^ospid, message} ->
handle_response(stream, message)
- after worker_timeout ->
- {:error, :worker_failure}
+ after
+ worker_timeout ->
+ {:error, :worker_failure}
end
end
defp handle_response(:stdout, "ok; " <> message) do
- case message |> String.trim |> String.split("\t") do
- [mime_type, encoding, content] -> {:ok, [mime_type: mime_type, encoding: encoding, content: content]}
- _ -> {:error, :malformed_response}
+ case message |> String.trim() |> String.split("\t") do
+ [mime_type, encoding, content] ->
+ {:ok, [mime_type: mime_type, encoding: encoding, content: content]}
+
+ _ ->
+ {:error, :malformed_response}
end
end
@@ -96,5 +103,4 @@ defmodule GenMagic.ApprenticeServer do
# (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
# (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
# Last message: {:stderr, 12304, "\n"}
-
end
diff --git a/lib/gen_magic/configuration.ex b/lib/gen_magic/configuration.ex
index b815aad..15334e2 100644
--- a/lib/gen_magic/configuration.ex
+++ b/lib/gen_magic/configuration.ex
@@ -3,12 +3,12 @@ defmodule GenMagic.Configuration do
Convenience module which returns information from configuration.
"""
- @otp_app Mix.Project.config[:app]
+ @otp_app Mix.Project.config()[:app]
def get_worker_command do
database_paths = get_database_paths()
worker_path = Path.join(:code.priv_dir(@otp_app), get_worker_name())
- worker_arguments = Enum.map(database_paths, & "--file " <> &1)
+ worker_arguments = Enum.map(database_paths, &("--file " <> &1))
Enum.join([worker_path | worker_arguments], " ")
end
@@ -19,7 +19,7 @@ defmodule GenMagic.Configuration do
def get_worker_timeout do
get_env(:worker_timeout)
end
-
+
def get_recycle_threshold do
get_env(:recycle_threshold)
end
diff --git a/mix.exs b/mix.exs
index 4ad7c93..bf27818 100644
--- a/mix.exs
+++ b/mix.exs
@@ -7,7 +7,7 @@ defmodule GenMagic.MixProject do
version: "0.1.0",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
- compilers: [:elixir_make] ++ Mix.compilers,
+ compilers: [:elixir_make] ++ Mix.compilers(),
deps: deps()
]
end