2

I was following this example of executing periodic tasks given by Hynek -Pichi- Vychodil here.

And i ran into a problem as I wanted to pass some Arguments also to the start_link function which would be used inside my do_task() function. But as given here the start_link/4 needs to return {ok,Pid} and in my case it is returning {ok,{Ref,Arguments}} and thus is failing.

How I can I fix this up.Here's my code:

start_link(Period,SERVER,Args) when Period > 0, is_integer(Period) ->
    gen_server:start_link({local, SERVER}, ?MODULE, [Period,Args], []).


init([Period,Args]) ->
    StartT = erlang:monotonic_time(millisecond),
    self() ! tick,
    {ok, {StartT, Period,Args}}.

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

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


handle_info(tick, {StartT, Period,Args} = S) ->
    Next = Period - (erlang:monotonic_time(millisecond)-StartT) rem Period,
    _Timer = erlang:send_after(Next, self(), tick),

    do_task(Args),
    {ok, S};

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

Here Period is->30000 and Arguments is -> {A,[a,b],'something'}

And here is the crash log

[error] gen_server '95ef60ae-b2fa-491a-821d-ffae85cc57f6' terminated with reason: bad return value: {ok,{-576460723187,30000,{A,[a,b],'something'}}
abhishek ranjan
  • 612
  • 7
  • 23

1 Answers1

3

handle_info cannot return an ok tuple.

(Answer given as a comment.)

aronisstav
  • 7,755
  • 5
  • 23
  • 48
  • More generally, handle_info should return a noreply tuple. I got a similar error (in Elixir) because of a code path that only returned "state". – Sinc Nov 15 '21 at 18:56