public
Description: A PHP eval server for Erlang/OTP.
Homepage:
Clone URL: git://github.com/skeltoac/php_app.git
php_app / php.erl
100644 346 lines (309 sloc) 11.341 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
%%%-------------------------------------------------------------------
%%% File : php.erl
%%% Author : Andy Skelton <andy@automattic.com>
%%% Purpose : Provides API for evaluating PHP code.
%%% Created : 15 Jan 2009 by Andy Skelton <andy@automattic.com>
%%%
%%% Copyright (C) 2009 Automattic
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program; if not, write to the Free Software
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%-------------------------------------------------------------------
 
%% @author Andy Skelton <andy@automattic.com>
%% [http://andy.wordpress.com/]
%% @doc This module provides all of the API functions and a gen_server
%% that marshals requests for PHP code evaluation. It maintains
%% queues of available and reserved PHP processes and serves each
%% client request in order. PHP processes are reused to cut down
%% on startup overhead and memory limits are checked after each
%% PHP code evaluation to help prevent resource hogging.
%%
%% Configuration is done in the php.app file. Options include the
%% path to the PHP binary, code to initialize the environment,
%% and a default memory limit. It is also possible to set the
%% number of concurrent PHP instances; the default is the number
%% of logical processors available to the Erlang VM.
-module(php).
 
-behaviour(gen_server).
 
%% API
-export([
start/0,
stop/0,
start_link/0,
eval/1,eval/2,eval/3,
reserve/0,reserve/1,
release/1,
get_mem/1,
restart_all/0
]).
 
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
 
-record(state, {
sup,
free = [],
reserved = [],
waiting = [],
restart
}).
 
-record(php, {
ref,
pid,
maxmem
}).
 
-record(restart, {
pids = [],
froms = []
}).
 
%%====================================================================
%% API
%%====================================================================
 
%% @spec start() -> ok
%% @doc Starts the PHP application, supervisor, a number of workers,
%% and this API server module with the options set in php.app.
start() ->
application:start(php).
 
%% @spec stop() -> ok
%% @doc Stops the PHP application and everything it started.
stop() ->
application:stop(php).
 
%% @private
%% @spec start_link() -> {ok, state()}
%% @doc An entry point for the supervisor. This calls init(Pid) with
%% the pid of the supervisor so we can discover the workers.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, self(), []).
 
%% @spec eval(Code) -> result()
%% Code = list() | binary()
%% @type result() = ( {ok, Output, Return, Error, Status} |
%% {parse_error, Error, Status} |
%% {exit, ExitCode | timeout} )
%% Output = binary()
%% Return = any()
%% Error = binary()
%% Status = continue | break
%% ExitCode = integer()
%% @doc Equivalent to eval(Code, undefined, infinity).
eval(Code) ->
eval(Code, undefined, infinity).
 
%% @spec eval(Code, (reference() | Timeout)) -> result()
%% Timeout = integer() | infinity
%% @doc Equivalent to eval(Code, Php, infinity) or eval(Code,
%% undefined, Timeout).
eval(Code, Ref) when is_reference(Ref) ->
eval(Code, Ref, infinity);
eval(Code, Timeout) when is_integer(Timeout), Timeout > 0; Timeout =:= infinity ->
eval(Code, undefined, Timeout);
eval(_, _) ->
{error, invalid_argument}.
 
%% @spec eval(Code, Ref, Timeout) -> result() | {error, term()}
%% Code = list() | binary()
%% Ref = reference() | undefined
%% Timeout = integer() | infinity
%% @doc Tests syntax and evaluates PHP code.
%% A parse error will result in {parse_error, Error, Status}.
%% A fatal error or exit() will result in {exit, ExitCode}.
%% On success, Error contains the last error message. You may
%% call trigger_error() to store a string here but an error or
%% warning (subject to error_reporting()) will overwrite it.
%% Status indicates whether the PHP process continued or broke
%% after evaluation. This suggests that any variables you set
%% in a reserved PHP persist but that can not be guaranteed.
eval(Code, Ref, Timeout) ->
gen_server:call(?MODULE, {eval, Code, Ref, Timeout}, infinity).
 
%% @spec reserve() -> reference()
%% @doc Equivalent to reserve(undefined).
reserve() ->
reserve(undefined).
 
%% @spec reserve(MaxMem) -> reference()
%% MaxMem = integer() | infinity | undefined
%% @doc Reserves a PHP instance that will only be accessible to
%% callers possessing the key returned by this function. A
%% reservation can be passed around or held indefinitely.
%% MaxMem is in KiB. PHP size is measured by `'ps -o rss`'
%% after each evaluation and if it exceeds MaxMem, the PHP
%% instance is restarted and the returned Status is break.
%% If MaxMem is undefined, it becomes the value in php.app.
reserve(MaxMem) ->
gen_server:call(?MODULE, {reserve, MaxMem, ref}, infinity).
 
%% @spec release(reference()) -> ok
%% @doc Cancels the reservation of a PHP instance, returning it
%% to the pool of available instances.
release(Ref) ->
gen_server:cast(?MODULE, {release, Ref}).
 
%% @spec get_mem(reference()) -> integer() | {error, term()}
%% @doc Measures the memory footprint of the PHP instance using
%% `'ps -o rss`'. If the instance has died, it is restarted
%% before the measurement is taken.
get_mem(Ref) ->
gen_server:call(?MODULE, {get_mem, Ref}, infinity).
 
%% @spec restart_all() -> ok
%% @doc Restarts each PHP thread, waiting if any are reserved. This
%% is intended to force an updated PHPLOOP into use.
restart_all() ->
gen_server:call(?MODULE, restart_all, infinity).
 
