From 24eb8aa146043740197904b1392643003593c9a2 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Thu, 30 Jan 2020 15:22:34 +0300 Subject: [PATCH 1/2] Allow to configure app wide woody event handlers --- apps/bender/src/bender.erl | 14 ++++++-------- apps/bender/src/bender_utils.erl | 14 ++++++++++++-- config/sys.config | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/apps/bender/src/bender.erl b/apps/bender/src/bender.erl index fa0bfd5..28d8e2e 100644 --- a/apps/bender/src/bender.erl +++ b/apps/bender/src/bender.erl @@ -53,6 +53,7 @@ stop(_State) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. init([]) -> + EventHandlers = bender_utils:get_woody_event_handlers(), ChildSpec = woody_server:child_spec( ?MODULE, #{ @@ -61,12 +62,9 @@ init([]) -> protocol_opts => get_protocol_opts(), transport_opts => get_transport_opts(), shutdown_timeout => get_shutdown_timeout(), - event_handler => [ - scoper_woody_event_handler, - hay_woody_event_handler - ], + event_handler => EventHandlers, handlers => [get_handler_spec()], - additional_routes => get_routes() + additional_routes => get_routes(EventHandlers) } ), {ok, { @@ -116,12 +114,12 @@ get_handler_spec() -> bender_handler }}. --spec get_routes() -> +-spec get_routes(woody:ev_handlers()) -> [woody_server_thrift_http_handler:route(_)]. -get_routes() -> +get_routes(EventHandlers) -> RouteOptsEnv = genlib_app:env(?MODULE, route_opts, #{}), - RouteOpts = RouteOptsEnv#{event_handler => scoper_woody_event_handler}, + RouteOpts = RouteOptsEnv#{event_handler => EventHandlers}, Generator = genlib_app:env(bender, generator, #{}), Sequence = genlib_app:env(bender, sequence, #{}), Handlers = [ diff --git a/apps/bender/src/bender_utils.erl b/apps/bender/src/bender_utils.erl index 4b982ad..ef34bef 100644 --- a/apps/bender/src/bender_utils.erl +++ b/apps/bender/src/bender_utils.erl @@ -2,6 +2,7 @@ -export([unique_id/0]). -export([get_backend/2]). +-export([get_woody_event_handlers/0]). -type woody_context() :: woody_context:ctx(). @@ -40,9 +41,18 @@ get_backend(Service, WoodyCtx) -> -spec get_woody_client(automaton()) -> machinery_mg_client:woody_client(). -get_woody_client(#{url := Url, event_handler := Handler} = Automaton) -> +get_woody_client(#{url := Url} = Automaton) -> genlib_map:compact(#{ url => Url, - event_handler => Handler, + event_handler => get_woody_event_handlers(), transport_opts => maps:get(transport_opts, Automaton, undefined) }). + +-spec get_woody_event_handlers() -> + woody:ev_handlers(). + +get_woody_event_handlers() -> + genlib_app:env(?MODULE, woody_event_handlers, [ + scoper_woody_event_handler, + hay_woody_event_handler + ]). diff --git a/config/sys.config b/config/sys.config index 90a584b..2c57e53 100644 --- a/config/sys.config +++ b/config/sys.config @@ -8,10 +8,6 @@ path => <<"/v1/stateproc/bender_generator">>, schema => machinery_mg_schema_generic, url => <<"http://machinegun:8022/v1/automaton">>, % mandatory - event_handler => [ - scoper_woody_event_handler, - hay_woody_event_handler - ], transport_opts => #{ max_connections => 1000 } @@ -21,7 +17,6 @@ path => <<"/v1/stateproc/bender_sequence">>, schema => machinery_mg_schema_generic, url => <<"http://machinegun:8022/v1/automaton">>, % mandatory - event_handler => scoper_woody_event_handler, % mandatory transport_opts => #{ max_connections => 1000 } @@ -45,6 +40,18 @@ num_acceptors => 100 % size of acceptors pool, default is 10 }}, + {woody_event_handlers, [ + hay_woody_event_handler, + {scoper_woody_event_handler, #{ + event_handler_opts => #{ + formatter_opts => #{ + max_length => 1000, + max_printable_string_length => 80 + } + } + }} + ]}, + {health_check, #{ disk => {erl_health, disk , ["/", 99]}, memory => {erl_health, cg_memory, [99]}, From 60d0aaca6c8bf0cd22c8be10fd05a126e53e48d4 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Thu, 30 Jan 2020 17:57:10 +0300 Subject: [PATCH 2/2] Avoid reckless ?MODULE usage --- apps/bender/src/bender_utils.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/bender/src/bender_utils.erl b/apps/bender/src/bender_utils.erl index ef34bef..eeea044 100644 --- a/apps/bender/src/bender_utils.erl +++ b/apps/bender/src/bender_utils.erl @@ -52,7 +52,7 @@ get_woody_client(#{url := Url} = Automaton) -> woody:ev_handlers(). get_woody_event_handlers() -> - genlib_app:env(?MODULE, woody_event_handlers, [ + genlib_app:env(bender, woody_event_handlers, [ scoper_woody_event_handler, hay_woody_event_handler ]).