Unified API: GenMagic.perform/2,3
This commit is contained in:
parent
9a70442ac2
commit
7f863a37f8
3 changed files with 46 additions and 11 deletions
18
README.md
18
README.md
|
@ -27,7 +27,7 @@ Depending on the use case, you may utilise a single (one-off) GenMagic process w
|
||||||
To use GenMagic directly, you can use `GenMagic.Helpers.perform_once/1`:
|
To use GenMagic directly, you can use `GenMagic.Helpers.perform_once/1`:
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
iex(1)> GenMagic.Helpers.perform_once "."
|
iex(1)> GenMagic.perform(".", once: true)
|
||||||
{:ok,
|
{:ok,
|
||||||
%GenMagic.Result{
|
%GenMagic.Result{
|
||||||
content: "directory",
|
content: "directory",
|
||||||
|
@ -40,7 +40,7 @@ To use the GenMagic server as a daemon, you can start it first, keep a reference
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
{:ok, pid} = GenMagic.Server.start_link([])
|
{:ok, pid} = GenMagic.Server.start_link([])
|
||||||
{:ok, result} = GenMagic.Server.perform(pid, path)
|
{:ok, result} = GenMagic.perform(path, server: pid)
|
||||||
```
|
```
|
||||||
|
|
||||||
See `GenMagic.Server.start_link/1` and `t:GenMagic.Server.option/0` for more information on startup parameters.
|
See `GenMagic.Server.start_link/1` and `t:GenMagic.Server.option/0` for more information on startup parameters.
|
||||||
|
@ -67,7 +67,7 @@ See `t:GenMagic.Server.option/0` for details.
|
||||||
For ad-hoc requests, you can use the helper method `GenMagic.Helpers.perform_once/2`:
|
For ad-hoc requests, you can use the helper method `GenMagic.Helpers.perform_once/2`:
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
iex(1)> GenMagic.Helpers.perform_once(Path.join(File.cwd!(), "Makefile"))
|
iex(1)> GenMagic.perform(Path.join(File.cwd!(), "Makefile"), once: true)
|
||||||
{:ok,
|
{:ok,
|
||||||
%GenMagic.Result{
|
%GenMagic.Result{
|
||||||
content: "makefile script, ASCII text",
|
content: "makefile script, ASCII text",
|
||||||
|
@ -90,8 +90,8 @@ iex(1)> {:ok, pid} = Supervisor.start_link([{GenMagic.Server, name: :gen_magic}]
|
||||||
Now we can ask it to inspect a file:
|
Now we can ask it to inspect a file:
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
iex(2)> GenMagic.Server.perform(:gen_magic, Path.expand("~/.bash_history"))
|
iex(2)> GenMagic.perform(Path.expand("~/.bash_history"), server: :gen_magic)
|
||||||
{:ok, [mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"]}
|
{:ok, %GenMagic.Result{mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"}}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that in this case we have opted to use a named process.
|
Note that in this case we have opted to use a named process.
|
||||||
|
@ -114,11 +114,11 @@ You can add a pool in your application supervisor by adding it as a child:
|
||||||
Supervisor.start_link(children, opts)
|
Supervisor.start_link(children, opts)
|
||||||
```
|
```
|
||||||
|
|
||||||
And then you can use it with `GenMagic.Pool.perform/2`:
|
And then you can use it with `GenMagic.perform/2` with `pool: YourApp.GenMagicPool` option:
|
||||||
|
|
||||||
```
|
```
|
||||||
iex(1)> GenMagic.Pool.perform(YourApp.GenMagicPool, Path.expand("~/.bash_history"))
|
iex(1)> GenMagic.perform(Path.expand("~/.bash_history"), pool: YourApp.GenMagicPool)
|
||||||
{:ok, [mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"]}
|
{:ok, %GenMagic.Result{mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"}}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Check Uploaded Files
|
### Check Uploaded Files
|
||||||
|
@ -127,7 +127,7 @@ If you use Phoenix, you can inspect the file from your controller:
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
def upload(conn, %{"upload" => %{path: path}}) do,
|
def upload(conn, %{"upload" => %{path: path}}) do,
|
||||||
{:ok, result} = GenMagic.Helpers.perform_once(:gen_magic, path)
|
{:ok, result} = GenMagic.perform(path, server: :gen_magic)
|
||||||
text(conn, "Received your file containing #{result.content}")
|
text(conn, "Received your file containing #{result.content}")
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
|
@ -4,4 +4,37 @@ defmodule GenMagic do
|
||||||
|
|
||||||
See `GenMagic.Server` or the README for usage.
|
See `GenMagic.Server` or the README for usage.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Perform on `path`.
|
||||||
|
|
||||||
|
An option of `server: ServerName`, `pool: PoolName` or `once: true` must be passed.
|
||||||
|
"""
|
||||||
|
@type option :: name
|
||||||
|
when name: {:pool, atom()} | {:server, GenMagic.Server.t()} | {:once, true}
|
||||||
|
|
||||||
|
@spec perform(GenMagic.Server.target(), [option()]) :: GenMagic.Server.result()
|
||||||
|
def perform(path, opts, timeout \\ 5000) do
|
||||||
|
mod = cond do
|
||||||
|
Keyword.has_key?(opts, :pool) -> {GenMagic.Pool, Keyword.get(opts, :pool)}
|
||||||
|
Keyword.has_key?(opts, :server) -> {GenMagic.Server, Keyword.get(opts, :server)}
|
||||||
|
Keyword.has_key?(opts, :once) -> {GenMagic.Helpers, nil}
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if mod do
|
||||||
|
do_perform(mod, path, timeout)
|
||||||
|
else
|
||||||
|
{:error, :no_method}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_perform({GenMagic.Helpers, _}, timeout) do
|
||||||
|
GenMagic.Helpers.perform_once(path, timeout)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_perform({mod, name}, path) do
|
||||||
|
mod.perform(name, path, tiemout)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,6 +55,9 @@ defmodule GenMagic.Server do
|
||||||
| {:recycle_threshold, non_neg_integer() | :infinity}
|
| {:recycle_threshold, non_neg_integer() | :infinity}
|
||||||
| {:database_patterns, nonempty_list(:default | Path.t())}
|
| {:database_patterns, nonempty_list(:default | Path.t())}
|
||||||
|
|
||||||
|
@type target :: Path.t() | {:bytes, binary()}
|
||||||
|
@type result :: {:ok, Result.t()} | {:error, term() | String.t()}
|
||||||
|
|
||||||
@typedoc """
|
@typedoc """
|
||||||
Current state of the Server:
|
Current state of the Server:
|
||||||
|
|
||||||
|
@ -81,8 +84,7 @@ defmodule GenMagic.Server do
|
||||||
|
|
||||||
@spec child_spec([option()]) :: Supervisor.child_spec()
|
@spec child_spec([option()]) :: Supervisor.child_spec()
|
||||||
@spec start_link([option()]) :: :gen_statem.start_ret()
|
@spec start_link([option()]) :: :gen_statem.start_ret()
|
||||||
@spec perform(t(), Path.t() | {:bytes, binary()}, timeout()) ::
|
@spec perform(t(), target(), timeout()) :: result()
|
||||||
{:ok, Result.t()} | {:error, term() | String.t()}
|
|
||||||
@spec status(t(), timeout()) :: {:ok, Status.t()} | {:error, term()}
|
@spec status(t(), timeout()) :: {:ok, Status.t()} | {:error, term()}
|
||||||
@spec stop(t(), term(), timeout()) :: :ok
|
@spec stop(t(), term(), timeout()) :: :ok
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue