Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md for important use case (use Supervisor). #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,56 @@ File.write!("input.txt", "lines\nread\nfrom\nfile\n")
result = Porcelain.exec("sort", ["input.txt"])
IO.inspect result.out #=> "file\nfrom\nlines\nread\n"
```
### Launching one-off programs under the Supervisor, so they are restarted when they crash

First the Application code:
```
defmodule Command do
use Application

def start(_type, _args) do
import Supervisor.Spec

children = [
supervisor(Command.Endpoint, []),
worker(Command.Minion, []),
]

opts = [strategy: :one_for_one, name: Command.Supervisor]
Supervisor.start_link(children, opts)
end

def config_change(changed, _new, removed) do
Command.Endpoint.config_change(changed, removed)
:ok
end
end
```

Second, the module that launches the one-off program:

```
defmodule Command.Minion do
use GenServer
require Logger
import Porcelain

def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts)
end

def init(state) do
send self, {:start_minion}
{:ok, state}
end

def handle_info({:start_minion}, state) do
process = Porcelain.spawn("command", [], [])
Porcelain.Process.await(process, :infinity)
{:noreply, state}
end
end
```


### Passing input and getting output
Expand Down