inaka / worker_pool

Erlang worker pool
https://hex.pm/packages/worker_pool
Apache License 2.0
276 stars 80 forks source link

gen_server with send_after can't work #136

Closed boboxiaodd closed 6 years ago

boboxiaodd commented 6 years ago
%%%-------------------------------------------------------------------
%%% @author linhaibo
%%% @copyright (C) 2018, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 25. 四月 2018 下午10:54
%%%-------------------------------------------------------------------
-module(task_worker).
-author("linhaibo").

-behaviour(gen_server).

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

%% gen_server callbacks
-export([init/1,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    terminate/2,
    code_change/3]).

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

add_task(M,F,A,Timeout) -> wpool:cast(?POOL_TASK,{add_task,M,F,A,Timeout},available_worker).

start_link() ->
    gen_server:start_link(?MODULE, [], []).

init([]) ->
    process_flag(trap_exit, true),
    error_logger:info_msg("task worker start..."),
    {ok, #state{}}.

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

handle_cast({add_task,M,F,A,Timeout}, State) ->
    error_logger:info_msg("add task ~p ~p",[self()]),
    erlang:send_after(Timeout,self(),{run_task,M,F,A}),
    {noreply, State};

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

handle_info({run_task,M,F,A} ,State) ->
    error_logger:info_msg("run task"),
    apply(M,F,A),
    {noreply, State};

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

terminate(_Reason, _State) ->
    ok.

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

run task can't trigger

boboxiaodd commented 6 years ago

my bad。。。