diff --git a/apps/iot/src/iot_issue.erl b/apps/iot/src/iot_issue.erl index b980933..7091473 100644 --- a/apps/iot/src/iot_issue.erl +++ b/apps/iot/src/iot_issue.erl @@ -8,11 +8,12 @@ %%%------------------------------------------------------------------- -module(iot_issue). -author("licheng5"). +-include("iot.hrl"). -behaviour(gen_server). %% API --export([start_link/0]). +-export([start_link/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -20,18 +21,22 @@ -define(SERVER, ?MODULE). -record(state, { - + issue :: #issue{} }). %%%=================================================================== %%% API %%%=================================================================== +get_name(IssueId) when is_integer(IssueId) -> + list_to_atom("iot_issue:" ++ integer_to_list(IssueId)). + %% @doc Spawns the server and registers the local name (unique) --spec(start_link() -> +-spec(start_link(Issue :: #issue{}) -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). -start_link() -> - gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). +start_link(Issue = #issue{issue_id = IssueId}) -> + Name = get_name(IssueId), + gen_server:start_link({local, Name}, ?MODULE, [Issue], []). %%%=================================================================== %%% gen_server callbacks @@ -42,8 +47,9 @@ start_link() -> -spec(init(Args :: term()) -> {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | {stop, Reason :: term()} | ignore). -init([]) -> - {ok, #state{}}. +init([Issue]) -> + lager:debug("iot_issue started!!"), + {ok, #state{issue = Issue}}. %% @private %% @doc Handling call messages diff --git a/apps/iot/src/iot_issue_sup.erl b/apps/iot/src/iot_issue_sup.erl index 0b71ae7..9c4a369 100644 --- a/apps/iot/src/iot_issue_sup.erl +++ b/apps/iot/src/iot_issue_sup.erl @@ -8,11 +8,12 @@ %%%------------------------------------------------------------------- -module(iot_issue_sup). -author("licheng5"). +-include("iot.hrl"). -behaviour(supervisor). %% API --export([start_link/0]). +-export([start_link/0, start_issue/1]). %% Supervisor callbacks -export([init/1]). @@ -58,3 +59,15 @@ init([]) -> %%%=================================================================== %%% Internal functions %%%=================================================================== + +%% 启动一个任务 +-spec start_issue(Issue :: #issue{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}. +start_issue(Issue = #issue{}) -> + case supervisor:start_child(?MODULE, [Issue]) of + {ok, Pid} -> + {ok, Pid}; + {error, {'already_started', Pid}} -> + {ok, Pid}; + Error -> + Error + end. \ No newline at end of file diff --git a/apps/iot/src/iot_mock.erl b/apps/iot/src/iot_mock.erl index 8eb16e3..f0d7b0d 100644 --- a/apps/iot/src/iot_mock.erl +++ b/apps/iot/src/iot_mock.erl @@ -14,6 +14,20 @@ -export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]). -export([start_router/1]). -export([rsa_encode/1]). +-export([start_issue/0]). + +start_issue() -> + iot_issue_sup:start_issue(#issue{ + issue_id = 1, + name = <<"issue 1">>, + uid = 1234, + deploy_type = 1, + assoc_id = 1, + hosts = [<<"host1">>, <<"host2">>], + timeout = 6, + create_ts = 0, + status = 0 + }). insert_hosts() -> lists:foreach(fun(Id0) ->