Gen Server Error noproc

时间:2016-05-05 00:40:06

标签: erlang gen-server

当我尝试使用erlang.mk通过shell运行程序时出现以下错误。

=INFO REPORT==== 5-May-2016::05:47:57 ===
application: rad
exited: {bad_return,
            {{rad_app,start,[normal,[]]},
             {'EXIT',
                 {noproc,{gen_server,call,[rad_config,{lookup,port}]}}}}}
type: permanent
Eshell V6.4  (abort with ^G)
(rad@127.0.0.1)1> {"Kernel pid terminated",application_controller,"{application_start_failure,rad,{bad_return,{{rad_app,start,[normal,[]]},{'EXIT',{noproc,{gen_server,call,[rad_config,{lookup,port}]}}}}}}"}
Kernel pid terminated (application_controller) ({application_start_failure,rad,{bad_return,{{rad_app,start,[normal,[]]},{'EXIT',{noproc,{gen_server,call,[radheart: Thu May  5 05:47:58 2016: _coErlang is crashing .. (waiting for crash dump file)nf
ig,{lookup,porheart: Thu May  5 05:47:58 2016: tWould reboot. Terminating.}
]}}}}}})
make: *** [run] Error 1

rad.app.src

{application, rad,
[
{description, "Awesome server written in Erlang"},
{vsn, "0.0.1"},
{registered, [rad_sup, rad_config]},
{modules, []},
{applications, [
  kernel,
  stdlib,
  cowboy
]},
{mod, {rad_app, []}},
{env, []}
]}.

rad_config.erl

-module(rad_config).
-behaviour(gen_server).

%% API
-export([start_link/0]).

%% Gen Server Callbacks
-export([init/1 , handle_call/3 , handle_cast/2 , handle_info/2 , terminate/2 , code_change/3]).
-export([lookup/1]).

-define(SERVER, ?MODULE).
-record(state, {conf}).

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
    {ok, Conf} = file:consult("config/rad.cfg"),
    {ok, #state{conf = Conf}}.

handle_call({lookup, Tag} , _From , State) ->
    Reply = case lists:keyfind(Tag, 1, State#state.conf) of
            {Tag, Value} ->
                Value;
                    false ->
                        {error, noinstance}
        end,
    {reply, Reply, State};

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

handle_cast(_Msg , State) ->
    {noreply, State}.

handle_info(_Info , State) ->
    {noreply, State}.

terminate(Reason , _State) ->
    io:format("~n Server shutdown. Reason: ~s.~n", [Reason]),
    ok.

code_change(_OldVsn , State , _Extra) ->
    {ok, State}.

lookup(Tag) ->
    gen_server:call(?SERVER, {lookup, Tag}).

rad_app.erl

-module(rad_app).
-behaviour(application).

-export([start/2]).
-export([stop/1]).

%%  First we need to define and compile the dispatch list, a list of routes
%%  that Cowboy will use to map requests to handler modules. Then we tell
%%  Cowboy to listen for connections.
start(_Type, _Args) ->
    Port = rad_config:lookup(port),

    Dispatch = cowboy_router:compile([
    {'_', [{"/", route_handler, []}]}
      ]),
    {ok, _} = cowboy:start_http(rad_http_listener, 100,
    [{port, Port}], [{env, [{dispatch, Dispatch}]}]),

    %% Start the supervisor
    rad_sup:start_link().

stop(_State) ->
    ok.

rad_sup.erl

-module(rad_sup).
-behaviour(supervisor).

%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1, shutdown/0]).

-define(SERVER, ?MODULE).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%%         ===================================================================
%% API functions
%% ===================================================================

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
    RestartStrategy = simple_one_for_one,
    MaxRestarts = 10,
    MaxSecondsBwRestarts = 5,

    SupFlag = {RestartStrategy, MaxRestarts, MaxSecondsBwRestarts},

    Processes = [?CHILD(rad_config, worker)],
    {ok, {SupFlag, Processes}}.

%% Supervisor can be shutdown by calling exit(SupPid,shutdown)
%% or, if it's linked to its parent, by parent calling exit/1.
shutdown() ->
    exit(whereis(?MODULE), shutdown).

所以基本上我有两个与此处引发的错误相关的问题:

  1. 是否因为我的gen_server无法启动而引发此错误?

  2. rad_config对应file:consult/1的行,我想询问此函数从哪里获取文件,就像我传递给它的参数是{{ 1}}但所有config/rad.cfg文件都存储在.erl文件夹中。这些文件夹srcsrc都位于同一目录级别。那么,我传递给config的参数是否正确?虽然我也尝试将参数传递给file:consult/1。我仍然得到同样的错误。

  3. 请帮帮我。我../config/rad.cfgnew我试图解决这个错误很长一段时间了。顺便说一句,我使用Erlang

1 个答案:

答案 0 :(得分:3)

首先,似乎当您运行rad_app.erl时,您的rad_config服务器尚未启动。所以,当你到达这一行:

Port = rad_config:lookup(port)

你实际上在呼唤:

lookup(Tag) ->
    gen_server:call(?SERVER, {lookup, Tag}).

gen_server未启动,因此您收到noproc错误。

除此之外,即使服务器已经启动,您也无法为自己制作gen_server:call。处理您想要自己发送事件的案例的最佳方法是使用spawn打开一个新流程,并从生成的流程内部进行调用。

您应该详细了解gen_serverOTP