mirror of
https://github.com/valitydev/machinery-erlang.git
synced 2024-11-06 00:35:19 +00:00
Add formatter (#29)
* Add erlfmt * Apply erlfmt * Update build-utils * Fix module names in elvis config
This commit is contained in:
parent
9d7e4b28cd
commit
032b66457e
8
Makefile
8
Makefile
@ -9,7 +9,7 @@ SUBTARGETS = $(patsubst %,%/.git,$(SUBMODULES))
|
||||
SERVICE_NAME := machinery
|
||||
BUILD_IMAGE_TAG := 0c638a682f4735a65ef232b81ed872ba494574c3
|
||||
|
||||
CALL_ANYWHERE := all submodules compile xref lint dialyze clean distclean
|
||||
CALL_ANYWHERE := all submodules compile xref lint dialyze clean distclean check_format format
|
||||
|
||||
CALL_W_CONTAINER := $(CALL_ANYWHERE) test
|
||||
|
||||
@ -34,6 +34,12 @@ xref: submodules
|
||||
lint:
|
||||
elvis rock
|
||||
|
||||
check_format:
|
||||
$(REBAR) as test fmt -c
|
||||
|
||||
format:
|
||||
$(REBAR) fmt -w
|
||||
|
||||
dialyze: submodules
|
||||
$(REBAR) dialyzer
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 540183862bc9fd04682e226de2056a320fd44be9
|
||||
Subproject commit f42e059d9ec93826ba4ad23232eed8ce67bd5486
|
@ -12,7 +12,7 @@
|
||||
{elvis_style, macro_module_names},
|
||||
{elvis_style, operator_spaces, #{rules => [{right, ","}, {right, "++"}, {left, "++"}]}},
|
||||
{elvis_style, nesting_level, #{level => 3}},
|
||||
{elvis_style, god_modules, #{limit => 30, ignore => [hg_client_party]}},
|
||||
{elvis_style, god_modules, #{limit => 30}},
|
||||
{elvis_style, no_if_expression},
|
||||
{elvis_style, invalid_dynamic_call, #{ignore => [elvis]}},
|
||||
{elvis_style, used_ignored_variable},
|
||||
|
@ -56,3 +56,12 @@
|
||||
]},
|
||||
{plt_apps, all_deps}
|
||||
]}.
|
||||
|
||||
{plugins, [
|
||||
{erlfmt, "0.8.0"}
|
||||
]}.
|
||||
|
||||
{erlfmt, [
|
||||
{print_width, 120},
|
||||
{files, "{src,include,test}/*.{hrl,erl}"}
|
||||
]}.
|
||||
|
@ -10,33 +10,33 @@
|
||||
|
||||
-module(machinery).
|
||||
|
||||
-type namespace() :: atom().
|
||||
-type ref() :: id() | {tag, tag()}.
|
||||
-type id() :: binary().
|
||||
-type tag() :: binary().
|
||||
-type args(T) :: T.
|
||||
-type response(T) :: T.
|
||||
-type error(T) :: T.
|
||||
-type namespace() :: atom().
|
||||
-type ref() :: id() | {tag, tag()}.
|
||||
-type id() :: binary().
|
||||
-type tag() :: binary().
|
||||
-type args(T) :: T.
|
||||
-type response(T) :: T.
|
||||
-type error(T) :: T.
|
||||
|
||||
-type usec_part() :: 0..999999.
|
||||
-type timestamp() :: {calendar:datetime(), usec_part()}.
|
||||
-type usec_part() :: 0..999999.
|
||||
-type timestamp() :: {calendar:datetime(), usec_part()}.
|
||||
|
||||
-type event_id() :: integer().
|
||||
-type event_body(T) :: T.
|
||||
-type event(T) :: {event_id(), timestamp(), event_body(T)}.
|
||||
-type history(T) :: [event(T)].
|
||||
-type aux_state(T) :: T.
|
||||
-type event_id() :: integer().
|
||||
-type event_body(T) :: T.
|
||||
-type event(T) :: {event_id(), timestamp(), event_body(T)}.
|
||||
-type history(T) :: [event(T)].
|
||||
-type aux_state(T) :: T.
|
||||
|
||||
-type event_cursor() :: undefined | event_id().
|
||||
-type limit() :: undefined | non_neg_integer().
|
||||
-type direction() :: forward | backward.
|
||||
-type range() :: {event_cursor(), limit(), direction()}.
|
||||
-type signal(T) :: {init, args(T)} | {repair, args(T)} | timeout.
|
||||
-type machine(E, A) :: #{
|
||||
namespace := namespace(),
|
||||
id := id(),
|
||||
history := history(E),
|
||||
aux_state := aux_state(A)
|
||||
-type event_cursor() :: undefined | event_id().
|
||||
-type limit() :: undefined | non_neg_integer().
|
||||
-type direction() :: forward | backward.
|
||||
-type range() :: {event_cursor(), limit(), direction()}.
|
||||
-type signal(T) :: {init, args(T)} | {repair, args(T)} | timeout.
|
||||
-type machine(E, A) :: #{
|
||||
namespace := namespace(),
|
||||
id := id(),
|
||||
history := history(E),
|
||||
aux_state := aux_state(A)
|
||||
%% TODO
|
||||
%% history_range ?
|
||||
%% timer ?
|
||||
@ -57,13 +57,19 @@
|
||||
-type modopts(O) :: module() | {module(), O}.
|
||||
|
||||
%% handler
|
||||
-type handler_opts(T) :: T. %% provided to logic handler from machinery backend
|
||||
-type handler_args(T) :: args(T). %% provided to logic handler from handler server spec
|
||||
-type logic_handler(A) :: modopts(handler_args(A)).
|
||||
|
||||
%% provided to logic handler from machinery backend
|
||||
-type handler_opts(T) :: T.
|
||||
%% provided to logic handler from handler server spec
|
||||
-type handler_args(T) :: args(T).
|
||||
-type logic_handler(A) :: modopts(handler_args(A)).
|
||||
|
||||
%% client
|
||||
-type backend_opts(T) :: T. %% opts for client backend
|
||||
-type backend(O) :: modopts(backend_opts(O)). %% client backend
|
||||
|
||||
%% opts for client backend
|
||||
-type backend_opts(T) :: T.
|
||||
%% client backend
|
||||
-type backend(O) :: modopts(backend_opts(O)).
|
||||
|
||||
-export_type([modopts/1]).
|
||||
-export_type([handler_opts/1]).
|
||||
@ -88,24 +94,24 @@
|
||||
|
||||
%% Behaviour definition
|
||||
-type seconds() :: non_neg_integer().
|
||||
-type timer() ::
|
||||
{timeout, seconds()} |
|
||||
{deadline, timestamp()}.
|
||||
-type timer() ::
|
||||
{timeout, seconds()}
|
||||
| {deadline, timestamp()}.
|
||||
|
||||
-type result(E, A) :: #{
|
||||
events => [event_body(E)],
|
||||
action => action() | [action()],
|
||||
events => [event_body(E)],
|
||||
action => action() | [action()],
|
||||
aux_state => aux_state(A)
|
||||
}.
|
||||
|
||||
-type action() ::
|
||||
{set_timer, timer()} |
|
||||
{set_timer, timer(), range()} |
|
||||
{set_timer, timer(), range(), seconds()} |
|
||||
{tag, tag()} |
|
||||
unset_timer |
|
||||
continue |
|
||||
remove.
|
||||
{set_timer, timer()}
|
||||
| {set_timer, timer(), range()}
|
||||
| {set_timer, timer(), range(), seconds()}
|
||||
| {tag, tag()}
|
||||
| unset_timer
|
||||
| continue
|
||||
| remove.
|
||||
|
||||
-export_type([timer/0]).
|
||||
-export_type([timestamp/0]).
|
||||
@ -113,39 +119,32 @@
|
||||
-export_type([result/2]).
|
||||
-export_type([action/0]).
|
||||
|
||||
-callback init(args(_), machine(E, A), handler_args(_), handler_opts(_)) ->
|
||||
result(E, A).
|
||||
-callback init(args(_), machine(E, A), handler_args(_), handler_opts(_)) -> result(E, A).
|
||||
|
||||
-callback process_repair(args(_), machine(E, A), handler_args(_), handler_opts(_)) ->
|
||||
{ok, {response(_), result(E, A)}} | {error, error(_)}.
|
||||
|
||||
-callback process_timeout(machine(E, A), handler_args(_), handler_opts(_)) ->
|
||||
result(E, A).
|
||||
-callback process_timeout(machine(E, A), handler_args(_), handler_opts(_)) -> result(E, A).
|
||||
|
||||
-callback process_call(args(_), machine(E, A), handler_args(_), handler_opts(_)) ->
|
||||
{response(_), result(E, A)}.
|
||||
-callback process_call(args(_), machine(E, A), handler_args(_), handler_opts(_)) -> {response(_), result(E, A)}.
|
||||
|
||||
%% API
|
||||
|
||||
-spec start(namespace(), id(), args(_), backend(_)) ->
|
||||
ok | {error, exists}.
|
||||
-spec start(namespace(), id(), args(_), backend(_)) -> ok | {error, exists}.
|
||||
start(NS, ID, Args, Backend) ->
|
||||
{Module, Opts} = machinery_utils:get_backend(Backend),
|
||||
machinery_backend:start(Module, NS, ID, Args, Opts).
|
||||
|
||||
-spec call(namespace(), ref(), args(_), backend(_)) ->
|
||||
{ok, response(_)} | {error, notfound}.
|
||||
-spec call(namespace(), ref(), args(_), backend(_)) -> {ok, response(_)} | {error, notfound}.
|
||||
call(NS, Ref, Args, Backend) ->
|
||||
call(NS, Ref, {undefined, undefined, forward}, Args, Backend).
|
||||
|
||||
-spec call(namespace(), ref(), range(), args(_), backend(_)) ->
|
||||
{ok, response(_)} | {error, notfound}.
|
||||
-spec call(namespace(), ref(), range(), args(_), backend(_)) -> {ok, response(_)} | {error, notfound}.
|
||||
call(NS, Ref, Range, Args, Backend) ->
|
||||
{Module, Opts} = machinery_utils:get_backend(Backend),
|
||||
machinery_backend:call(Module, NS, Ref, Range, Args, Opts).
|
||||
|
||||
-spec repair(namespace(), ref(), args(_), backend(_)) ->
|
||||
{ok, response(_)} | {error, notfound | working}.
|
||||
-spec repair(namespace(), ref(), args(_), backend(_)) -> {ok, response(_)} | {error, notfound | working}.
|
||||
repair(NS, Ref, Args, Backend) ->
|
||||
repair(NS, Ref, {undefined, undefined, forward}, Args, Backend).
|
||||
|
||||
@ -155,21 +154,18 @@ repair(NS, Ref, Range, Args, Backend) ->
|
||||
{Module, Opts} = machinery_utils:get_backend(Backend),
|
||||
machinery_backend:repair(Module, NS, Ref, Range, Args, Opts).
|
||||
|
||||
-spec get(namespace(), ref(), backend(_)) ->
|
||||
{ok, machine(_, _)} | {error, notfound}.
|
||||
-spec get(namespace(), ref(), backend(_)) -> {ok, machine(_, _)} | {error, notfound}.
|
||||
get(NS, Ref, Backend) ->
|
||||
get(NS, Ref, {undefined, undefined, forward}, Backend).
|
||||
|
||||
-spec get(namespace(), ref(), range(), backend(_)) ->
|
||||
{ok, machine(_, _)} | {error, notfound}.
|
||||
-spec get(namespace(), ref(), range(), backend(_)) -> {ok, machine(_, _)} | {error, notfound}.
|
||||
get(NS, Ref, Range, Backend) ->
|
||||
{Module, Opts} = machinery_utils:get_backend(Backend),
|
||||
machinery_backend:get(Module, NS, Ref, Range, Opts).
|
||||
|
||||
%% Internal API
|
||||
|
||||
-spec dispatch_signal(signal(_), machine(E, A), logic_handler(_), handler_opts(_)) ->
|
||||
result(E, A).
|
||||
-spec dispatch_signal(signal(_), machine(E, A), logic_handler(_), handler_opts(_)) -> result(E, A).
|
||||
dispatch_signal({init, Args}, Machine, {Handler, HandlerArgs}, Opts) ->
|
||||
Handler:init(Args, Machine, HandlerArgs, Opts);
|
||||
dispatch_signal({repair, Args}, Machine, {Handler, HandlerArgs}, Opts) ->
|
||||
@ -182,8 +178,7 @@ dispatch_signal({repair, Args}, Machine, {Handler, HandlerArgs}, Opts) ->
|
||||
dispatch_signal(timeout, Machine, {Handler, HandlerArgs}, Opts) ->
|
||||
Handler:process_timeout(Machine, HandlerArgs, Opts).
|
||||
|
||||
-spec dispatch_call(args(_), machine(E, A), logic_handler(_), handler_opts(_)) ->
|
||||
{response(_), result(E, A)}.
|
||||
-spec dispatch_call(args(_), machine(E, A), logic_handler(_), handler_opts(_)) -> {response(_), result(E, A)}.
|
||||
dispatch_call(Args, Machine, {Handler, HandlerArgs}, Opts) ->
|
||||
Handler:process_call(Args, Machine, HandlerArgs, Opts).
|
||||
|
||||
|
@ -11,31 +11,27 @@
|
||||
|
||||
%% Behaviour definition
|
||||
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type id() :: machinery:id().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type args() :: machinery:args(_).
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type id() :: machinery:id().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type args() :: machinery:args(_).
|
||||
-type backend_opts() :: machinery:backend_opts(_).
|
||||
|
||||
-callback start(namespace(), id(), args(), backend_opts()) ->
|
||||
ok | {error, exists}.
|
||||
-callback start(namespace(), id(), args(), backend_opts()) -> ok | {error, exists}.
|
||||
|
||||
-callback call(namespace(), id(), range(), args(), backend_opts()) ->
|
||||
{ok, machinery:response(_)} | {error, notfound}.
|
||||
-callback call(namespace(), id(), range(), args(), backend_opts()) -> {ok, machinery:response(_)} | {error, notfound}.
|
||||
|
||||
-callback repair(namespace(), id(), range(), args(), backend_opts()) ->
|
||||
{ok, machinery:response(_)} | {error, {failed, machinery:error(_)} | notfound | working}.
|
||||
|
||||
-callback get(namespace(), id(), range(), backend_opts()) ->
|
||||
{ok, machinery:machine(_, _)} | {error, notfound}.
|
||||
-callback get(namespace(), id(), range(), backend_opts()) -> {ok, machinery:machine(_, _)} | {error, notfound}.
|
||||
|
||||
%% API
|
||||
|
||||
-type backend() :: module().
|
||||
|
||||
-spec start(backend(), namespace(), id(), args(), backend_opts()) ->
|
||||
ok | {error, exists}.
|
||||
-spec start(backend(), namespace(), id(), args(), backend_opts()) -> ok | {error, exists}.
|
||||
start(Backend, Namespace, Id, Args, Opts) ->
|
||||
Backend:start(Namespace, Id, Args, Opts).
|
||||
|
||||
@ -49,7 +45,6 @@ call(Backend, Namespace, Ref, Range, Args, Opts) ->
|
||||
repair(Backend, Namespace, Ref, Range, Args, Opts) ->
|
||||
Backend:repair(Namespace, Ref, Range, Args, Opts).
|
||||
|
||||
-spec get(backend(), namespace(), ref(), range(), backend_opts()) ->
|
||||
{ok, machinery:machine(_, _)} | {error, notfound}.
|
||||
-spec get(backend(), namespace(), ref(), range(), backend_opts()) -> {ok, machinery:machine(_, _)} | {error, notfound}.
|
||||
get(Backend, Namespace, Ref, Range, Opts) ->
|
||||
Backend:get(Namespace, Ref, Range, Opts).
|
||||
|
@ -5,10 +5,10 @@
|
||||
|
||||
%% API
|
||||
|
||||
-type id() :: machinery:id().
|
||||
-type id() :: machinery:id().
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type timer() :: machinery:timer().
|
||||
-type tag() :: binary().
|
||||
-type timer() :: machinery:timer().
|
||||
-type tag() :: binary().
|
||||
|
||||
-export_type([tag/0]).
|
||||
|
||||
@ -28,8 +28,7 @@
|
||||
|
||||
%%
|
||||
|
||||
-spec tag(namespace(), tag(), id(), machinery:backend(_)) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec tag(namespace(), tag(), id(), machinery:backend(_)) -> ok | {error, {set, id()}}.
|
||||
tag(NS, Tag, ID, Backend) ->
|
||||
case machinery:start(construct_namespace(NS), Tag, {tag, ID}, Backend) of
|
||||
ok ->
|
||||
@ -45,8 +44,7 @@ tag(NS, Tag, ID, Backend) ->
|
||||
end
|
||||
end.
|
||||
|
||||
-spec tag_until(namespace(), tag(), id(), timer(), machinery:backend(_)) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec tag_until(namespace(), tag(), id(), timer(), machinery:backend(_)) -> ok | {error, {set, id()}}.
|
||||
tag_until(NS, Tag, ID, Timer, Backend) ->
|
||||
case machinery:start(construct_namespace(NS), Tag, {tag, ID, Timer}, Backend) of
|
||||
ok ->
|
||||
@ -62,8 +60,7 @@ tag_until(NS, Tag, ID, Timer, Backend) ->
|
||||
end
|
||||
end.
|
||||
|
||||
-spec untag(namespace(), tag(), id(), machinery:backend(_)) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec untag(namespace(), tag(), id(), machinery:backend(_)) -> ok | {error, {set, id()}}.
|
||||
untag(NS, Tag, ID, Backend) ->
|
||||
case machinery:call(construct_namespace(NS), Tag, {untag, ID}, Backend) of
|
||||
{ok, ok} ->
|
||||
@ -74,8 +71,7 @@ untag(NS, Tag, ID, Backend) ->
|
||||
ok
|
||||
end.
|
||||
|
||||
-spec get(namespace(), tag(), machinery:backend(_)) ->
|
||||
{ok, id()} | {error, unset}.
|
||||
-spec get(namespace(), tag(), machinery:backend(_)) -> {ok, id()} | {error, unset}.
|
||||
get(NS, Tag, Backend) ->
|
||||
case machinery:get(construct_namespace(NS), Tag, Backend) of
|
||||
{ok, Machine} ->
|
||||
@ -90,17 +86,16 @@ construct_namespace(NS) ->
|
||||
|
||||
%%
|
||||
|
||||
-type machine() :: machinery:machine(ev(), _).
|
||||
-type machine() :: machinery:machine(ev(), _).
|
||||
-type handler_opts() :: machinery:handler_opts(_).
|
||||
-type result() :: machinery:result(ev(), _).
|
||||
-type response() :: machinery:response(
|
||||
-type result() :: machinery:result(ev(), _).
|
||||
-type response() :: machinery:response(
|
||||
ok | {error, id()}
|
||||
).
|
||||
|
||||
-type ev() :: id().
|
||||
|
||||
-spec init({tag, id()}, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init({tag, id()}, machine(), undefined, handler_opts()) -> result().
|
||||
init({tag, ID}, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => [ID]
|
||||
@ -111,15 +106,13 @@ init({tag, ID, Timer}, _Machine, _, _Opts) ->
|
||||
action => [{set_timer, Timer, {undefined, 0, forward}}]
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(#{}, _, _Opts) ->
|
||||
#{
|
||||
action => [remove]
|
||||
}.
|
||||
|
||||
-spec process_call({untag, id()}, machine(), undefined, handler_opts()) ->
|
||||
{response(), result()}.
|
||||
-spec process_call({untag, id()}, machine(), undefined, handler_opts()) -> {response(), result()}.
|
||||
process_call({untag, ID}, Machine, _, _Opts) ->
|
||||
case get_machine_st(Machine) of
|
||||
ID ->
|
||||
@ -130,8 +123,7 @@ process_call({untag, ID}, Machine, _, _Opts) ->
|
||||
{{error, IDWas}, #{}}
|
||||
end.
|
||||
|
||||
-spec process_repair({untag, id()}, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair({untag, id()}, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, repair}).
|
||||
|
||||
|
@ -8,49 +8,44 @@
|
||||
-export([untag/4]).
|
||||
-export([get/3]).
|
||||
|
||||
-type id() :: machinery:id().
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type tag() :: machinery_machine_unique_tag:tag().
|
||||
-type timer() :: machinery:timer().
|
||||
-type id() :: machinery:id().
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type tag() :: machinery_machine_unique_tag:tag().
|
||||
-type timer() :: machinery:timer().
|
||||
|
||||
-type opts() :: #{
|
||||
-type opts() :: #{
|
||||
woody_ctx := woody_context:ctx()
|
||||
}.
|
||||
|
||||
-export_type([opts/0]).
|
||||
|
||||
-define(LOGIC_HANDLER, machinery_machine_unique_tag).
|
||||
|
||||
%% API
|
||||
-spec child_spec(_Id) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(_Id) -> supervisor:child_spec().
|
||||
child_spec(Id) ->
|
||||
machinery_utils:woody_child_spec(Id, get_routes(), config(server_opts)).
|
||||
|
||||
-spec tag(namespace(), tag(), id(), opts()) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec tag(namespace(), tag(), id(), opts()) -> ok | {error, {set, id()}}.
|
||||
tag(NS, Tag, ID, Opts) ->
|
||||
machinery_machine_unique_tag:tag(NS, Tag, ID, get_backend(Opts)).
|
||||
|
||||
-spec tag_until(namespace(), tag(), id(), timer(), opts()) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec tag_until(namespace(), tag(), id(), timer(), opts()) -> ok | {error, {set, id()}}.
|
||||
tag_until(NS, Tag, ID, Timer, Opts) ->
|
||||
machinery_machine_unique_tag:tag_until(NS, Tag, ID, Timer, get_backend(Opts)).
|
||||
|
||||
-spec untag(namespace(), tag(), id(), opts()) ->
|
||||
ok | {error, {set, id()}}.
|
||||
-spec untag(namespace(), tag(), id(), opts()) -> ok | {error, {set, id()}}.
|
||||
untag(NS, Tag, ID, Opts) ->
|
||||
machinery_machine_unique_tag:untag(NS, Tag, ID, get_backend(Opts)).
|
||||
|
||||
-spec get(namespace(), tag(), opts()) ->
|
||||
{ok, id()} | {error, unset}.
|
||||
-spec get(namespace(), tag(), opts()) -> {ok, id()} | {error, unset}.
|
||||
get(NS, Tag, Opts) ->
|
||||
machinery_machine_unique_tag:get(NS, Tag, get_backend(Opts)).
|
||||
|
||||
%%% Internal functions
|
||||
|
||||
%% handler
|
||||
-spec get_routes() ->
|
||||
machinery_utils:woody_routes().
|
||||
-spec get_routes() -> machinery_utils:woody_routes().
|
||||
get_routes() ->
|
||||
machinery_mg_backend:get_routes(
|
||||
[get_handler(config(handler_path))],
|
||||
@ -67,16 +62,14 @@ get_backend_config() ->
|
||||
maps:with([schema], config()).
|
||||
|
||||
%% client
|
||||
-spec get_backend(opts()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(opts()) -> machinery_mg_backend:backend().
|
||||
get_backend(#{woody_ctx := WoodyCtx}) ->
|
||||
machinery_mg_backend:new(WoodyCtx, #{
|
||||
client => get_woody_client(),
|
||||
schema => config(schema)
|
||||
client => get_woody_client(),
|
||||
schema => config(schema)
|
||||
}).
|
||||
|
||||
-spec get_woody_client() ->
|
||||
machinery_mg_client:woody_client().
|
||||
-spec get_woody_client() -> machinery_mg_client:woody_client().
|
||||
get_woody_client() ->
|
||||
maps:with([url, event_handler], config()).
|
||||
|
||||
@ -86,12 +79,12 @@ config(Key) ->
|
||||
|
||||
config() ->
|
||||
#{
|
||||
schema => machinery_mg_schema_generic,
|
||||
url => <<"http://machinegun:8022/v1/automaton">>,
|
||||
handler_path => <<"/v1/stateproc">>,
|
||||
schema => machinery_mg_schema_generic,
|
||||
url => <<"http://machinegun:8022/v1/automaton">>,
|
||||
handler_path => <<"/v1/stateproc">>,
|
||||
event_handler => woody_event_handler_default,
|
||||
server_opts => #{
|
||||
ip => {0, 0, 0, 0},
|
||||
port => 8022
|
||||
server_opts => #{
|
||||
ip => {0, 0, 0, 0},
|
||||
port => 8022
|
||||
}
|
||||
}.
|
||||
|
@ -6,20 +6,21 @@
|
||||
%% - There's marshalling scattered around which is common enough for _any_ thrift interface.
|
||||
|
||||
-module(machinery_mg_backend).
|
||||
|
||||
-include_lib("mg_proto/include/mg_proto_state_processing_thrift.hrl").
|
||||
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type ref() :: machinery:ref().
|
||||
-type id() :: machinery:id().
|
||||
-type range() :: machinery:range().
|
||||
-type args(T) :: machinery:args(T).
|
||||
-type response(T) :: machinery:response(T).
|
||||
-type error(T) :: machinery:error(T).
|
||||
-type machine(E, A) :: machinery:machine(E, A).
|
||||
-type logic_handler(T) :: machinery:logic_handler(T).
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type ref() :: machinery:ref().
|
||||
-type id() :: machinery:id().
|
||||
-type range() :: machinery:range().
|
||||
-type args(T) :: machinery:args(T).
|
||||
-type response(T) :: machinery:response(T).
|
||||
-type error(T) :: machinery:error(T).
|
||||
-type machine(E, A) :: machinery:machine(E, A).
|
||||
-type logic_handler(T) :: machinery:logic_handler(T).
|
||||
|
||||
-define(BACKEND_CORE_OPTS,
|
||||
schema := machinery_mg_schema:schema()
|
||||
schema := machinery_mg_schema:schema()
|
||||
).
|
||||
|
||||
-define(MICROS_PER_SEC, (1000 * 1000)).
|
||||
@ -30,32 +31,33 @@
|
||||
}.
|
||||
|
||||
-type handler_config() :: #{
|
||||
path := woody:path(),
|
||||
backend_config := backend_config()
|
||||
path := woody:path(),
|
||||
backend_config := backend_config()
|
||||
}.
|
||||
|
||||
-type handler(A) :: {logic_handler(A), handler_config()}. %% handler server spec
|
||||
%% handler server spec
|
||||
-type handler(A) :: {logic_handler(A), handler_config()}.
|
||||
|
||||
-type handler_opts() :: machinery:handler_opts(#{
|
||||
woody_ctx := woody_context:ctx()
|
||||
woody_ctx := woody_context:ctx()
|
||||
}).
|
||||
|
||||
-type backend_handler_opts() :: #{
|
||||
handler := logic_handler(_),
|
||||
handler := logic_handler(_),
|
||||
?BACKEND_CORE_OPTS
|
||||
}.
|
||||
|
||||
%% Client types
|
||||
-type backend_opts() :: machinery:backend_opts(#{
|
||||
woody_ctx := woody_context:ctx(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
woody_ctx := woody_context:ctx(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
?BACKEND_CORE_OPTS
|
||||
}).
|
||||
|
||||
-type backend() :: {?MODULE, backend_opts()}.
|
||||
|
||||
-type backend_opts_static() :: #{
|
||||
client := machinery_mg_client:woody_client(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
?BACKEND_CORE_OPTS
|
||||
}.
|
||||
|
||||
@ -87,28 +89,24 @@
|
||||
|
||||
%% API
|
||||
|
||||
-spec get_routes([handler(_)], machinery_utils:route_opts()) ->
|
||||
machinery_utils:woody_routes().
|
||||
-spec get_routes([handler(_)], machinery_utils:route_opts()) -> machinery_utils:woody_routes().
|
||||
get_routes(Handlers, Opts) ->
|
||||
machinery_utils:get_woody_routes(Handlers, fun get_handler/2, Opts).
|
||||
|
||||
-spec get_handler(handler(_), machinery_utils:route_opts()) ->
|
||||
machinery_utils:woody_handler().
|
||||
-spec get_handler(handler(_), machinery_utils:route_opts()) -> machinery_utils:woody_handler().
|
||||
get_handler({LogicHandler, #{path := Path, backend_config := Config}}, _) ->
|
||||
{Path, {
|
||||
{mg_proto_state_processing_thrift, 'Processor'},
|
||||
{?MODULE, get_backend_handler_opts(LogicHandler, Config)}
|
||||
}}.
|
||||
|
||||
-spec new(woody_context:ctx(), backend_opts_static()) ->
|
||||
backend().
|
||||
-spec new(woody_context:ctx(), backend_opts_static()) -> backend().
|
||||
new(WoodyCtx, Opts = #{client := _, schema := _}) ->
|
||||
{?MODULE, Opts#{woody_ctx => WoodyCtx}}.
|
||||
|
||||
%% Machinery backend
|
||||
|
||||
-spec start(namespace(), id(), args(_), backend_opts()) ->
|
||||
ok | {error, exists}.
|
||||
-spec start(namespace(), id(), args(_), backend_opts()) -> ok | {error, exists}.
|
||||
start(NS, ID, Args, Opts) ->
|
||||
Client = get_client(Opts),
|
||||
Schema = get_schema(Opts),
|
||||
@ -125,8 +123,7 @@ start(NS, ID, Args, Opts) ->
|
||||
error({failed, NS, ID})
|
||||
end.
|
||||
|
||||
-spec call(namespace(), ref(), range(), args(_), backend_opts()) ->
|
||||
{ok, response(_)} | {error, notfound}.
|
||||
-spec call(namespace(), ref(), range(), args(_), backend_opts()) -> {ok, response(_)} | {error, notfound}.
|
||||
call(NS, Ref, Range, Args, Opts) ->
|
||||
Client = get_client(Opts),
|
||||
Schema = get_schema(Opts),
|
||||
@ -169,8 +166,7 @@ repair(NS, Ref, Range, Args, Opts) ->
|
||||
error({failed, NS, Ref})
|
||||
end.
|
||||
|
||||
-spec get(namespace(), ref(), range(), backend_opts()) ->
|
||||
{ok, machine(_, _)} | {error, notfound}.
|
||||
-spec get(namespace(), ref(), range(), backend_opts()) -> {ok, machine(_, _)} | {error, notfound}.
|
||||
get(NS, Ref, Range, Opts) ->
|
||||
Client = get_client(Opts),
|
||||
Schema = get_schema(Opts),
|
||||
@ -238,8 +234,7 @@ handle_function('ProcessRepair', FunctionArgs, WoodyCtx, Opts) ->
|
||||
|
||||
%% Utils
|
||||
|
||||
-spec get_backend_handler_opts(logic_handler(_), backend_config()) ->
|
||||
backend_handler_opts().
|
||||
-spec get_backend_handler_opts(logic_handler(_), backend_config()) -> backend_handler_opts().
|
||||
get_backend_handler_opts(Handler, Config) ->
|
||||
Config#{handler => Handler}.
|
||||
|
||||
@ -262,18 +257,19 @@ dispatch_repair(Args, Machine, Handler, Opts) ->
|
||||
machinery:dispatch_repair(Args, Machine, Handler, Opts).
|
||||
|
||||
handle_result(Result, OrigMachine) ->
|
||||
Result#{aux_state => set_aux_state(
|
||||
maps:get(aux_state, Result, undefined),
|
||||
maps:get(aux_state, OrigMachine, machinery_msgpack:nil())
|
||||
)}.
|
||||
Result#{
|
||||
aux_state => set_aux_state(
|
||||
maps:get(aux_state, Result, undefined),
|
||||
maps:get(aux_state, OrigMachine, machinery_msgpack:nil())
|
||||
)
|
||||
}.
|
||||
|
||||
set_aux_state(undefined, ReceivedState) ->
|
||||
ReceivedState;
|
||||
set_aux_state(NewState, _) ->
|
||||
NewState.
|
||||
|
||||
-spec build_schema_context(namespace(), ref()) ->
|
||||
machinery_mg_schema:context().
|
||||
-spec build_schema_context(namespace(), ref()) -> machinery_mg_schema:context().
|
||||
build_schema_context(NS, Ref) ->
|
||||
#{
|
||||
machine_ns => NS,
|
||||
@ -287,7 +283,6 @@ marshal({signal_result, Schema, Context}, #{} = V) ->
|
||||
change = marshal({state_change, Schema, Context}, V),
|
||||
action = marshal(action, maps:get(action, V, []))
|
||||
};
|
||||
|
||||
marshal({call_result, Schema, Context}, {Response0, #{} = V}) ->
|
||||
% It is expected that schema doesn't want to save anything in the context here.
|
||||
% The main reason for this is the intention to simplify the code.
|
||||
@ -295,26 +290,23 @@ marshal({call_result, Schema, Context}, {Response0, #{} = V}) ->
|
||||
{Response1, Context} = marshal({schema, Schema, {response, call}, Context}, Response0),
|
||||
#mg_stateproc_CallResult{
|
||||
response = Response1,
|
||||
change = marshal({state_change, Schema, Context}, V),
|
||||
action = marshal(action, maps:get(action, V, []))
|
||||
change = marshal({state_change, Schema, Context}, V),
|
||||
action = marshal(action, maps:get(action, V, []))
|
||||
};
|
||||
|
||||
marshal({repair_result, Schema, Context}, {Response0, #{} = V}) ->
|
||||
% It is expected that schema doesn't want to save anything in the context here.
|
||||
{Response1, Context} = marshal({schema, Schema, {response, {repair, success}}, Context}, Response0),
|
||||
#mg_stateproc_RepairResult{
|
||||
response = Response1,
|
||||
change = marshal({state_change, Schema, Context}, V),
|
||||
action = marshal(action, maps:get(action, V, []))
|
||||
change = marshal({state_change, Schema, Context}, V),
|
||||
action = marshal(action, maps:get(action, V, []))
|
||||
};
|
||||
|
||||
marshal({repair_fail, Schema, Context}, Reason) ->
|
||||
% It is expected that schema doesn't want to save anything in the context here.
|
||||
{Reason1, Context} = marshal({schema, Schema, {response, {repair, failure}}, Context}, Reason),
|
||||
#mg_stateproc_RepairFailed{
|
||||
reason = Reason1
|
||||
};
|
||||
|
||||
marshal({state_change, Schema, Context}, #{} = V) ->
|
||||
AuxStateVersion = machinery_mg_schema:get_version(Schema, aux_state),
|
||||
EventVersion = machinery_mg_schema:get_version(Schema, event),
|
||||
@ -322,7 +314,6 @@ marshal({state_change, Schema, Context}, #{} = V) ->
|
||||
events = marshal({list, {new_event_change, EventVersion, Schema, Context}}, maps:get(events, V, [])),
|
||||
aux_state = marshal({aux_state_change, AuxStateVersion, Schema, Context}, maps:get(aux_state, V, undefined))
|
||||
};
|
||||
|
||||
marshal({new_event_change, EventVersion, Schema, Context}, V) ->
|
||||
% It is expected that schema doesn't want to save anything in the context here.
|
||||
{Event, Context} = marshal({schema, Schema, {event, EventVersion}, Context}, V),
|
||||
@ -330,7 +321,6 @@ marshal({new_event_change, EventVersion, Schema, Context}, V) ->
|
||||
data = Event,
|
||||
format_version = EventVersion
|
||||
};
|
||||
|
||||
marshal({aux_state_change, AuxStateVersion, Schema, Context}, V) ->
|
||||
% It is expected that schema doesn't want to save anything in the context here.
|
||||
{AuxState, Context} = marshal({schema, Schema, {aux_state, AuxStateVersion}, Context}, V),
|
||||
@ -338,22 +328,16 @@ marshal({aux_state_change, AuxStateVersion, Schema, Context}, V) ->
|
||||
data = AuxState,
|
||||
format_version = AuxStateVersion
|
||||
};
|
||||
|
||||
marshal(action, V) when is_list(V) ->
|
||||
lists:foldl(fun apply_action/2, #mg_stateproc_ComplexAction{}, V);
|
||||
|
||||
marshal(action, V) ->
|
||||
marshal(action, [V]);
|
||||
|
||||
marshal(timer, {timeout, V}) ->
|
||||
{timeout, marshal(integer, V)};
|
||||
|
||||
marshal(timer, {deadline, V}) ->
|
||||
{deadline, marshal(timestamp, V)};
|
||||
|
||||
marshal({list, T}, V) when is_list(V) ->
|
||||
[marshal(T, E) || E <- V];
|
||||
|
||||
marshal(T, V) ->
|
||||
machinery_mg_codec:marshal(T, V).
|
||||
|
||||
@ -361,39 +345,35 @@ apply_action({set_timer, V}, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
timer = {set_timer, #mg_stateproc_SetTimerAction{timer = marshal(timer, V)}}
|
||||
};
|
||||
|
||||
apply_action({set_timer, T, Range}, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
timer = {set_timer, #mg_stateproc_SetTimerAction{
|
||||
timer = marshal(timer, T),
|
||||
range = marshal(range, Range)
|
||||
}}
|
||||
timer =
|
||||
{set_timer, #mg_stateproc_SetTimerAction{
|
||||
timer = marshal(timer, T),
|
||||
range = marshal(range, Range)
|
||||
}}
|
||||
};
|
||||
|
||||
apply_action({set_timer, T, Range, HandlingTimeout}, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
timer = {set_timer, #mg_stateproc_SetTimerAction{
|
||||
timer = marshal(timer, T),
|
||||
range = marshal(range, Range),
|
||||
timeout = marshal(integer, HandlingTimeout)
|
||||
}}
|
||||
timer =
|
||||
{set_timer, #mg_stateproc_SetTimerAction{
|
||||
timer = marshal(timer, T),
|
||||
range = marshal(range, Range),
|
||||
timeout = marshal(integer, HandlingTimeout)
|
||||
}}
|
||||
};
|
||||
|
||||
apply_action(unset_timer, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
timer = {unset_timer, #mg_stateproc_UnsetTimerAction{}}
|
||||
};
|
||||
|
||||
apply_action(continue, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
timer = {set_timer, #mg_stateproc_SetTimerAction{timer = {timeout, 0}}}
|
||||
};
|
||||
|
||||
apply_action(remove, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
remove = #mg_stateproc_RemoveAction{}
|
||||
};
|
||||
|
||||
apply_action({tag, Tag}, CA) ->
|
||||
CA#mg_stateproc_ComplexAction{
|
||||
tag = #mg_stateproc_TagAction{
|
||||
@ -404,11 +384,11 @@ apply_action({tag, Tag}, CA) ->
|
||||
unmarshal(
|
||||
{machine, Schema},
|
||||
#mg_stateproc_Machine{
|
||||
'ns' = NS,
|
||||
'id' = ID,
|
||||
'history' = History,
|
||||
'ns' = NS,
|
||||
'id' = ID,
|
||||
'history' = History,
|
||||
'history_range' = Range,
|
||||
'aux_state' = #mg_stateproc_Content{format_version = Version, data = AuxState}
|
||||
'aux_state' = #mg_stateproc_Content{format_version = Version, data = AuxState}
|
||||
}
|
||||
) ->
|
||||
ID1 = unmarshal(id, ID),
|
||||
@ -416,24 +396,22 @@ unmarshal(
|
||||
Context0 = build_schema_context(NS1, ID1),
|
||||
{AuxState1, Context1} = unmarshal({schema, Schema, {aux_state, Version}, Context0}, AuxState),
|
||||
Machine = #{
|
||||
ns => ID1,
|
||||
id => NS1,
|
||||
history => unmarshal({history, Schema, Context1}, History),
|
||||
range => unmarshal(range, Range),
|
||||
aux_state => AuxState1
|
||||
ns => ID1,
|
||||
id => NS1,
|
||||
history => unmarshal({history, Schema, Context1}, History),
|
||||
range => unmarshal(range, Range),
|
||||
aux_state => AuxState1
|
||||
},
|
||||
{Machine, Context1};
|
||||
|
||||
unmarshal({history, Schema, Context}, V) ->
|
||||
unmarshal({list, {event, Schema, Context}}, V);
|
||||
|
||||
unmarshal(
|
||||
{event, Schema, Context0},
|
||||
#mg_stateproc_Event{
|
||||
'id' = EventID,
|
||||
'created_at' = CreatedAt0,
|
||||
'id' = EventID,
|
||||
'created_at' = CreatedAt0,
|
||||
'format_version' = Version,
|
||||
'data' = Payload0
|
||||
'data' = Payload0
|
||||
}
|
||||
) ->
|
||||
CreatedAt1 = unmarshal(timestamp, CreatedAt0),
|
||||
@ -445,20 +423,15 @@ unmarshal(
|
||||
CreatedAt1,
|
||||
Payload1
|
||||
};
|
||||
|
||||
unmarshal({signal, Schema, Context0}, {init, #mg_stateproc_InitSignal{arg = Args0}}) ->
|
||||
{Args1, Context1} = unmarshal({schema, Schema, {args, init}, Context0}, Args0),
|
||||
{{init, Args1}, Context1};
|
||||
|
||||
unmarshal({signal, _Schema, Context}, {timeout, #mg_stateproc_TimeoutSignal{}}) ->
|
||||
{timeout, Context};
|
||||
|
||||
unmarshal({signal, Schema, Context0}, {repair, #mg_stateproc_RepairSignal{arg = Args0}}) ->
|
||||
{Args1, Context1} = unmarshal({schema, Schema, {args, repair}, Context0}, Args0),
|
||||
{{repair, Args1}, Context1};
|
||||
|
||||
unmarshal({list, T}, V) when is_list(V) ->
|
||||
[unmarshal(T, E) || E <- V];
|
||||
|
||||
unmarshal(T, V) ->
|
||||
machinery_mg_codec:unmarshal(T, V).
|
||||
|
@ -2,6 +2,7 @@
|
||||
%%% Simplistic machinegun client.
|
||||
|
||||
-module(machinery_mg_client).
|
||||
|
||||
-include_lib("mg_proto/include/mg_proto_state_processing_thrift.hrl").
|
||||
|
||||
%% API
|
||||
@ -15,8 +16,8 @@
|
||||
-export([modernize/2]).
|
||||
|
||||
-type woody_client() :: #{
|
||||
url := woody:url(),
|
||||
event_handler := woody:ev_handler(),
|
||||
url := woody:url(),
|
||||
event_handler := woody:ev_handler(),
|
||||
transport_opts => woody_client_thrift_http_transport:transport_options()
|
||||
}.
|
||||
|
||||
@ -27,79 +28,78 @@
|
||||
|
||||
%%
|
||||
|
||||
-spec new(woody_client(), woody_context:ctx()) ->
|
||||
client().
|
||||
-spec new(woody_client(), woody_context:ctx()) -> client().
|
||||
new(WoodyClient = #{url := _, event_handler := _}, WoodyCtx) ->
|
||||
{WoodyClient, WoodyCtx}.
|
||||
|
||||
%%
|
||||
|
||||
-type namespace() :: mg_proto_base_thrift:'Namespace'().
|
||||
-type id() :: mg_proto_base_thrift:'ID'().
|
||||
-type args() :: mg_proto_state_processing_thrift:'Args'().
|
||||
-type descriptor() :: mg_proto_state_processing_thrift:'MachineDescriptor'().
|
||||
-type call_response() :: mg_proto_state_processing_thrift:'CallResponse'().
|
||||
-type repair_response() :: mg_proto_state_processing_thrift:'RepairResponse'().
|
||||
-type machine() :: mg_proto_state_processing_thrift:'Machine'().
|
||||
-type namespace_not_found() :: mg_proto_state_processing_thrift:'NamespaceNotFound'().
|
||||
-type machine_not_found() :: mg_proto_state_processing_thrift:'MachineNotFound'().
|
||||
-type machine_already_exists() :: mg_proto_state_processing_thrift:'MachineAlreadyExists'().
|
||||
-type namespace() :: mg_proto_base_thrift:'Namespace'().
|
||||
-type id() :: mg_proto_base_thrift:'ID'().
|
||||
-type args() :: mg_proto_state_processing_thrift:'Args'().
|
||||
-type descriptor() :: mg_proto_state_processing_thrift:'MachineDescriptor'().
|
||||
-type call_response() :: mg_proto_state_processing_thrift:'CallResponse'().
|
||||
-type repair_response() :: mg_proto_state_processing_thrift:'RepairResponse'().
|
||||
-type machine() :: mg_proto_state_processing_thrift:'Machine'().
|
||||
-type namespace_not_found() :: mg_proto_state_processing_thrift:'NamespaceNotFound'().
|
||||
-type machine_not_found() :: mg_proto_state_processing_thrift:'MachineNotFound'().
|
||||
-type machine_already_exists() :: mg_proto_state_processing_thrift:'MachineAlreadyExists'().
|
||||
-type machine_already_working() :: mg_proto_state_processing_thrift:'MachineAlreadyWorking'().
|
||||
-type machine_failed() :: mg_proto_state_processing_thrift:'MachineFailed'().
|
||||
-type repair_failed() :: mg_proto_state_processing_thrift:'RepairFailed'().
|
||||
-type machine_failed() :: mg_proto_state_processing_thrift:'MachineFailed'().
|
||||
-type repair_failed() :: mg_proto_state_processing_thrift:'RepairFailed'().
|
||||
|
||||
-type start_errors() ::
|
||||
namespace_not_found() |
|
||||
machine_already_exists() |
|
||||
machine_failed().
|
||||
namespace_not_found()
|
||||
| machine_already_exists()
|
||||
| machine_failed().
|
||||
|
||||
-type call_errors() ::
|
||||
namespace_not_found() |
|
||||
machine_not_found() |
|
||||
machine_failed().
|
||||
namespace_not_found()
|
||||
| machine_not_found()
|
||||
| machine_failed().
|
||||
|
||||
-type repair_errors() ::
|
||||
namespace_not_found() |
|
||||
machine_not_found() |
|
||||
machine_already_working() |
|
||||
machine_failed() |
|
||||
repair_failed().
|
||||
namespace_not_found()
|
||||
| machine_not_found()
|
||||
| machine_already_working()
|
||||
| machine_failed()
|
||||
| repair_failed().
|
||||
|
||||
-type get_errors() ::
|
||||
namespace_not_found() |
|
||||
machine_not_found().
|
||||
namespace_not_found()
|
||||
| machine_not_found().
|
||||
|
||||
-type modernize_errors() ::
|
||||
namespace_not_found() |
|
||||
machine_not_found().
|
||||
namespace_not_found()
|
||||
| machine_not_found().
|
||||
|
||||
-spec start(namespace(), id(), args(), client()) ->
|
||||
{ok, ok} |
|
||||
{exception, start_errors()}.
|
||||
{ok, ok}
|
||||
| {exception, start_errors()}.
|
||||
start(NS, ID, Args, Client) ->
|
||||
issue_call('Start', [NS, ID, Args], Client).
|
||||
|
||||
-spec call(descriptor(), args(), client()) ->
|
||||
{ok, call_response()} |
|
||||
{exception, call_errors()}.
|
||||
{ok, call_response()}
|
||||
| {exception, call_errors()}.
|
||||
call(Descriptor, Args, Client) ->
|
||||
issue_call('Call', [Descriptor, Args], Client).
|
||||
|
||||
-spec repair(descriptor(), args(), client()) ->
|
||||
{ok, repair_response()} |
|
||||
{exception, repair_errors()}.
|
||||
{ok, repair_response()}
|
||||
| {exception, repair_errors()}.
|
||||
repair(Descriptor, Args, Client) ->
|
||||
issue_call('Repair', [Descriptor, Args], Client).
|
||||
|
||||
-spec get_machine(descriptor(), client()) ->
|
||||
{ok, machine()} |
|
||||
{exception, get_errors()}.
|
||||
{ok, machine()}
|
||||
| {exception, get_errors()}.
|
||||
get_machine(Descriptor, Client) ->
|
||||
issue_call('GetMachine', [Descriptor], Client).
|
||||
|
||||
-spec modernize(descriptor(), client()) ->
|
||||
{ok, ok} |
|
||||
{exception, modernize_errors()}.
|
||||
{ok, ok}
|
||||
| {exception, modernize_errors()}.
|
||||
modernize(Descriptor, Client) ->
|
||||
issue_call('Modernize', [Descriptor], Client).
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
-module(machinery_mg_codec).
|
||||
|
||||
-include_lib("mg_proto/include/mg_proto_state_processing_thrift.hrl").
|
||||
|
||||
-export([marshal/2]).
|
||||
@ -16,88 +17,64 @@
|
||||
-type decoded_value() :: decoded_value(any()).
|
||||
-type decoded_value(T) :: T.
|
||||
|
||||
-spec marshal(type(), decoded_value()) ->
|
||||
encoded_value().
|
||||
|
||||
-spec marshal(type(), decoded_value()) -> encoded_value().
|
||||
marshal(id, V) ->
|
||||
marshal(string, V);
|
||||
|
||||
marshal(event_id, V) ->
|
||||
marshal(integer, V);
|
||||
|
||||
marshal(namespace, V) ->
|
||||
marshal(atom, V);
|
||||
|
||||
marshal(tag, V) ->
|
||||
marshal(string, V);
|
||||
|
||||
marshal(ref, V) when is_binary(V) ->
|
||||
{id, marshal(id, V)};
|
||||
|
||||
marshal(ref, {tag, V}) ->
|
||||
{tag, marshal(tag, V)};
|
||||
|
||||
marshal(descriptor, {NS, Ref, Range}) ->
|
||||
#mg_stateproc_MachineDescriptor{
|
||||
'ns' = marshal(namespace, NS),
|
||||
'ref' = marshal(ref, Ref),
|
||||
'range' = marshal(range, Range)
|
||||
'ns' = marshal(namespace, NS),
|
||||
'ref' = marshal(ref, Ref),
|
||||
'range' = marshal(range, Range)
|
||||
};
|
||||
|
||||
marshal(range, {Cursor, Limit, Direction}) ->
|
||||
#mg_stateproc_HistoryRange{
|
||||
'after' = maybe_marshal(event_id, Cursor),
|
||||
'limit' = maybe_marshal(limit, Limit),
|
||||
'after' = maybe_marshal(event_id, Cursor),
|
||||
'limit' = maybe_marshal(limit, Limit),
|
||||
'direction' = marshal(direction, Direction)
|
||||
};
|
||||
|
||||
marshal(limit, V) ->
|
||||
marshal(integer, V);
|
||||
|
||||
marshal(direction, V) ->
|
||||
marshal({enum, [forward, backward]}, V);
|
||||
|
||||
marshal({enum, Choices = [_ | _]} = T, V) when is_atom(V) ->
|
||||
_ = lists:member(V, Choices) orelse erlang:error(badarg, [T, V]),
|
||||
V;
|
||||
|
||||
marshal({schema, Schema, T, Context}, V) ->
|
||||
machinery_mg_schema:marshal(Schema, T, V, Context);
|
||||
|
||||
marshal(timestamp, {DateTime, USec}) ->
|
||||
Ts = genlib_time:daytime_to_unixtime(DateTime) * ?MICROS_PER_SEC + USec,
|
||||
Str = calendar:system_time_to_rfc3339(Ts, [{unit, microsecond}, {offset, "Z"}]),
|
||||
erlang:list_to_binary(Str);
|
||||
|
||||
marshal(atom, V) when is_atom(V) ->
|
||||
atom_to_binary(V, utf8);
|
||||
|
||||
marshal(string, V) when is_binary(V) ->
|
||||
V;
|
||||
|
||||
marshal(integer, V) when is_integer(V) ->
|
||||
V;
|
||||
|
||||
marshal(T, V) ->
|
||||
erlang:error(badarg, [T, V]).
|
||||
|
||||
%%
|
||||
|
||||
-spec unmarshal(type(), encoded_value()) ->
|
||||
decoded_value().
|
||||
|
||||
-spec unmarshal(type(), encoded_value()) -> decoded_value().
|
||||
unmarshal(id, V) ->
|
||||
unmarshal(string, V);
|
||||
|
||||
unmarshal(event_id, V) ->
|
||||
unmarshal(integer, V);
|
||||
|
||||
unmarshal(namespace, V) ->
|
||||
unmarshal(atom, V);
|
||||
|
||||
unmarshal(tag, V) ->
|
||||
unmarshal(string, V);
|
||||
|
||||
%%
|
||||
%% No unmarshalling for the decriptor required by the protocol so far.
|
||||
%%
|
||||
@ -114,9 +91,9 @@ unmarshal(tag, V) ->
|
||||
unmarshal(
|
||||
range,
|
||||
#mg_stateproc_HistoryRange{
|
||||
'after' = Cursor,
|
||||
'limit' = Limit,
|
||||
'direction' = Direction
|
||||
'after' = Cursor,
|
||||
'limit' = Limit,
|
||||
'direction' = Direction
|
||||
}
|
||||
) ->
|
||||
{
|
||||
@ -124,13 +101,10 @@ unmarshal(
|
||||
maybe_unmarshal(limit, Limit),
|
||||
unmarshal(direction, Direction)
|
||||
};
|
||||
|
||||
unmarshal(limit, V) ->
|
||||
unmarshal(integer, V);
|
||||
|
||||
unmarshal(direction, V) ->
|
||||
unmarshal({enum, [forward, backward]}, V);
|
||||
|
||||
unmarshal({enum, Choices = [_ | _]} = T, V) when is_atom(V) ->
|
||||
case lists:member(V, Choices) of
|
||||
true ->
|
||||
@ -138,10 +112,8 @@ unmarshal({enum, Choices = [_ | _]} = T, V) when is_atom(V) ->
|
||||
false ->
|
||||
erlang:error(badarg, [T, V])
|
||||
end;
|
||||
|
||||
unmarshal({schema, Schema, T, Context}, V) ->
|
||||
machinery_mg_schema:unmarshal(Schema, T, V, Context);
|
||||
|
||||
unmarshal(timestamp, V) when is_binary(V) ->
|
||||
ok = assert_is_utc(V),
|
||||
Str = erlang:binary_to_list(V),
|
||||
@ -153,16 +125,12 @@ unmarshal(timestamp, V) when is_binary(V) ->
|
||||
error:Reason ->
|
||||
erlang:error(badarg, [timestamp, V, Reason])
|
||||
end;
|
||||
|
||||
unmarshal(atom, V) when is_binary(V) ->
|
||||
binary_to_existing_atom(V, utf8);
|
||||
|
||||
unmarshal(string, V) when is_binary(V) ->
|
||||
V;
|
||||
|
||||
unmarshal(integer, V) when is_integer(V) ->
|
||||
V;
|
||||
|
||||
unmarshal(T, V) ->
|
||||
erlang:error(badarg, [T, V]).
|
||||
|
||||
@ -178,8 +146,7 @@ maybe_marshal(_Type, undefined) ->
|
||||
maybe_marshal(Type, Value) ->
|
||||
marshal(Type, Value).
|
||||
|
||||
-spec assert_is_utc(binary()) ->
|
||||
ok | no_return().
|
||||
-spec assert_is_utc(binary()) -> ok | no_return().
|
||||
assert_is_utc(Rfc3339) ->
|
||||
Size0 = erlang:byte_size(Rfc3339),
|
||||
Size1 = Size0 - 1,
|
||||
|
@ -3,26 +3,23 @@
|
||||
|
||||
-module(machinery_mg_schema).
|
||||
|
||||
|
||||
%% Behaviour definition
|
||||
|
||||
-type schema() :: module().
|
||||
|
||||
-type t() ::
|
||||
{args,
|
||||
init |
|
||||
repair |
|
||||
init
|
||||
| repair
|
||||
| call}
|
||||
| {response,
|
||||
call
|
||||
} |
|
||||
{response,
|
||||
call |
|
||||
{repair,
|
||||
success |
|
||||
failure
|
||||
}
|
||||
} |
|
||||
{event, Version} |
|
||||
{aux_state, Version}.
|
||||
| {repair,
|
||||
success
|
||||
| failure}}
|
||||
| {event, Version}
|
||||
| {aux_state, Version}.
|
||||
|
||||
-type v(T) ::
|
||||
T.
|
||||
|
||||
@ -36,14 +33,11 @@
|
||||
atom() => term()
|
||||
}.
|
||||
|
||||
-callback marshal(t(), v(_), context()) ->
|
||||
{machinery_msgpack:t(), context()}.
|
||||
-callback marshal(t(), v(_), context()) -> {machinery_msgpack:t(), context()}.
|
||||
|
||||
-callback unmarshal(t(), machinery_msgpack:t(), context()) ->
|
||||
{v(_), context()}.
|
||||
-callback unmarshal(t(), machinery_msgpack:t(), context()) -> {v(_), context()}.
|
||||
|
||||
-callback get_version(vt()) ->
|
||||
version().
|
||||
-callback get_version(vt()) -> version().
|
||||
|
||||
-export_type([schema/0]).
|
||||
-export_type([t/0]).
|
||||
@ -58,17 +52,14 @@
|
||||
-export([unmarshal/4]).
|
||||
-export([get_version/2]).
|
||||
|
||||
-spec marshal(schema(), t(), v(_), context()) ->
|
||||
{machinery_msgpack:t(), context()}.
|
||||
-spec marshal(schema(), t(), v(_), context()) -> {machinery_msgpack:t(), context()}.
|
||||
marshal(Schema, T, V, C) ->
|
||||
Schema:marshal(T, V, C).
|
||||
|
||||
-spec unmarshal(schema(), t(), machinery_msgpack:t(), context()) ->
|
||||
{v(_), context()}.
|
||||
-spec unmarshal(schema(), t(), machinery_msgpack:t(), context()) -> {v(_), context()}.
|
||||
unmarshal(Schema, T, V, C) ->
|
||||
Schema:unmarshal(T, V, C).
|
||||
|
||||
-spec get_version(schema(), vt()) ->
|
||||
version().
|
||||
-spec get_version(schema(), vt()) -> version().
|
||||
get_version(Schema, T) ->
|
||||
Schema:get_version(T).
|
||||
Schema:get_version(T).
|
||||
|
@ -28,34 +28,29 @@
|
||||
-type v(T) :: machinery_mg_schema:v(T).
|
||||
-type context() :: machinery_mg_schema:context().
|
||||
|
||||
-spec marshal(t(), v(eterm()), context()) ->
|
||||
{machinery_msgpack:t(), context()}.
|
||||
-spec marshal(t(), v(eterm()), context()) -> {machinery_msgpack:t(), context()}.
|
||||
marshal(_T, V, C) ->
|
||||
{marshal(V), C}.
|
||||
|
||||
-spec unmarshal(t(), machinery_msgpack:t(), context()) ->
|
||||
{v(eterm()), context()}.
|
||||
-spec unmarshal(t(), machinery_msgpack:t(), context()) -> {v(eterm()), context()}.
|
||||
unmarshal(_T, V, C) ->
|
||||
{unmarshal(V), C}.
|
||||
|
||||
-spec get_version(machinery_mg_schema:vt()) ->
|
||||
machinery_mg_schema:version().
|
||||
|
||||
-spec get_version(machinery_mg_schema:vt()) -> machinery_mg_schema:version().
|
||||
get_version(_) ->
|
||||
undefined.
|
||||
|
||||
%% API
|
||||
|
||||
-type eterm() ::
|
||||
atom() |
|
||||
number() |
|
||||
tuple() |
|
||||
binary() |
|
||||
list() |
|
||||
map().
|
||||
atom()
|
||||
| number()
|
||||
| tuple()
|
||||
| binary()
|
||||
| list()
|
||||
| map().
|
||||
|
||||
-spec marshal(eterm()) ->
|
||||
machinery_msgpack:t().
|
||||
-spec marshal(eterm()) -> machinery_msgpack:t().
|
||||
marshal(undefined) ->
|
||||
nil();
|
||||
marshal(V) when is_boolean(V) ->
|
||||
@ -73,12 +68,11 @@ marshal(V) when is_list(V) ->
|
||||
marshal(V) when is_tuple(V) ->
|
||||
wrap([marshal(tup) | lists:map(fun marshal/1, tuple_to_list(V))]);
|
||||
marshal(V) when is_map(V) ->
|
||||
wrap([marshal(map), wrap(genlib_map:truemap(fun (Ke, Ve) -> {marshal(Ke), marshal(Ve)} end, V))]);
|
||||
wrap([marshal(map), wrap(genlib_map:truemap(fun(Ke, Ve) -> {marshal(Ke), marshal(Ve)} end, V))]);
|
||||
marshal(V) ->
|
||||
erlang:error(badarg, [V]).
|
||||
|
||||
-spec unmarshal(machinery_msgpack:t()) ->
|
||||
eterm().
|
||||
-spec unmarshal(machinery_msgpack:t()) -> eterm().
|
||||
unmarshal(M) ->
|
||||
unmarshal_v(unwrap(M)).
|
||||
|
||||
@ -102,4 +96,4 @@ unmarshal_v(lst, Vs) ->
|
||||
unmarshal_v(tup, Es) ->
|
||||
list_to_tuple(unmarshal_v(lst, Es));
|
||||
unmarshal_v(map, [V]) ->
|
||||
genlib_map:truemap(fun (Ke, Ve) -> {unmarshal(Ke), unmarshal(Ve)} end, unwrap(V)).
|
||||
genlib_map:truemap(fun(Ke, Ve) -> {unmarshal(Ke), unmarshal(Ve)} end, unwrap(V)).
|
||||
|
@ -7,22 +7,20 @@
|
||||
|
||||
% API
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type backend() :: machinery:backend(_).
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type backend() :: machinery:backend(_).
|
||||
|
||||
-export([modernize/3]).
|
||||
-export([modernize/4]).
|
||||
|
||||
%% API
|
||||
|
||||
-spec modernize(namespace(), ref(), backend()) ->
|
||||
ok| {error, notfound}.
|
||||
-spec modernize(namespace(), ref(), backend()) -> ok | {error, notfound}.
|
||||
modernize(NS, Ref, Backend) ->
|
||||
modernize(NS, Ref, {undefined, undefined, forward}, Backend).
|
||||
|
||||
-spec modernize(namespace(), ref(), range(), backend()) ->
|
||||
ok | {error, notfound}.
|
||||
-spec modernize(namespace(), ref(), range(), backend()) -> ok | {error, notfound}.
|
||||
modernize(NS, Ref, Range, Backend) ->
|
||||
{Module, Opts} = machinery_utils:get_backend(Backend),
|
||||
machinery_modernizer_backend:modernize(Module, NS, Ref, Range, Opts).
|
||||
|
@ -9,20 +9,18 @@
|
||||
|
||||
%% Behaviour definition
|
||||
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type id() :: machinery:id().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type id() :: machinery:id().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type backend_opts() :: machinery:backend_opts(_).
|
||||
|
||||
-callback modernize(namespace(), id(), range(), backend_opts()) ->
|
||||
ok | {error, notfound}.
|
||||
-callback modernize(namespace(), id(), range(), backend_opts()) -> ok | {error, notfound}.
|
||||
|
||||
%% API
|
||||
|
||||
-type backend() :: module().
|
||||
|
||||
-spec modernize(backend(), namespace(), ref(), range(), backend_opts()) ->
|
||||
ok | {error, notfound}.
|
||||
-spec modernize(backend(), namespace(), ref(), range(), backend_opts()) -> ok | {error, notfound}.
|
||||
modernize(Backend, Namespace, Ref, Range, Opts) ->
|
||||
Backend:modernize(Namespace, Ref, Range, Opts).
|
||||
|
@ -3,15 +3,16 @@
|
||||
%%%
|
||||
|
||||
-module(machinery_modernizer_mg_backend).
|
||||
|
||||
-include_lib("mg_proto/include/mg_proto_state_processing_thrift.hrl").
|
||||
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type logic_handler(T) :: machinery:logic_handler(T).
|
||||
-type namespace() :: machinery:namespace().
|
||||
-type ref() :: machinery:ref().
|
||||
-type range() :: machinery:range().
|
||||
-type logic_handler(T) :: machinery:logic_handler(T).
|
||||
|
||||
-define(BACKEND_CORE_OPTS,
|
||||
schema := machinery_mg_schema:schema()
|
||||
schema := machinery_mg_schema:schema()
|
||||
).
|
||||
|
||||
%% Server types
|
||||
@ -20,14 +21,15 @@
|
||||
}.
|
||||
|
||||
-type handler_config() :: #{
|
||||
path := woody:path(),
|
||||
backend_config := backend_config()
|
||||
path := woody:path(),
|
||||
backend_config := backend_config()
|
||||
}.
|
||||
|
||||
-type handler( ) :: handler_config(). %% handler server spec
|
||||
%% handler server spec
|
||||
-type handler() :: handler_config().
|
||||
|
||||
-type handler_opts() :: machinery:handler_opts(#{
|
||||
woody_ctx := woody_context:ctx()
|
||||
woody_ctx := woody_context:ctx()
|
||||
}).
|
||||
|
||||
-type backend_handler_opts() :: #{
|
||||
@ -36,15 +38,15 @@
|
||||
|
||||
%% Client types
|
||||
-type backend_opts() :: machinery:backend_opts(#{
|
||||
woody_ctx := woody_context:ctx(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
woody_ctx := woody_context:ctx(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
?BACKEND_CORE_OPTS
|
||||
}).
|
||||
|
||||
-type backend() :: {?MODULE, backend_opts()}.
|
||||
|
||||
-type backend_opts_static() :: #{
|
||||
client := machinery_mg_client:woody_client(),
|
||||
client := machinery_mg_client:woody_client(),
|
||||
?BACKEND_CORE_OPTS
|
||||
}.
|
||||
|
||||
@ -73,28 +75,24 @@
|
||||
|
||||
%% API
|
||||
|
||||
-spec get_routes([handler()], machinery_utils:route_opts()) ->
|
||||
machinery_utils:woody_routes().
|
||||
-spec get_routes([handler()], machinery_utils:route_opts()) -> machinery_utils:woody_routes().
|
||||
get_routes(Handlers, Opts) ->
|
||||
machinery_utils:get_woody_routes(Handlers, fun get_handler/2, Opts).
|
||||
|
||||
-spec get_handler(handler(), machinery_utils:route_opts()) ->
|
||||
machinery_utils:woody_handler().
|
||||
-spec get_handler(handler(), machinery_utils:route_opts()) -> machinery_utils:woody_handler().
|
||||
get_handler(#{path := Path, backend_config := Config}, _) ->
|
||||
{Path, {
|
||||
{mg_proto_state_processing_thrift, 'Modernizer'},
|
||||
{?MODULE, Config}
|
||||
}}.
|
||||
|
||||
-spec new(woody_context:ctx(), backend_opts_static()) ->
|
||||
backend().
|
||||
-spec new(woody_context:ctx(), backend_opts_static()) -> backend().
|
||||
new(WoodyCtx, Opts = #{client := _, schema := _}) ->
|
||||
{?MODULE, Opts#{woody_ctx => WoodyCtx}}.
|
||||
|
||||
%% Machinery backend
|
||||
|
||||
-spec modernize(namespace(), ref(), range(), backend_opts()) ->
|
||||
ok | {error, notfound}.
|
||||
-spec modernize(namespace(), ref(), range(), backend_opts()) -> ok | {error, notfound}.
|
||||
modernize(NS, Ref, Range, Opts) ->
|
||||
Client = get_client(Opts),
|
||||
Descriptor = {NS, Ref, Range},
|
||||
@ -119,7 +117,6 @@ handle_function('ModernizeEvent', [MachineEvent0], _WoodyCtx, #{schema := Schema
|
||||
|
||||
%% Utils
|
||||
|
||||
|
||||
unmarshal_machine_event(Schema, #mg_stateproc_MachineEvent{
|
||||
ns = MachineNS,
|
||||
id = MachineID,
|
||||
@ -134,7 +131,7 @@ marshal_event_content(Schema, Version, Context0, _Event = #{data := EventData0})
|
||||
{EventData1, Context0} = marshal({schema, Schema, {event, Version}, Context0}, EventData0),
|
||||
#mg_stateproc_Content{
|
||||
format_version = maybe_marshal(format_version, Version),
|
||||
data = EventData1
|
||||
data = EventData1
|
||||
}.
|
||||
|
||||
get_client(#{client := Client, woody_ctx := WoodyCtx}) ->
|
||||
@ -152,34 +149,30 @@ marshal(modernize_event_result, Content) ->
|
||||
#mg_stateproc_ModernizeEventResult{
|
||||
event_payload = Content
|
||||
};
|
||||
|
||||
marshal(format_version, V) ->
|
||||
marshal(integer, V);
|
||||
|
||||
marshal(T, V) ->
|
||||
machinery_mg_codec:marshal(T, V).
|
||||
|
||||
%% Unmarshalling
|
||||
|
||||
unmarshal({event, Schema, Context0}, #mg_stateproc_Event{
|
||||
id = EventID,
|
||||
created_at = CreatedAt0,
|
||||
id = EventID,
|
||||
created_at = CreatedAt0,
|
||||
format_version = Version,
|
||||
data = EventData0
|
||||
data = EventData0
|
||||
}) ->
|
||||
CreatedAt1 = unmarshal(timestamp, CreatedAt0),
|
||||
Context1 = Context0#{created_at => CreatedAt1},
|
||||
{EventData1, Context1} = unmarshal({schema, Schema, {event, Version}, Context1}, EventData0),
|
||||
#{
|
||||
id => unmarshal(event_id, EventID),
|
||||
created_at => CreatedAt1,
|
||||
id => unmarshal(event_id, EventID),
|
||||
created_at => CreatedAt1,
|
||||
format_version => maybe_unmarshal(format_version, Version),
|
||||
data => EventData1
|
||||
data => EventData1
|
||||
};
|
||||
|
||||
unmarshal(format_version, V) ->
|
||||
unmarshal(integer, V);
|
||||
|
||||
unmarshal(T, V) ->
|
||||
machinery_mg_codec:unmarshal(T, V).
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
%%% Msgpack manipulation employed by machinegun interfaces.
|
||||
|
||||
-module(machinery_msgpack).
|
||||
|
||||
-include_lib("mg_proto/include/mg_proto_msgpack_thrift.hrl").
|
||||
|
||||
%% API
|
||||
@ -18,15 +19,16 @@
|
||||
%%
|
||||
|
||||
-spec wrap
|
||||
(nil ) -> t();
|
||||
(boolean() ) -> t();
|
||||
(integer() ) -> t();
|
||||
(float() ) -> t();
|
||||
(binary() ) -> t(); %% string
|
||||
({binary, binary()}) -> t(); %% binary
|
||||
([t()] ) -> t();
|
||||
(#{t() => t()} ) -> t().
|
||||
|
||||
(nil) -> t();
|
||||
(boolean()) -> t();
|
||||
(integer()) -> t();
|
||||
(float()) -> t();
|
||||
%% string
|
||||
(binary()) -> t();
|
||||
%% binary
|
||||
({binary, binary()}) -> t();
|
||||
([t()]) -> t();
|
||||
(#{t() => t()}) -> t().
|
||||
wrap(nil) ->
|
||||
{nl, #mg_msgpack_Nil{}};
|
||||
wrap(V) when is_boolean(V) ->
|
||||
@ -46,15 +48,16 @@ wrap(V) when is_map(V) ->
|
||||
{obj, V}.
|
||||
|
||||
-spec unwrap(t()) ->
|
||||
nil |
|
||||
boolean() |
|
||||
integer() |
|
||||
float() |
|
||||
binary() | %% string
|
||||
{binary, binary()} | %% binary
|
||||
[t()] |
|
||||
#{t() => t()} .
|
||||
|
||||
nil
|
||||
| boolean()
|
||||
| integer()
|
||||
| float()
|
||||
%% string
|
||||
| binary()
|
||||
%% binary
|
||||
| {binary, binary()}
|
||||
| [t()]
|
||||
| #{t() => t()}.
|
||||
unwrap({nl, #mg_msgpack_Nil{}}) ->
|
||||
nil;
|
||||
unwrap({b, V}) when is_boolean(V) ->
|
||||
@ -76,6 +79,5 @@ unwrap({obj, V}) when is_map(V) ->
|
||||
%%
|
||||
|
||||
-spec nil() -> t().
|
||||
|
||||
nil() ->
|
||||
wrap(nil).
|
||||
|
@ -2,20 +2,20 @@
|
||||
|
||||
%% Types
|
||||
|
||||
-type woody_routes() :: [woody_server_thrift_http_handler:route(_)].
|
||||
-type woody_handler() :: woody:http_handler(woody:th_handler()).
|
||||
-type handler(T) :: T.
|
||||
-type woody_routes() :: [woody_server_thrift_http_handler:route(_)].
|
||||
-type woody_handler() :: woody:http_handler(woody:th_handler()).
|
||||
-type handler(T) :: T.
|
||||
-type get_woody_handler() :: fun((handler(_), route_opts()) -> woody_handler()).
|
||||
|
||||
-type woody_server_config() :: #{
|
||||
ip := inet:ip_address(),
|
||||
port := inet:port_number(),
|
||||
protocol_opts => woody_server_thrift_http_handler:protocol_opts(),
|
||||
ip := inet:ip_address(),
|
||||
port := inet:port_number(),
|
||||
protocol_opts => woody_server_thrift_http_handler:protocol_opts(),
|
||||
transport_opts => woody_server_thrift_http_handler:transport_opts()
|
||||
}.
|
||||
|
||||
-type route_opts() :: #{
|
||||
event_handler := woody:ev_handler() | [woody:ev_handler()],
|
||||
event_handler := woody:ev_handler() | [woody:ev_handler()],
|
||||
handler_limits => woody_server_thrift_http_handler:handler_limits()
|
||||
}.
|
||||
|
||||
@ -36,40 +36,30 @@
|
||||
|
||||
%% API
|
||||
|
||||
-spec get_handler(machinery:modopts(Opts)) ->
|
||||
{module(), Opts}.
|
||||
|
||||
-spec get_handler(machinery:modopts(Opts)) -> {module(), Opts}.
|
||||
get_handler(Handler) ->
|
||||
expand_modopts(Handler, undefined).
|
||||
|
||||
-spec get_backend(machinery:backend(Opts)) ->
|
||||
{module(), Opts}.
|
||||
|
||||
-spec get_backend(machinery:backend(Opts)) -> {module(), Opts}.
|
||||
get_backend(Backend) ->
|
||||
expand_modopts(Backend, #{}).
|
||||
|
||||
-spec expand_modopts(machinery:modopts(Opts), Opts) ->
|
||||
{module(), Opts}.
|
||||
|
||||
-spec expand_modopts(machinery:modopts(Opts), Opts) -> {module(), Opts}.
|
||||
expand_modopts({Mod, Opts}, _) ->
|
||||
{Mod, Opts};
|
||||
expand_modopts(Mod, Opts) ->
|
||||
{Mod, Opts}.
|
||||
|
||||
-spec woody_child_spec(_Id, woody_routes(), woody_server_config()) ->
|
||||
supervisor:child_spec().
|
||||
|
||||
-spec woody_child_spec(_Id, woody_routes(), woody_server_config()) -> supervisor:child_spec().
|
||||
woody_child_spec(Id, Routes, Config) ->
|
||||
woody_server:child_spec(Id, Config#{
|
||||
%% ev handler for `handlers`, which is `[]`, so this is just to satisfy the spec.
|
||||
event_handler => woody_event_handler_default,
|
||||
handlers => [],
|
||||
event_handler => woody_event_handler_default,
|
||||
handlers => [],
|
||||
additional_routes => Routes
|
||||
}).
|
||||
|
||||
-spec get_woody_routes([handler(_)], get_woody_handler(), route_opts()) ->
|
||||
woody_routes().
|
||||
|
||||
-spec get_woody_routes([handler(_)], get_woody_handler(), route_opts()) -> woody_routes().
|
||||
get_woody_routes(Handlers, GetHandler, Opts = #{event_handler := _}) ->
|
||||
woody_server_thrift_http_handler:get_routes(Opts#{
|
||||
handlers => [GetHandler(H, Opts) || H <- Handlers]
|
||||
|
@ -26,24 +26,22 @@
|
||||
%%
|
||||
|
||||
-spec cfg(atom(), config()) -> term().
|
||||
|
||||
cfg(Key, Config) ->
|
||||
case lists:keyfind(Key, 1, Config) of
|
||||
{Key, V} -> V;
|
||||
_ -> error({undefined, Key, Config})
|
||||
_ -> error({undefined, Key, Config})
|
||||
end.
|
||||
|
||||
%%
|
||||
|
||||
-type app_name() :: atom().
|
||||
-type app_env() :: [{atom(), term()}].
|
||||
-type app_name() :: atom().
|
||||
-type app_env() :: [{atom(), term()}].
|
||||
-type startup_ctx() :: #{atom() => _}.
|
||||
|
||||
-spec start_apps([app_name()]) -> {[Started :: app_name()], startup_ctx()}.
|
||||
|
||||
start_apps(AppNames) ->
|
||||
lists:foldl(
|
||||
fun (AppName, {SAcc, CtxAcc}) ->
|
||||
fun(AppName, {SAcc, CtxAcc}) ->
|
||||
{Started, Ctx} = start_app(AppName),
|
||||
{SAcc ++ Started, maps:merge(CtxAcc, Ctx)}
|
||||
end,
|
||||
@ -52,22 +50,18 @@ start_apps(AppNames) ->
|
||||
).
|
||||
|
||||
-spec start_app(app_name()) -> {[Started :: app_name()], startup_ctx()}.
|
||||
|
||||
start_app(scoper = AppName) ->
|
||||
{start_app_with(AppName, [
|
||||
{storage, scoper_storage_logger}
|
||||
]), #{}};
|
||||
|
||||
{storage, scoper_storage_logger}
|
||||
]), #{}};
|
||||
start_app(woody = AppName) ->
|
||||
{start_app_with(AppName, [
|
||||
{acceptors_pool_size, 4}
|
||||
]), #{}};
|
||||
|
||||
{acceptors_pool_size, 4}
|
||||
]), #{}};
|
||||
start_app(AppName) ->
|
||||
{start_app_with(AppName, []), #{}}.
|
||||
|
||||
-spec start_app_with(app_name(), app_env()) -> {[app_name()], #{atom() => _}}.
|
||||
|
||||
start_app_with(AppName, Env) ->
|
||||
_ = application:load(AppName),
|
||||
_ = set_app_env(AppName, Env),
|
||||
@ -80,19 +74,17 @@ start_app_with(AppName, Env) ->
|
||||
|
||||
set_app_env(AppName, Env) ->
|
||||
lists:foreach(
|
||||
fun ({K, V}) ->
|
||||
fun({K, V}) ->
|
||||
ok = application:set_env(AppName, K, V)
|
||||
end,
|
||||
Env
|
||||
).
|
||||
|
||||
-spec stop_apps([app_name()]) -> ok.
|
||||
|
||||
stop_apps(AppNames) ->
|
||||
lists:foreach(fun stop_app/1, lists:reverse(AppNames)).
|
||||
|
||||
-spec stop_app(app_name()) -> ok.
|
||||
|
||||
stop_app(AppName) ->
|
||||
case application:stop(AppName) of
|
||||
ok ->
|
||||
@ -111,14 +103,12 @@ stop_app(AppName) ->
|
||||
-type config_mut_fun() :: fun((config()) -> config()).
|
||||
|
||||
-spec makeup_cfg([config_mut_fun()], config()) -> config().
|
||||
|
||||
makeup_cfg(CMFs, C0) ->
|
||||
lists:foldl(fun (CMF, C) -> CMF(C) end, C0, CMFs).
|
||||
lists:foldl(fun(CMF, C) -> CMF(C) end, C0, CMFs).
|
||||
|
||||
-spec woody_ctx() -> config_mut_fun().
|
||||
|
||||
woody_ctx() ->
|
||||
fun (C) -> [{'$woody_ctx', construct_woody_ctx(C)} | C] end.
|
||||
fun(C) -> [{'$woody_ctx', construct_woody_ctx(C)} | C] end.
|
||||
|
||||
construct_woody_ctx(C) ->
|
||||
woody_context:new(construct_rpc_id(get_test_case_name(C))).
|
||||
@ -131,18 +121,15 @@ construct_rpc_id(TestCaseName) ->
|
||||
).
|
||||
|
||||
-spec get_woody_ctx(config()) -> woody_context:ctx().
|
||||
|
||||
get_woody_ctx(C) ->
|
||||
cfg('$woody_ctx', C).
|
||||
|
||||
%%
|
||||
|
||||
-spec test_case_name(test_case_name()) -> config_mut_fun().
|
||||
|
||||
test_case_name(TestCaseName) ->
|
||||
fun (C) -> [{'$test_case_name', TestCaseName} | C] end.
|
||||
fun(C) -> [{'$test_case_name', TestCaseName} | C] end.
|
||||
|
||||
-spec get_test_case_name(config()) -> test_case_name().
|
||||
|
||||
get_test_case_name(C) ->
|
||||
cfg('$test_case_name', C).
|
||||
|
@ -6,27 +6,24 @@
|
||||
%%
|
||||
|
||||
-behaviour(supervisor).
|
||||
|
||||
-export([init/1]).
|
||||
|
||||
%%
|
||||
|
||||
-spec start() -> pid().
|
||||
|
||||
start() ->
|
||||
{ok, PID} = supervisor:start_link(?MODULE, []),
|
||||
true = unlink(PID),
|
||||
PID.
|
||||
|
||||
-spec stop(pid()) -> ok.
|
||||
|
||||
stop(PID) ->
|
||||
true = exit(PID, kill),
|
||||
ok.
|
||||
|
||||
%%
|
||||
|
||||
-spec init([]) ->
|
||||
{ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
|
||||
|
||||
-spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
|
||||
init([]) ->
|
||||
{ok, {#{strategy => one_for_all, intensity => 1, period => 1}, []}}.
|
||||
|
@ -43,8 +43,7 @@ all() ->
|
||||
{group, machinery_mg_backend}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{machinery_mg_backend, [], [{group, all}]},
|
||||
@ -61,7 +60,7 @@ groups() ->
|
||||
-spec init_per_suite(config()) -> config().
|
||||
init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
[{started_apps, StartedApps}| C].
|
||||
[{started_apps, StartedApps} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -134,20 +133,17 @@ remove_call_test(C) ->
|
||||
-type result() :: machinery:result(event(), aux_st()).
|
||||
-type response() :: machinery:response(_).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(init_numbers, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => lists:seq(1, 100)
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> no_return().
|
||||
process_timeout(#{}, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_timeout}).
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
{response(), result()}.
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> {response(), result()}.
|
||||
process_call(do_something, _Machine, _, _Opts) ->
|
||||
{done, #{
|
||||
events => [1, yet_another_event],
|
||||
@ -161,8 +157,7 @@ process_call(remove, _Machine, _, _Opts) ->
|
||||
process_call(fail, _Machine, _, _Opts) ->
|
||||
erlang:error(fail).
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_repair}).
|
||||
|
||||
@ -189,13 +184,11 @@ start_backend(C) ->
|
||||
child_spec(C)
|
||||
).
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
BackendConfig = #{
|
||||
path => <<"/v1/stateproc">>,
|
||||
@ -214,13 +207,11 @@ child_spec(machinery_mg_backend, _C) ->
|
||||
},
|
||||
machinery_utils:woody_child_spec(machinery_mg_backend, Routes, ServerConfig).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(atom(), config()) -> machinery_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
@ -26,26 +26,24 @@
|
||||
|
||||
%%
|
||||
|
||||
-type config() :: ct_helper:config().
|
||||
-type config() :: ct_helper:config().
|
||||
-type test_case_name() :: ct_helper:test_case_name().
|
||||
-type group_name() :: ct_helper:group_name().
|
||||
-type test_return() :: _ | no_return().
|
||||
-type group_name() :: ct_helper:group_name().
|
||||
-type test_return() :: _ | no_return().
|
||||
|
||||
-spec all() -> [test_case_name() | {group, group_name()}].
|
||||
|
||||
all() ->
|
||||
[
|
||||
tag_success ,
|
||||
tag_twice_success ,
|
||||
single_tag_set_only ,
|
||||
untag_success ,
|
||||
conflict_untag_failure ,
|
||||
reset_tag_success ,
|
||||
tag_success,
|
||||
tag_twice_success,
|
||||
single_tag_set_only,
|
||||
untag_success,
|
||||
conflict_untag_failure,
|
||||
reset_tag_success,
|
||||
tag_unset_timely
|
||||
].
|
||||
|
||||
-spec init_per_suite(config()) -> config().
|
||||
|
||||
init_per_suite(C) ->
|
||||
% _ = dbg:tracer(),
|
||||
% _ = dbg:p(all, c),
|
||||
@ -53,9 +51,10 @@ init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = start_apps([machinery]),
|
||||
SuiteSup = ct_sup:start(),
|
||||
start_woody_server([
|
||||
{started_apps , StartedApps},
|
||||
{suite_sup , SuiteSup}
|
||||
| C]).
|
||||
{started_apps, StartedApps},
|
||||
{suite_sup, SuiteSup}
|
||||
| C
|
||||
]).
|
||||
|
||||
start_woody_server(C) ->
|
||||
{ok, PID} = supervisor:start_child(
|
||||
@ -65,21 +64,18 @@ start_woody_server(C) ->
|
||||
[{payproc_mg_machine_sup, PID} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
|
||||
end_per_suite(C) ->
|
||||
ok = ct_sup:stop(cfg(suite_sup, C)),
|
||||
ok = ct_helper:stop_apps(cfg(started_apps, C)),
|
||||
ok.
|
||||
|
||||
-spec init_per_testcase(test_case_name(), config()) -> config().
|
||||
|
||||
init_per_testcase(TestCaseName, C) ->
|
||||
ct_helper:makeup_cfg([ct_helper:test_case_name(TestCaseName), ct_helper:woody_ctx()], C).
|
||||
|
||||
%%
|
||||
|
||||
-spec tag_success(config()) -> test_return().
|
||||
|
||||
tag_success(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID = pid_to_binary(self()),
|
||||
@ -88,7 +84,6 @@ tag_success(C) ->
|
||||
{ok, ID} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts).
|
||||
|
||||
-spec tag_twice_success(config()) -> test_return().
|
||||
|
||||
tag_twice_success(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID = pid_to_binary(self()),
|
||||
@ -98,13 +93,12 @@ tag_twice_success(C) ->
|
||||
{ok, ID} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts).
|
||||
|
||||
-spec single_tag_set_only(config()) -> test_return().
|
||||
|
||||
single_tag_set_only(C) ->
|
||||
Tag = genlib:unique(),
|
||||
Opts = #{woody_ctx => get_woody_ctx(C)},
|
||||
IDs = [integer_to_binary(E) || E <- lists:seq(1, 42)],
|
||||
Rs = genlib_pmap:map(
|
||||
fun (ID) ->
|
||||
fun(ID) ->
|
||||
{ID, machinery_machine_unique_tag_mg_example:tag(payproc, Tag, ID, Opts)}
|
||||
end,
|
||||
IDs
|
||||
@ -114,7 +108,6 @@ single_tag_set_only(C) ->
|
||||
IDsLeft = [ID0 || {ID0, {error, {set, ID}}} <- Rs, ID == IDSet].
|
||||
|
||||
-spec untag_success(config()) -> test_return().
|
||||
|
||||
untag_success(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID = pid_to_binary(self()),
|
||||
@ -124,7 +117,6 @@ untag_success(C) ->
|
||||
{error, unset} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts).
|
||||
|
||||
-spec conflict_untag_failure(config()) -> test_return().
|
||||
|
||||
conflict_untag_failure(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID1 = pid_to_binary(self()),
|
||||
@ -136,7 +128,6 @@ conflict_untag_failure(C) ->
|
||||
ok = machinery_machine_unique_tag_mg_example:untag(payproc, Tag, ID2, Opts).
|
||||
|
||||
-spec reset_tag_success(config()) -> test_return().
|
||||
|
||||
reset_tag_success(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID = pid_to_binary(self()),
|
||||
@ -148,14 +139,14 @@ reset_tag_success(C) ->
|
||||
{ok, ID} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts).
|
||||
|
||||
-spec tag_unset_timely(config()) -> test_return().
|
||||
|
||||
tag_unset_timely(C) ->
|
||||
Tag = genlib:unique(),
|
||||
ID = pid_to_binary(self()),
|
||||
Opts = #{woody_ctx => get_woody_ctx(C)},
|
||||
ok = machinery_machine_unique_tag_mg_example:tag_until(payproc, Tag, ID, {timeout, 1}, Opts),
|
||||
{ok, ID} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts),
|
||||
ok = timer:sleep(2 * 1000), % twice as much as needed
|
||||
% twice as much as needed
|
||||
ok = timer:sleep(2 * 1000),
|
||||
{error, unset} = machinery_machine_unique_tag_mg_example:get(payproc, Tag, Opts).
|
||||
|
||||
%%
|
||||
|
@ -45,8 +45,7 @@ all() ->
|
||||
{group, machinery_mg_backend}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{machinery_mg_backend, [], [{group, all}]},
|
||||
@ -59,7 +58,7 @@ groups() ->
|
||||
-spec init_per_suite(config()) -> config().
|
||||
init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
[{started_apps, StartedApps}| C].
|
||||
[{started_apps, StartedApps} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -71,7 +70,8 @@ init_per_group(machinery_mg_backend = Name, C0) ->
|
||||
C1 = [
|
||||
{backend, Name},
|
||||
{modernizer_backend, machinery_modernizer_mg_backend},
|
||||
{group_sup, ct_sup:start()} | C0
|
||||
{group_sup, ct_sup:start()}
|
||||
| C0
|
||||
],
|
||||
{ok, _Pid} = start_backend(C1),
|
||||
C1;
|
||||
@ -123,26 +123,22 @@ skip_upgrading_test(C) ->
|
||||
-type handler_opts() :: machinery:handler_opts(_).
|
||||
-type result() :: machinery:result(event(), aux_st()).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(init_something, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => [init_event],
|
||||
aux_state => #{}
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(_Args, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_timeout}).
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_call(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_call}).
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_repair}).
|
||||
|
||||
@ -168,8 +164,7 @@ unmarshal({event, 1}, V, C) ->
|
||||
unmarshal(T, V, C) ->
|
||||
machinery_mg_schema_generic:unmarshal(T, V, C).
|
||||
|
||||
-spec get_version(machinery_mg_schema:vt()) ->
|
||||
machinery_mg_schema:version().
|
||||
-spec get_version(machinery_mg_schema:vt()) -> machinery_mg_schema:version().
|
||||
get_version(aux_state) ->
|
||||
undefined;
|
||||
get_version(event) ->
|
||||
@ -217,13 +212,11 @@ get_event_version() ->
|
||||
[{event_version, Version}] = ets:lookup(?ETS, event_version),
|
||||
Version.
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
Routes = backend_mg_routes() ++ modernizer_mg_routes(),
|
||||
ServerConfig = #{
|
||||
@ -257,19 +250,17 @@ modernizer_mg_routes() ->
|
||||
#{event_handler => woody_event_handler_default}
|
||||
).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_modernizer_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_modernizer_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_modernizer_backend(C) ->
|
||||
get_backend(?config(modernizer_backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend() |
|
||||
machinery_modernizer_mg_backend:backend().
|
||||
machinery_mg_backend:backend()
|
||||
| machinery_modernizer_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
@ -42,8 +42,7 @@ all() ->
|
||||
{group, all}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{all, [parallel], [
|
||||
@ -56,7 +55,7 @@ init_per_suite(C0) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
C1 = [{backend, machinery_mg_backend}, {group_sup, ct_sup:start()} | C0],
|
||||
{ok, _Pid} = start_backend(C1),
|
||||
[{started_apps, StartedApps}| C1].
|
||||
[{started_apps, StartedApps} | C1].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -89,8 +88,7 @@ schema_versions_used_properly_test(C) ->
|
||||
-type result() :: machinery:result(event(), aux_st()).
|
||||
-type response() :: machinery:response(_).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(init_something, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => [init_event],
|
||||
@ -98,17 +96,16 @@ init(init_something, _Machine, _, _Opts) ->
|
||||
aux_state => #{some => <<"complex">>, aux_state => 1}
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(_Machine, _, _Opts) ->
|
||||
#{
|
||||
events => [{timeout_event, 1}],
|
||||
action => unset_timer, % why not
|
||||
% why not
|
||||
action => unset_timer,
|
||||
aux_state => #{some => <<"other complex">>, aux_state => 1}
|
||||
}.
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
{response(), result()}.
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> {response(), result()}.
|
||||
process_call(do_something, _Machine, _, _Opts) ->
|
||||
{done, #{
|
||||
events => [call_event],
|
||||
@ -117,8 +114,7 @@ process_call(do_something, _Machine, _, _Opts) ->
|
||||
process_call(fail, _Machine, _, _Opts) ->
|
||||
erlang:error(fail).
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(repair_something, #{history := History}, _, _Opts) ->
|
||||
{ok, {done, #{events => [{count_events, erlang:length(History)}]}}}.
|
||||
|
||||
@ -128,13 +124,13 @@ process_repair(repair_something, #{history := History}, _, _Opts) ->
|
||||
{machinery_msgpack:t(), machinery_mg_schema:context()}.
|
||||
marshal(T, V, C) when
|
||||
T =:= {aux_state, 1} orelse
|
||||
T =:= {event, 2} orelse
|
||||
T =:= {args, init} orelse
|
||||
T =:= {args, call} orelse
|
||||
T =:= {args, repair} orelse
|
||||
T =:= {response, call} orelse
|
||||
T =:= {response, {repair, success}} orelse
|
||||
T =:= {response, {repair, failure}}
|
||||
T =:= {event, 2} orelse
|
||||
T =:= {args, init} orelse
|
||||
T =:= {args, call} orelse
|
||||
T =:= {args, repair} orelse
|
||||
T =:= {response, call} orelse
|
||||
T =:= {response, {repair, success}} orelse
|
||||
T =:= {response, {repair, failure}}
|
||||
->
|
||||
{{bin, erlang:term_to_binary(V)}, process_context(T, C)}.
|
||||
|
||||
@ -142,12 +138,12 @@ marshal(T, V, C) when
|
||||
{any(), machinery_mg_schema:context()}.
|
||||
unmarshal(T, V, C) when
|
||||
T =:= {aux_state, 1} orelse
|
||||
T =:= {event, 2} orelse
|
||||
T =:= {args, init} orelse
|
||||
T =:= {args, call} orelse
|
||||
T =:= {args, repair} orelse
|
||||
T =:= {response, call} orelse
|
||||
T =:= {response, {repair, failure}}
|
||||
T =:= {event, 2} orelse
|
||||
T =:= {args, init} orelse
|
||||
T =:= {args, call} orelse
|
||||
T =:= {args, repair} orelse
|
||||
T =:= {response, call} orelse
|
||||
T =:= {response, {repair, failure}}
|
||||
->
|
||||
{bin, EncodedV} = V,
|
||||
{erlang:binary_to_term(EncodedV), process_context(T, C)};
|
||||
@ -158,8 +154,7 @@ unmarshal({response, {repair, success}} = T, {bin, <<"ok">>}, C) ->
|
||||
% mg repair migration artefact
|
||||
{done, process_context(T, C)}.
|
||||
|
||||
-spec get_version(machinery_mg_schema:vt()) ->
|
||||
machinery_mg_schema:version().
|
||||
-spec get_version(machinery_mg_schema:vt()) -> machinery_mg_schema:version().
|
||||
get_version(aux_state) ->
|
||||
1;
|
||||
get_version(event) ->
|
||||
@ -213,13 +208,11 @@ start_backend(C) ->
|
||||
child_spec(C)
|
||||
).
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
BackendConfig = #{
|
||||
path => <<"/v1/stateproc">>,
|
||||
@ -238,13 +231,11 @@ child_spec(machinery_mg_backend, _C) ->
|
||||
},
|
||||
machinery_utils:woody_child_spec(machinery_mg_backend, Routes, ServerConfig).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(atom(), config()) -> machinery_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
@ -45,8 +45,7 @@ all() ->
|
||||
{group, machinery_mg_backend}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{machinery_mg_backend, [], [{group, all}]},
|
||||
@ -65,7 +64,7 @@ groups() ->
|
||||
-spec init_per_suite(config()) -> config().
|
||||
init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
[{started_apps, StartedApps}| C].
|
||||
[{started_apps, StartedApps} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -159,28 +158,24 @@ working_repair_test(C) ->
|
||||
-type response() :: machinery:response(_).
|
||||
-type error() :: machinery:error(_).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(init_numbers, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => lists:seq(1, 100)
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(#{}, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_timeout}).
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
{response(), result()}.
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> {response(), result()}.
|
||||
process_call(get_events, #{history := History}, _, _Opts) ->
|
||||
Bodies = lists:map(fun({_ID, _CreatedAt, Body}) -> Body end, History),
|
||||
{Bodies, #{}};
|
||||
process_call(fail, _Machine, _, _Opts) ->
|
||||
erlang:error(fail).
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
{ok, {response(), result()}} | {error, error()}.
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> {ok, {response(), result()}} | {error, error()}.
|
||||
process_repair(simple, _Machine, _, _Opts) ->
|
||||
{ok, {done, #{}}};
|
||||
process_repair({add_events, Events}, _Machine, _, _Opts) ->
|
||||
@ -218,13 +213,11 @@ start_backend(C) ->
|
||||
child_spec(C)
|
||||
).
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
BackendConfig = #{
|
||||
path => <<"/v1/stateproc">>,
|
||||
@ -243,13 +236,11 @@ child_spec(machinery_mg_backend, _C) ->
|
||||
},
|
||||
machinery_utils:woody_child_spec(machinery_mg_backend, Routes, ServerConfig).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(atom(), config()) -> machinery_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
@ -41,8 +41,7 @@ all() ->
|
||||
{group, machinery_mg_backend}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{machinery_mg_backend, [], [{group, all}]},
|
||||
@ -57,7 +56,7 @@ groups() ->
|
||||
-spec init_per_suite(config()) -> config().
|
||||
init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
[{started_apps, StartedApps}| C].
|
||||
[{started_apps, StartedApps} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -117,8 +116,7 @@ failed_start_test(C) ->
|
||||
-type handler_opts() :: machinery:handler_opts(_).
|
||||
-type result() :: machinery:result(event(), aux_st()).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(init_something, _Machine, _, _Opts) ->
|
||||
#{
|
||||
events => [init_event],
|
||||
@ -127,18 +125,15 @@ init(init_something, _Machine, _, _Opts) ->
|
||||
init(fail, _Machine, _, _Opts) ->
|
||||
erlang:error(fail).
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(#{}, _, _Opts) ->
|
||||
#{}.
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_call(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_call}).
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_repair}).
|
||||
|
||||
@ -159,13 +154,11 @@ start_backend(C) ->
|
||||
child_spec(C)
|
||||
).
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
BackendConfig = #{
|
||||
path => <<"/v1/stateproc">>,
|
||||
@ -184,13 +177,11 @@ child_spec(machinery_mg_backend, _C) ->
|
||||
},
|
||||
machinery_utils:woody_child_spec(machinery_mg_backend, Routes, ServerConfig).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(atom(), config()) -> machinery_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
@ -43,8 +43,7 @@ all() ->
|
||||
{group, machinery_mg_backend}
|
||||
].
|
||||
|
||||
-spec groups() ->
|
||||
[{group_name(), list(), test_case_name()}].
|
||||
-spec groups() -> [{group_name(), list(), test_case_name()}].
|
||||
groups() ->
|
||||
[
|
||||
{machinery_mg_backend, [], [{group, all}]},
|
||||
@ -61,7 +60,7 @@ groups() ->
|
||||
-spec init_per_suite(config()) -> config().
|
||||
init_per_suite(C) ->
|
||||
{StartedApps, _StartupCtx} = ct_helper:start_apps([machinery]),
|
||||
[{started_apps, StartedApps}| C].
|
||||
[{started_apps, StartedApps} | C].
|
||||
|
||||
-spec end_per_suite(config()) -> _.
|
||||
end_per_suite(C) ->
|
||||
@ -149,8 +148,7 @@ call_with_ranged_timer_test(C) ->
|
||||
-type result() :: machinery:result(event(), aux_st()).
|
||||
-type response() :: machinery:response(_).
|
||||
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec init(_Args, machine(), undefined, handler_opts()) -> result().
|
||||
init(nop, _Machine, _, _Opts) ->
|
||||
#{};
|
||||
init(init_timer, _Machine, _, _Opts) ->
|
||||
@ -169,18 +167,17 @@ init(init_timer_with_range, _Machine, _, _Opts) ->
|
||||
action => {set_timer, {deadline, {{{1990, 01, 01}, {0, 0, 0}}, 0}}, {1, 2, forward}, 15}
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) ->
|
||||
result().
|
||||
-spec process_timeout(machine(), undefined, handler_opts()) -> result().
|
||||
process_timeout(#{history := History}, _, _Opts) ->
|
||||
Bodies = lists:map(fun({_ID, _CreatedAt, Body}) -> Body end, History),
|
||||
#{
|
||||
events => [timer_fired],
|
||||
action => unset_timer, % why not
|
||||
% why not
|
||||
action => unset_timer,
|
||||
aux_state => Bodies
|
||||
}.
|
||||
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) ->
|
||||
{response(), result()}.
|
||||
-spec process_call(_Args, machine(), undefined, handler_opts()) -> {response(), result()}.
|
||||
process_call(timer, _Machine, _, _Opts) ->
|
||||
{done, #{
|
||||
events => lists:seq(1, 10),
|
||||
@ -197,8 +194,7 @@ process_call(timer_with_range, _Machine, _, _Opts) ->
|
||||
action => {set_timer, {deadline, {{{1990, 01, 01}, {0, 0, 0}}, 0}}, {1, 2, forward}, 15}
|
||||
}}.
|
||||
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) ->
|
||||
no_return().
|
||||
-spec process_repair(_Args, machine(), undefined, handler_opts()) -> no_return().
|
||||
process_repair(_Args, _Machine, _, _Opts) ->
|
||||
erlang:error({not_implemented, process_repair}).
|
||||
|
||||
@ -225,13 +221,11 @@ start_backend(C) ->
|
||||
child_spec(C)
|
||||
).
|
||||
|
||||
-spec child_spec(config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(config()) -> supervisor:child_spec().
|
||||
child_spec(C) ->
|
||||
child_spec(?config(backend, C), C).
|
||||
|
||||
-spec child_spec(atom(), config()) ->
|
||||
supervisor:child_spec().
|
||||
-spec child_spec(atom(), config()) -> supervisor:child_spec().
|
||||
child_spec(machinery_mg_backend, _C) ->
|
||||
BackendConfig = #{
|
||||
path => <<"/v1/stateproc">>,
|
||||
@ -250,13 +244,11 @@ child_spec(machinery_mg_backend, _C) ->
|
||||
},
|
||||
machinery_utils:woody_child_spec(machinery_mg_backend, Routes, ServerConfig).
|
||||
|
||||
-spec get_backend(config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(config()) -> machinery_mg_backend:backend().
|
||||
get_backend(C) ->
|
||||
get_backend(?config(backend, C), C).
|
||||
|
||||
-spec get_backend(atom(), config()) ->
|
||||
machinery_mg_backend:backend().
|
||||
-spec get_backend(atom(), config()) -> machinery_mg_backend:backend().
|
||||
get_backend(machinery_mg_backend, C) ->
|
||||
machinery_mg_backend:new(
|
||||
ct_helper:get_woody_ctx(C),
|
||||
|
Loading…
Reference in New Issue
Block a user