%%====================================================================
%% gen_server callbacks
%%====================================================================
 
%% @private
init(From) ->
process_flag(trap_exit,true),
State=#state{sup=From,restart=#restart{}},
{ok, State}.
 
%% @private
handle_call(get_state, _From, State) ->
{reply, State, State};
handle_call({eval, Code, Ref, Timeout}, From, State) ->
Php = if
Ref =:= undefined ->
undefined;
true ->
find_php(Ref, State#state.reserved)
end,
if
Php =:= none ->
{reply, {error, invalid_reservation}, State};
true ->
spawn_link( fun () -> do_eval(Code, Php, From, Timeout) end ),
{noreply, State}
end;
handle_call({reserve, MaxMem, What}, From, State) ->
case State#state.waiting of
[] -> % no processes waiting for php reservation
case {State#state.free, State#state.reserved} of
{[],[]} -> % php_eval workers undiscovered (first call to reserve)
[Pid|Free] = lists:foldl(
fun ({_,Pid,_,[php_eval]}, Acc)->[Pid|Acc];
(_, Acc) -> Acc
end,
[],
supervisor:which_children(State#state.sup)
),
Php = make_php(Pid,MaxMem),
{reply, make_reply(Php, What), State#state{free=Free, reserved=[Php]}};
{[],_} -> % all php_eval workers are reserved
Waiting = [{From,MaxMem,What}],
{noreply, State#state{waiting=Waiting}};
{[Pid|Free],_} -> % at least one php_eval worker is free
Php = make_php(Pid,MaxMem),
Reserved = [Php|State#state.reserved],
{reply, make_reply(Php, What), State#state{free=Free, reserved=Reserved}}
end;
_ -> % processes are already waiting
Waiting = State#state.waiting++[{From,MaxMem,What}],
{noreply, State#state{waiting=Waiting}}
end;
handle_call({get_mem, Ref}, From, State) ->
case find_php(Ref, State#state.reserved) of
none -> {reply, {error, invalid_reservation}, State};
Php ->
spawn_link( fun () -> do_get_mem(Php, From) end ),
{noreply, State}
end;
handle_call(restart_all, From, State) ->
Froms = (State#state.restart)#restart.froms,
Pids = all_pids(State),
[spawn(fun()->eval(";")end) || _ <- lists:seq(1,length(Pids))],
{noreply, State#state{restart=#restart{froms=[From|Froms],pids=Pids}}};
handle_call(Request, _From, State) ->
{reply, {unknown_call, Request}, State}.
 
%% @private
handle_cast({release, Ref}, State) ->
case find_php(Ref, State#state.reserved) of
none ->
{noreply, State};
Php ->
State2 = maybe_restart(Php#php.pid, State),
State3 = do_release(Php, State2),
{noreply, State3}
end;
handle_cast(_Msg, State) ->
{noreply, State}.
 
%% @private
handle_info(_Info, State) ->
{noreply, State}.
 
%% @private
terminate(_Reason, _State) ->
ok.
 
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
 
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
 
reserve_php() ->
gen_server:call(?MODULE, {reserve, undefined, php}, infinity).
 
maybe_restart(Pid, #state{restart=#restart{froms=Froms,pids=Pids}}=State) ->
case lists:member(Pid, Pids) of
false ->
State;
true ->
gen_server:call(Pid, {eval, "exit;", 1, infinity}, infinity),
Pids2 = lists:delete(Pid, Pids),
if
Pids2 =:= [] ->
Restart = #restart{},
lists:foreach(
fun (From) -> gen_server:reply(From, ok) end,
Froms
);
true ->
Restart = #restart{froms=Froms,pids=Pids2}
end,
State#state{restart=Restart}
end.
 
do_release(Php, State) ->
Reserved = lists:delete(Php, State#state.reserved),
Free = State#state.free ++ [Php#php.pid],
case State#state.waiting of
[] -> % no processes in the queue
State#state{reserved=Reserved, free=Free};
[{From,MaxMem,What}|Waiting] -> % there is a process waiting for a reservation
[Pid|NewFree] = Free,
NextPhp=make_php(Pid,MaxMem),
gen_server:reply(From, make_reply(NextPhp, What)),
State#state{waiting=Waiting, reserved=[NextPhp|Reserved], free=NewFree}
end.
 
do_eval(Code, #php{pid=Pid,maxmem=MaxMem}, From, Timeout) ->
Reply = gen_server:call(Pid, {eval, Code, Timeout, MaxMem}, infinity),
gen_server:reply(From, Reply);
do_eval(Code, _, From, Timeout) ->
Php = reserve_php(),
do_eval(Code, Php, From, Timeout),
release(Php#php.ref).
 
do_get_mem(#php{pid=Pid}, From) ->
Mem = gen_server:call(Pid, get_mem),
gen_server:reply(From, Mem).
 
all_pids(#state{free=[], reserved=[]}) ->
[];
all_pids(#state{free=Free,reserved=Reserved}) ->
lists:foldl(
fun (#php{pid=Pid}, Acc) -> [Pid|Acc] end,
Free,
Reserved
).
 
make_php(Pid, MaxMem) ->
#php{ref=make_ref(),pid=Pid,maxmem=MaxMem}.
 
find_php(_, []) ->
none;
find_php(Ref, [#php{ref=Ref}=Php|_]) ->
Php;
find_php(Ref, [_|T]) ->
find_php(Ref, T).
 
make_reply(Php, php) ->
Php;
make_reply(#php{ref=Ref}, ref) ->
Ref.