fix issue

This commit is contained in:
安礼成 2023-03-10 17:19:15 +08:00
parent 70d624aeb4
commit 7bdaad2013
3 changed files with 41 additions and 8 deletions

View File

@ -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

View File

@ -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.

View File

@ -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) ->