Explorar el Código

refactor: apply review remarks

Thales Macedo Garitezi hace 2 años
padre
commit
89ea40a8b7
Se han modificado 3 ficheros con 44 adiciones y 84 borrados
  1. 6 33
      apps/emqx_machine/src/emqx_machine_boot.erl
  2. 23 26
      mix.exs
  3. 15 25
      rebar.config.erl

+ 6 - 33
apps/emqx_machine/src/emqx_machine_boot.erl

@@ -133,25 +133,11 @@ basic_reboot_apps() ->
     RebootListPath = filename:join([PrivDir, "reboot_lists.eterm"]),
     {ok, [
         #{
-            common_business_apps := CommonBusinessApps0,
+            common_business_apps := CommonBusinessApps,
             ee_business_apps := EEBusinessApps,
             ce_business_apps := CEBusinessApps
         }
     ]} = file:consult(RebootListPath),
-    Filters0 = maps:from_list([
-        {App, is_app(App)}
-     || App <- [quicer, bcrypt, jq, observer]
-    ]),
-    CommonBusinessApps =
-        filter(
-            CommonBusinessApps0,
-            %% We don't need to restart these
-            Filters0#{
-                system_monitor => false,
-                observer => false,
-                quicer => false
-            }
-        ),
     EditionSpecificApps =
         case emqx_release:edition() of
             ee -> EEBusinessApps;
@@ -159,25 +145,12 @@ basic_reboot_apps() ->
             _ -> []
         end,
     BusinessApps = CommonBusinessApps ++ EditionSpecificApps,
-    ?BASIC_REBOOT_APPS ++ BusinessApps.
+    ?BASIC_REBOOT_APPS ++ (BusinessApps -- excluded_apps()).
 
-filter(AppList, Filters) ->
-    lists:foldr(
-        fun(App, Acc) ->
-            AppName =
-                case App of
-                    {Name, _Type} -> Name;
-                    Name when is_atom(Name) -> Name
-                end,
-            ShouldKeep = maps:get(AppName, Filters, true),
-            case ShouldKeep of
-                true -> [App | Acc];
-                false -> Acc
-            end
-        end,
-        [],
-        AppList
-    ).
+excluded_apps() ->
+    OptionalApps = [bcrypt, jq, observer],
+    [system_monitor, observer_cli] ++
+        [App || App <- OptionalApps, not is_app(App)].
 
 is_app(Name) ->
     case application:load(Name) of

+ 23 - 26
mix.exs

@@ -341,16 +341,6 @@ defmodule EMQXUmbrella.MixProject do
        }
      ]} = :file.consult("apps/emqx_machine/priv/reboot_lists.eterm")
 
-    db_apps = filter(db_apps, %{mnesia_rocksdb: enable_rocksdb?()})
-
-    common_business_apps =
-      filter(common_business_apps, %{
-        quicer: enable_quicer?(),
-        bcrypt: enable_bcrypt?(),
-        jq: enable_jq?(),
-        observer: is_app?(:observer)
-      })
-
     edition_specific_apps =
       if edition_type == :enterprise do
         ee_business_apps
@@ -360,24 +350,31 @@ defmodule EMQXUmbrella.MixProject do
 
     business_apps = common_business_apps ++ edition_specific_apps
 
-    Enum.map(system_apps, fn app ->
-      if is_atom(app), do: {app, :permanent}, else: app
-    end) ++
-      Enum.map(db_apps, &{&1, :load}) ++
-      [emqx_machine: :permanent] ++
-      Enum.map(business_apps, &{&1, :load})
-  end
+    excluded_apps = excluded_apps()
 
-  defp filter(apps, filters) do
-    Enum.filter(apps, fn app ->
-      app_name =
-        case app do
-          {app_name, _type} -> app_name
-          app_name when is_atom(app_name) -> app_name
-        end
+    system_apps =
+      Enum.map(system_apps, fn app ->
+        if is_atom(app), do: {app, :permanent}, else: app
+      end)
 
-      Map.get(filters, app_name, true)
-    end)
+    db_apps = Enum.map(db_apps, &{&1, :load})
+    business_apps = Enum.map(business_apps, &{&1, :load})
+
+    [system_apps, db_apps, [emqx_machine: :permanent], business_apps]
+    |> List.flatten()
+    |> Keyword.reject(fn {app, _type} -> app in excluded_apps end)
+  end
+
+  defp excluded_apps() do
+    %{
+      mnesia_rocksdb: enable_rocksdb?(),
+      quicer: enable_quicer?(),
+      bcrypt: enable_bcrypt?(),
+      jq: enable_jq?(),
+      observer: is_app?(:observer)
+    }
+    |> Enum.reject(&elem(&1, 1))
+    |> Enum.map(&elem(&1, 0))
   end
 
   defp is_app?(name) do

+ 15 - 25
rebar.config.erl

@@ -388,45 +388,35 @@ overlay_vars_pkg(pkg) ->
 relx_apps(ReleaseType, Edition) ->
     {ok, [
         #{
-            db_apps := DBApps0,
+            db_apps := DBApps,
             system_apps := SystemApps,
-            common_business_apps := CommonBusinessApps0,
+            common_business_apps := CommonBusinessApps,
             ee_business_apps := EEBusinessApps,
             ce_business_apps := CEBusinessApps
         }
     ]} = file:consult("apps/emqx_machine/priv/reboot_lists.eterm"),
-    DBApps = filter(DBApps0, #{mnesia_rocksdb => is_rocksdb_supported()}),
-    CommonBusinessApps =
-        filter(CommonBusinessApps0, #{
-            quicer => is_quicer_supported(),
-            bcrypt => provide_bcrypt_release(ReleaseType),
-            jq => is_jq_supported(),
-            observer => is_app(observer)
-        }),
     EditionSpecificApps =
         case Edition of
             ee -> EEBusinessApps;
             ce -> CEBusinessApps
         end,
     BusinessApps = CommonBusinessApps ++ EditionSpecificApps,
+    ExcludedApps = excluded_apps(ReleaseType),
     SystemApps ++
         %% EMQX starts the DB and the business applications:
-        [{App, load} || App <- DBApps] ++
+        [{App, load} || App <- (DBApps -- ExcludedApps)] ++
         [emqx_machine] ++
-        [{App, load} || App <- BusinessApps].
-
-filter(AppList, Filters) ->
-    lists:filter(
-        fun(App) ->
-            AppName =
-                case App of
-                    {Name, _Type} -> Name;
-                    Name when is_atom(Name) -> Name
-                end,
-            maps:get(AppName, Filters, true)
-        end,
-        AppList
-    ).
+        [{App, load} || App <- (BusinessApps -- ExcludedApps)].
+
+excluded_apps(ReleaseType) ->
+    OptionalApps = [
+        {quicer, is_quicer_supported()},
+        {bcrypt, provide_bcrypt_release(ReleaseType)},
+        {jq, is_jq_supported()},
+        {observer, is_app(observer)},
+        {mnesia_rocksdb, is_rocksdb_supported()}
+    ],
+    [App || {App, false} <- OptionalApps].
 
 is_app(Name) ->
     case application:load(Name) of