This commit is contained in:
anlicheng 2024-07-12 11:25:59 +08:00
parent a3354f3145
commit 99bb1aa1d0
2 changed files with 7 additions and 12 deletions

View File

@ -32,7 +32,7 @@
%%
-spec submit(Task :: fun()) -> no_return().
submit(Task) when is_function(Task, 0) ->
gen_server:cast(?SERVER, Task).
gen_server:cast(?SERVER, {submit, Task}).
debug_info() ->
gen_server:cast(?SERVER, debug_info).

View File

@ -12,7 +12,7 @@
-behaviour(gen_server).
%% API
-export([start_link/0]).
-export([start_link/1]).
-export([execute/2]).
%% gen_server callbacks
@ -33,10 +33,10 @@ execute(Pid, Task) when is_pid(Pid), is_function(Task, 0) ->
gen_server:call(Pid, {execute, Task}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
-spec(start_link(Args :: list()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
start_link(Args) when is_list(Args) ->
gen_server:start_link(?MODULE, Args, []).
%%%===================================================================
%%% gen_server callbacks
@ -47,7 +47,7 @@ start_link() ->
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
init(_) ->
{ok, #state{}}.
%% @private
@ -61,12 +61,7 @@ init([]) ->
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call({execute, Task}, _From, State = #state{}) ->
case catch Task() of
{error, Reason} ->
lager:warning("[iot_task_worker] execute task get error: ~p", Reason);
_ ->
ok
end,
catch Task(),
{reply, ok, State}.
%% @private