summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Kocoloski <adam@cloudant.com>2010-05-01 08:37:53 -0400
committerAdam Kocoloski <adam@cloudant.com>2010-05-01 08:37:53 -0400
commit6e6813d355aa7371f63e46472faaa2b326ddf394 (patch)
treea2fa6d748e375bf9e5ae9e6607233c56a2ac42ec
parent09a9e6e6e88974b5bcc91a35275b71a104846674 (diff)
rexi API module, and change msg sent if rexi worker dies
-rw-r--r--src/rexi.erl38
-rw-r--r--src/rexi_server.erl8
2 files changed, 43 insertions, 3 deletions
diff --git a/src/rexi.erl b/src/rexi.erl
new file mode 100644
index 00000000..745afedb
--- /dev/null
+++ b/src/rexi.erl
@@ -0,0 +1,38 @@
+-module(rexi).
+-export([start/0, stop/0, restart/0]).
+-export([cast/2, cast/3]).
+-export([reply/1]).
+
+-define(SERVER, rexi_server).
+
+start() ->
+ application:start(rexi).
+
+stop() ->
+ application:stop(rexi).
+
+restart() ->
+ stop(), start().
+
+-spec cast(node(), mfa()) -> {ok, reference()}.
+cast(Node, MFA) ->
+ cast(Node, self(), MFA).
+
+%% @doc Executes apply(M, F, A) on Node.
+%% You might want to use this instead of rpc:cast/4 for two reasons. First,
+%% the Caller pid and the returned reference are inserted into the remote
+%% process' dictionary as 'rexi_from', so it has a way to communicate with you.
+%% Second, the remote process is monitored. If it dies, Caller will receive a
+%% message of the form `{rexi_EXIT, Ref, Reason}' where Ref is the returned
+%% reference and Reason is the exit reason.
+-spec cast(node(), pid(), mfa()) -> {ok, reference()}.
+cast(Node, Caller, MFA) ->
+ Ref = make_ref(),
+ ok = gen_server:cast({?SERVER, Node}, {doit, {Caller,Ref}, MFA}),
+ {ok, Ref}.
+
+%% @doc convenience function to reply to the original rexi Caller.
+-spec reply(any()) -> any().
+reply(Reply) ->
+ {Caller, Ref} = get(rexi_from),
+ erlang:send(Caller, {Ref,Reply}).
diff --git a/src/rexi_server.erl b/src/rexi_server.erl
index 65110535..e06fd86d 100644
--- a/src/rexi_server.erl
+++ b/src/rexi_server.erl
@@ -30,7 +30,7 @@ handle_info({'DOWN', Ref, process, _, normal}, #st{workers=Workers} = St) ->
handle_info({'DOWN', Ref, process, Pid, Reason}, #st{workers=Workers} = St) ->
case find_worker(Ref, Workers) of
{Pid, Ref, From} ->
- notify_caller(From, Pid, Reason);
+ notify_caller(From, Reason);
false -> ok end,
{noreply, St#st{workers = remove_worker(Ref, Workers)}};
@@ -44,6 +44,8 @@ terminate(_Reason, St) ->
code_change(_OldVsn, St, _Extra) ->
{ok, St}.
+%% @doc initializes a process started by rexi_server.
+-spec init_p({pid(),reference()}, mfa()) -> any().
init_p(From, {M,F,A}) ->
put(rexi_from, From),
try apply(M, F, A) catch _:Reason -> exit(Reason) end.
@@ -59,5 +61,5 @@ remove_worker(Ref, List) ->
find_worker(Ref, List) ->
lists:keyfind(Ref, 2, List).
-notify_caller({Caller, CallerRef}, Pid, Reason) ->
- Caller ! {worker_died, CallerRef, Pid, Reason}.
+notify_caller({Caller, Ref}, Reason) ->
+ erlang:send(Caller, {rexi_EXIT, Ref, Reason}).