我寫了一個gen_server模塊(data_cahe.erl),它將數據保存在ETS中。ERLang OTP gen_server:call()失敗
我的代碼如下:
-export([start_link/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-define(TABLE_ID, ?MODULE).
-record(state, {user_id, my_reading, my_status}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
init([]) ->
{ok, ?TABLE_ID} = new_cache(?TABLE_ID),
{ok, #state{user_id=undefined, my_reading=undefined, my_status=undefined}}.
的handle_call:
handle_call({save, UserId, Readings}, _From, _Status) ->
io:format("Inside handle_call_save: ~n~p~n",[]);
%Check if email is present
case my_reading(UserId) of
{error, not_found} -> %%Email not present
io:format("Inside handle_call_save Just before save: ~n~p~n",[]),
Result = save_my_readings(UserId, Readings),
{reply, ok, #state{user_id=UserId, my_reading=Readings, my_status=Result}};
{ok, Reading} ->
io:format("Inside handle_call_save Just before delete and save: ~n~p~n",[]),
delete_my_reading(UserId), %%delete last reading
Result = save_my_readings(UserId, Readings), %%Save this new Reading
{reply, ok, #state{user_id=UserId, my_reading=Readings, my_status=Result}}
end;
我使用這個handel_call(可以訪問電子郵件和的accessToken)從保存在ETS數據嘗試工人模塊:
case my_app_interface:get_data_summary(binary_to_list(AccessToken)) of
{error, _Reason1} ->
%%Start a new Timer Cycle
..
..
Readings1 ->
gen_server:call(data_cahe, {save, Email, Readings1}), %%HERE IT CRASHES
io:format("Get Data Summary : ~n~p~n",[Readings1]) %%This is printed when the line above is commented
end,
然而gen_server:調用(...)崩潰。當我註釋掉這一行時,讀數按通常順序打印。
我甚至已經列出了除了handle_call方法中的print語句外的所有行 - 但沒有任何內容被打印。看起來gen_server:call(...)根本沒有經過。如果有人發出什麼問題,會非常感激。
由於您的程序崩潰,如果包含錯誤消息,它會很好。 – mpm 2014-10-28 22:51:27