add iot_task
This commit is contained in:
parent
686187a9cc
commit
9119aaff69
@ -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(<<?API_TOKEN/binary, (integer_to_binary(Id))/binary, ?API_TOKEN/binary>>),
|
||||
{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).
|
||||
end,
|
||||
iot_task:submit(Task).
|
||||
@ -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, []},
|
||||
|
||||
@ -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}.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user