-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathn2o_pi.erl
More file actions
144 lines (120 loc) · 4.15 KB
/
n2o_pi.erl
File metadata and controls
144 lines (120 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-module(n2o_pi).
-description('N2O Process Instance'). % gen_server replacement
-include_lib("n2o/include/n2o.hrl").
-include_lib("n2o/include/n2o_pi.hrl").
-behaviour(gen_server).
-export([start_link/1]).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-export([start/1,
stop/2,
send/2,
send/3,
cast/2,
cast/3,
pid/2,
restart/2]).
-define(CALL_TIMEOUT,
application:get_env(n2o, pi_call_timeout, 5000)).
start(#pi{table = Tab, name = Name, module = Module,
sup = Sup, timeout = Timeout, restart = Restart} =
Async) ->
ChildSpec = {{Tab, Name},
{?MODULE, start_link, [Async]},
Restart,
Timeout,
worker,
[Module]},
case supervisor:start_child(Sup, ChildSpec) of
{ok, Pid} -> {Pid, Async#pi.name};
{ok, Pid, _} -> {Pid, Async#pi.name};
{error, already_present} ->
case supervisor:restart_child(Sup, {Tab, Name}) of
{ok, Pid} -> {Pid, Async#pi.name};
{ok, Pid, _} -> {Pid, Async#pi.name};
{error, running} -> {pid(Tab, Name), Async#pi.name};
{error, _} = X -> X
end;
{error, Reason} -> {error, Reason}
end.
stop(Tab, Name) ->
case n2o_pi:pid(Tab, Name) of
Pid when is_pid(Pid) ->
try
#pi{sup = Sup} = Async = send(Pid, {get}),
[supervisor:F(Sup, {Tab, Name})
|| F <- [terminate_child, delete_child]],
n2o:cache(Tab, {Tab, Name}, undefined),
Async
catch
_X:_Y:_Z -> error
end;
Data -> {error, {not_pid, Data}}
end.
send(Pid, Message) when is_pid(Pid) ->
try gen_server:call(Pid, Message, ?CALL_TIMEOUT) catch
exit:{normal, _}:_Z -> {exit, normal};
X:Y:Z -> {error, {X, Y, Z}}
end.
send(Tab, Name, Message) ->
try gen_server:call(n2o_pi:pid(Tab, Name), Message, ?CALL_TIMEOUT) catch
exit:{normal, _}:_Z -> {exit, normal};
X:Y:Z -> {error, {X, Y, Z}}
end.
cast(Pid, Message) when is_pid(Pid) ->
gen_server:cast(Pid, Message).
cast(Tab, Name, Message) ->
gen_server:cast(n2o_pi:pid(Tab, Name), Message).
pid(Tab, Name) -> n2o:cache(Tab, {Tab, Name}).
restart(Tab, Name) ->
case stop(Tab, Name) of
#pi{} = Async -> start(Async);
Error -> Error
end.
handle(Mod, Message, Async) ->
case Mod:proc(Message, Async) of
{ok, S} -> {ok, S};
{ok, S, T} -> {ok, S, T};
{stop, X, Y, S} -> {stop, X, Y, S};
{stop, X, S} -> {stop, X, S};
{stop, S} -> {stop, S};
{reply, X, S, T} -> {reply, X, S, T};
{reply, X, S} -> {reply, X, S};
{noreply, X, S} -> {noreply, X, S};
{noreply, S} -> {noreply, S};
{_, S} -> {noreply, S};
S -> {noreply, S}
end.
start_link(Parameters) ->
gen_server:start_link(?MODULE, Parameters, []).
code_change(_, State, _) -> {ok, State}.
handle_call({get}, _, Async) -> {reply, Async, Async};
handle_call(_, _, #pi{module = undefined}) ->
{noreply, []};
handle_call(Message, _, #pi{module = Mod} = Async) ->
handle(Mod, Message, Async).
handle_cast(_, #pi{module = undefined}) ->
{noreply, []};
handle_cast(Message, #pi{module = Mod} = Async) ->
handle(Mod, Message, Async).
handle_info(timeout, #pi{module = undefined}) ->
{noreply, []};
handle_info(timeout, #pi{module = Mod} = Async) ->
handle(Mod, timeout, Async);
handle_info(_, #pi{module = undefined}) ->
{noreply, []};
handle_info(Message, #pi{module = Mod} = Async) ->
handle(Mod, Message, Async);
handle_info(_, _) -> {noreply, []}.
init(#pi{module = Mod, table = Tab, name = Name} =
Handler) ->
n2o:cache(Tab, {Tab, Name}, self(), infinity),
Mod:proc(init, Handler).
terminate(Reason, #pi{name = Name, table = Tab, module = Mod} = Pi) ->
case erlang:function_exported(Mod, terminate, 2) of true -> Mod:terminate(Reason, Pi); false -> false end,
n2o:cache(Tab, {Tab, Name}, undefined),
ok.