Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import time | ||
|
||
if __name__ == '__main__': | ||
naptime = 5 | ||
if len(sys.argv) > 1: | ||
naptime = float(sys.argv[1]) | ||
time.sleep(naptime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-module(ns_port_server). | ||
-behavior(gen_server). | ||
|
||
-export([start_link/3]). | ||
|
||
%% gen_server callbacks | ||
-export([init/1, | ||
handle_call/3, | ||
handle_cast/2, | ||
handle_info/2, | ||
code_change/3, | ||
terminate/2]). | ||
|
||
%% Server state | ||
-record(state, {port, name}). | ||
|
||
start_link(Name, Cmd, Args) -> | ||
gen_server:start_link({local, Name}, ?MODULE, {Name, Cmd, Args}, []). | ||
|
||
init({Name, Cmd, Args}) -> | ||
error_logger:info_msg("Starting ~p with ~p~n", [Cmd, Args]), | ||
process_flag(trap_exit, true), | ||
Port = open_port({spawn_executable, Cmd}, [{args, Args}]), | ||
{ok, #state{port = Port, name = Name}}. | ||
|
||
handle_info({'EXIT', _Port, Reason}, State) -> | ||
error_logger:info_msg("Port subprocess (~p) exited: ~p~n", | ||
[State#state.name, Reason]), | ||
{stop, {port_terminated, Reason}, State}. | ||
|
||
handle_call(Something, _From, State) -> | ||
error_logger:info_msg("Unexpected call: ~p~n", [Something]), | ||
{reply, "What?", State}. | ||
|
||
handle_cast(Something, State) -> | ||
error_logger:info_msg("Unexpected cast: ~p~n", [Something]), | ||
{noreply, State}. | ||
|
||
terminate({port_terminated, _Reason}, _State) -> | ||
ok; | ||
terminate(Reason, State) -> | ||
error_logger:info_msg("Terminating ~p: ~p~n", [State#state.name, Reason]), | ||
port_close(State#state.port). | ||
|
||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-module(ns_port_sup). | ||
-behavior(supervisor). | ||
|
||
-export([start_link/0]). | ||
|
||
-export([init/1, launch_port/2, launch_port/3, terminate_port/1]). | ||
|
||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
init([]) -> | ||
{ok, {{one_for_one, | ||
get_env_default(max_r, 3), | ||
get_env_default(max_t, 10)}, | ||
[]}}. | ||
|
||
get_env_default(Var, Def) -> | ||
case application:get_env(Var) of | ||
{ok, Value} -> Value; | ||
undefined -> Def | ||
end. | ||
|
||
launch_port(Name, Cmd) -> | ||
launch_port(Name, Cmd, []). | ||
|
||
launch_port(Name, Cmd, Args) when is_atom(Name); is_list(Cmd); is_list(Args) -> | ||
error_logger:info_msg("Supervising ~p~n", [Cmd]), | ||
supervisor:start_child(?MODULE, {Name, | ||
{ns_port_server, start_link, [Name, Cmd, Args]}, | ||
permanent, 10, worker, [ns_port_server]}). | ||
|
||
terminate_port(Name) -> | ||
error_logger:info_msg("Unsupervising ~p~n", [Name]), | ||
ok = supervisor:terminate_child(?MODULE, Name), | ||
ok = supervisor:delete_child(?MODULE, Name). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters