diff --git a/apps/iot/src/iot_api.erl b/apps/iot/src/iot_api.erl index 5ed9f07..abf5f34 100644 --- a/apps/iot/src/iot_api.erl +++ b/apps/iot/src/iot_api.erl @@ -14,8 +14,9 @@ %% API -export([ai_event/1]). +-spec ai_event(Id :: integer()) -> no_return(). ai_event(Id) when is_integer(Id) -> - spawn(fun() -> + Task = fun() -> Token = iot_util:md5(<>), {ok, Url} = application:get_env(iot, api_url), @@ -39,4 +40,5 @@ ai_event(Id) when is_integer(Id) -> {error, Reason} -> lager:warning("[iot_api] send body: ~p, get error is: ~p", [Body, Reason]) end - end). \ No newline at end of file + end, + iot_task:submit(Task). \ No newline at end of file diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index 6a8255e..4d2fea6 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -28,6 +28,15 @@ start_link() -> init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, Specs = [ + #{ + id => 'iot_task', + start => {'iot_task', start_link, []}, + restart => permanent, + shutdown => 2000, + type => worker, + modules => ['iot_task'] + }, + #{ id => 'iot_event_period_settings', start => {'iot_event_period_settings', start_link, []}, diff --git a/apps/iot/iot_task.erl b/apps/iot/src/iot_task.erl similarity index 86% rename from apps/iot/iot_task.erl rename to apps/iot/src/iot_task.erl index 8a34c6c..0a151e3 100644 --- a/apps/iot/iot_task.erl +++ b/apps/iot/src/iot_task.erl @@ -13,7 +13,7 @@ %% API -export([start_link/0]). --export([submit/1]). +-export([submit/1, debug_info/0]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -21,7 +21,7 @@ -define(SERVER, ?MODULE). -record(state, { - + counter = 0 }). %%%=================================================================== @@ -33,6 +33,9 @@ submit(Task) when is_function(Task, 0) -> gen_server:cast(?SERVER, Task). +debug_info() -> + gen_server:cast(?SERVER, debug_info). + %% @doc Spawns the server and registers the local name (unique) -spec(start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). @@ -72,6 +75,9 @@ handle_call(_Request, _From, State = #state{}) -> {stop, Reason :: term(), NewState :: #state{}}). handle_cast({submit, Task}, State = #state{}) -> spawn_monitor(fun() -> Task() end), + {noreply, State}; +handle_cast(debug_info, State = #state{counter = Counter}) -> + lager:debug("[iot_task] execute task_num: ~p", [Counter]), {noreply, State}. %% @private @@ -80,6 +86,13 @@ handle_cast({submit, Task}, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). +%% Task进程挂掉 +handle_info({'DOWN', _MRef, process, _Pid, normal}, State=#state{counter = Counter}) -> + {noreply, State#state{counter = Counter + 1}}; +handle_info({'DOWN', _MRef, process, _Pid, Reason}, State) -> + lager:notice("[iot_task] task process down with reason: ~p", [Reason]), + {noreply, State}; + handle_info(_Info, State = #state{}) -> {noreply, State}.