dding service -> worker template

This commit is contained in:
Craig Everett 2020-09-26 12:58:17 +09:00
parent 55e3ac871f
commit 378a30e112
53 changed files with 322 additions and 31 deletions

View File

@ -1 +1 @@
0.11.6
0.11.7

View File

@ -1,6 +1,6 @@
{application,zx,
[{description,"An Erlang development tool and Zomp user client"},
{vsn,"0.11.6"},
{vsn,"0.11.7"},
{applications,[stdlib,kernel]},
{modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key,
zx_lib,zx_local,zx_net,zx_peer,zx_peer_man,

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -24,7 +24,7 @@
%%% @end
-module(zx).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(application).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -9,7 +9,7 @@
%%% @end
-module(zx_auth).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end
-module(zx_conn).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_conn_sup).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -138,7 +138,7 @@
%%% @end
-module(zx_daemon).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -8,7 +8,7 @@
%%% @end
-module(zx_key).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -10,7 +10,7 @@
%%% @end
-module(zx_lib).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_local).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_net).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -8,7 +8,7 @@
%%% @end
-module(zx_peer).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -9,7 +9,7 @@
%%% @end
-module(zx_peer_man).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_peer_sup).
-vsn("0.11.6").
-vsn("0.11.7").
-behaviour(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -10,7 +10,7 @@
%%% @end
-module(zx_peers).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_proxy).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_sup).
-vsn("0.11.6").
-vsn("0.11.7").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_tty).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_userconf).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end
-module(zx_zsp).
-vsn("0.11.6").
-vsn("0.11.7").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -243,7 +243,7 @@ do_ignore(State = #s{listener = none}) ->
State;
do_ignore(State = #s{listener = Listener}) ->
ok = gen_tcp:close(Listener),
State#s{listener = none}.
State#s{port_num = none, listener = none}.
-spec do_enroll(Pid, State) -> NewState

View File

@ -35,9 +35,9 @@
| {shutdown, term()}
| term().
%% @private
%% Spawns the first listener at the request of the *PREFIX*_client_man when es:listen/1
%% is called, or the next listener at the request of the currently listening *PREFIX*_client
%% when a connection is made.
%% Spawns the first listener at the request of the *PREFIX*_client_man when
%% *APP MOD*:listen/1 is called, or the next listener at the request of the
%% currently listening *PREFIX*_client when a connection is made.
%%
%% Error conditions, supervision strategies and other important issues are
%% explained in the supervisor module docs:

View File

@ -32,17 +32,17 @@ start_link() ->
init(none) ->
RestartStrategy = {rest_for_one, 1, 60},
ClientSup = {*PREFIX*_client_sup,
{*PREFIX*_client_sup, start_link, []},
permanent,
5000,
supervisor,
[*PREFIX*_client_sup]},
ClientMan = {*PREFIX*_client_man,
{*PREFIX*_client_man, start_link, []},
permanent,
5000,
worker,
[*PREFIX*_client_man]},
ClientSup = {*PREFIX*_client_sup,
{*PREFIX*_client_sup, start_link, []},
permanent,
5000,
supervisor,
[*PREFIX*_client_sup]},
Children = [ClientSup, ClientMan],
{ok, {RestartStrategy, Children}}.

View File

@ -0,0 +1,73 @@
%%% @doc
%%% *PROJECT NAME*: *CAP SERVICE* Worker
%%% @end
-module(*PREFIX*_*SERVICE*).
-vsn("〘*VERSION*〙").
-behavior(gen_server).
*AUTHOR*
*COPYRIGHT*
*LICENSE*
%% gen_server
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]).
-include("$zx_include/zx_logger.hrl").
%%% Type and Record Definitions
-record(s,
{}).
-type state() :: #s{}.
%%% Interface
%%% gen_server
-spec start_link() -> Result
when Result :: {ok, pid()}
| {error, Reason :: term()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
init(none) ->
State = #s{},
{ok, State}.
handle_call(Unexpected, From, State) ->
ok = log(warning, "Unexpected call from ~tp: ~tp", [From, Unexpected]),
{noreply, State}.
handle_cast(Unexpected, State) ->
ok = log(warning, "Unexpected cast: ~tp", [Unexpected]),
{noreply, State}.
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp", [Unexpected]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%% Doer Functions

View File

@ -0,0 +1,119 @@
%%% @doc
%%% *PROJECT NAME*: *CAP SERVICE* Worker Manager
%%% @end
-module(*PREFIX*_*SERVICE*_man).
-vsn("〘*VERSION*〙").
-behavior(gen_server).
*AUTHOR*
*COPYRIGHT*
*LICENSE*
%% Worker interface
-export([enroll/0]).
%% gen_server
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]).
-include("$zx_include/zx_logger.hrl").
%%% Type and Record Definitions
-record(s,
{*SERVICE*s = [] :: [pid()]}).
-type state() :: #s{}.
%%% Service Interface
%%% Worker Interface
-spec enroll() -> ok.
%% @doc
%% Workers register here after they initialize.
enroll() ->
gen_server:cast(?MODULE, {enroll, self()}).
%%% gen_server
-spec start_link() -> Result
when Result :: {ok, pid()}
| {error, Reason :: term()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
init(none) ->
State = #s{},
{ok, State}.
handle_call(Unexpected, From, State) ->
ok = log(warning, "Unexpected call from ~tp: ~tp", [From, Unexpected]),
{noreply, State}.
handle_cast({enroll, PID}, State) ->
NewState = do_enroll(PID, State),
{noreply, NewState};
handle_cast(Unexpected, State) ->
ok = log(warning, "Unexpected cast: ~tp", [Unexpected]),
{noreply, State}.
handle_info({'DOWN', Mon, process, PID, Reason}, State) ->
NewState = handle_down(Mon, PID, Reason, State),
{noreply, NewState};
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp", [Unexpected]),
{noreply, State}.
handle_down(Mon, PID, Reason, State = #s{clients = Clients}) ->
case lists:member(PID, Clients) of
true ->
NewClients = lists:delete(PID, Clients),
State#s{clients = NewClients};
false ->
Unexpected = {'DOWN', Mon, process, PID, Reason},
ok = log(warning, "Unexpected info: ~tp", [Unexpected]),
State
end.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%% Doer Functions
-spec do_enroll(PID, State) -> NewState
when PID :: pid(),
State :: state(),
NewState :: state().
do_enroll(PID, State = #s{*SERVICE*s = *CAP SERVICE*s}) ->
case lists:member(PID, *CAP SERVICE*s) of
false ->
Mon = monitor(process, PID),
ok = log(info, "Enroll: ~tp @ ~tp", [PID, Mon]),
State#s{*SERVICE*s = [PID | *CAP SERVICE*s]};
true ->
State
end.

View File

@ -0,0 +1,49 @@
%%% @doc
%%% *PROJECT NAME* : *CAP SERVICE* Worker Supervisor
%%% @end
-module(*PREFIX*_*SERVICE*_sup).
-vsn("〘*VERSION*〙").
-behaviour(supervisor).
*AUTHOR*
*COPYRIGHT*
*LICENSE*
-export([start_acceptor/1]).
-export([start_link/0]).
-export([init/1]).
-spec start_acceptor(ListenSocket) -> Result
when ListenSocket :: gen_tcp:socket(),
Result :: {ok, pid()}
| {error, Reason},
Reason :: {already_started, pid()}
| {shutdown, term()}
| term().
start_acceptor(ListenSocket) ->
supervisor:start_child(?MODULE, [ListenSocket]).
-spec start_link() -> {ok, pid()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, none).
-spec init(none) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% @private
%% The OTP init/1 function.
init(none) ->
RestartStrategy = {simple_one_for_one, 1, 60},
*CAP SERVICE* =
{*PREFIX*_*SERVICE*,
{*PREFIX*_*SERVICE*, start_link, []},
temporary,
brutal_kill,
worker,
[*PREFIX*_*SERVICE*]},
{ok, {RestartStrategy, [*CAP SERVICE*]}}.

View File

@ -0,0 +1,50 @@
%%% @doc
%%% *PROJECT NAME*: *CAP SERVICE* Service Supervisor
%%%
%%% This is the service-level supervisor of the system. It is the parent of both the
%%% client connection handlers and the client manager (which manages the client
%%% connection handlers). This is the child of *PREFIX*_sup.
%%%
%%% See: http://erlang.org/doc/apps/kernel/application.html
%%% @end
-module(*PREFIX*_*SERVICE*s).
-vsn("〘*VERSION*〙").
-behavior(supervisor).
*AUTHOR*
*COPYRIGHT*
*LICENSE*
-export([start_link/0]).
-export([init/1]).
-spec start_link() -> {ok, pid()}.
%% @private
%% This supervisor's own start function.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, none).
-spec init(none) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% @private
%% The OTP init/1 function.
init(none) ->
RestartStrategy = {rest_for_one, 1, 60},
*CAP SERVICE*Man =
{*PREFIX*_*SERVICE*_man,
{*PREFIX*_*SERVICE*_man, start_link, []},
permanent,
5000,
worker,
[*PREFIX*_*SERVICE*_man]},
*CAP SERVICE*Sup =
{*PREFIX*_*SERVICE*_sup,
{*PREFIX*_*SERVICE*_sup, start_link, []},
permanent,
5000,
supervisor,
[*PREFIX*_*SERVICE*_sup]},
Children = [*CAP SERVICE*Sup, *CAP SERVICE*Man],
{ok, {RestartStrategy, Children}}.

View File

@ -9,7 +9,7 @@
{license,"MIT"}.
{modules,[]}.
{name,"zx"}.
{package_id,{"otpr","zx",{0,11,6}}}.
{package_id,{"otpr","zx",{0,11,7}}}.
{prefix,"zx_"}.
{repo_url,"https://gitlab.com/zxq9/zx"}.
{tags,["tools","package manager","erlang"]}.