-6

%%sometimes my loop returns ok because of timeout how to write this code in proper way.when there is a timeout it just returns ok but not my actual value that i am assuming.In handle call i am calling a function loop() in the loop() function i am receiving a message with receive clause .Now i am sending this data to my database using loop2 function returns response from database whether data has been sucessfully saved or not and giving response back to loop().But if there is a timeout my loop function returns ok but not actual value

 -module(getAccDataCons).
-behaviour(gen_server).
-include_lib("deps/amqp_client/include/amqp_client.hrl").
-export([start_link/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3,
         terminate/2]).
-export([get_account/0]).
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
    gen_server:cast(?MODULE, stop).
get_account() ->
    gen_server:call(?MODULE, {get_account}).
init(_Args) ->
    {ok, Connection} = amqp_connection:start(#amqp_params_network{host = "localhost"}),
    {ok, Channel} = amqp_connection:open_channel(Connection),
    {ok, Channel}.
handle_call({get_account}, _From, State) ->
    amqp_channel:call(State, #'exchange.declare'{exchange = <<"get">>, type = <<"topic">>}),
    amqp_channel:call(State, #'queue.declare'{queue = <<"get_account">>, durable = true}),
    Binding =
        #'queue.bind'{exchange = <<"get">>,
                      routing_key = <<"get.account">>,
                      queue = <<"get_account">>},
    #'queue.bind_ok'{} = amqp_channel:call(State, Binding),
    io:format(" [*] Waiting for logs. To exit press CTRL+C~n"),
    amqp_channel:subscribe(State,#'basic.consume'{queue = <<"get_account">>, no_ack = true},self()),
    Returned =loop(),
    io:format("~nReti=~p",[Returned]),
    {reply, Returned, State};
handle_call(Message, _From, State) ->
    io:format("received other handle_call message: ~p~n", [Message]),
    {reply, ok, State}.
handle_cast(stop, State) ->
    {stop, normal, State};
handle_cast(Message, State) ->
    io:format("received other handle_cast call : ~p~n", [Message]),
    {noreply, State}.
handle_info(Message, State) ->
    io:format("received handle_info message : ~p~n", [Message]),
    {noreply, State}.
code_change(_OldVer, State, _Extra) ->
    {ok, State}.
terminate(Reason, _State) ->
    io:format("server is terminating with reason :~p~n", [Reason]).
    loop()->
        receive
         #'basic.consume_ok'{} ->
             loop();
             {#'basic.deliver'{}, Msg} ->
                 #amqp_msg{payload = Payload} = Msg,
                 Value=loop2(Payload),
         Value
     after 200->
     io:format("timeout")
     end.`
Nick Jonas
  • 103
  • 1
  • 7

1 Answers1

1

Your loop/0 function evaluates a receive statement.

The result of evaluating a receive statement in the case of a timeout is the result of evaluating the list of expressions within its after block. In your case, that is io:format(" Server timeout ") and that prints out Server timeout and evaluates to ok.

That's why the whole function evaluates to (i.e. "returns") ok.

Brujo Benavides
  • 1,919
  • 12
  • 15