nickva commented on a change in pull request #525: Pre-calculate application 
start order
URL: https://github.com/apache/couchdb/pull/525#discussion_r116919550
 
 

 ##########
 File path: src/couch/src/test_util.erl
 ##########
 @@ -258,3 +259,48 @@ load_applications_with_stats() ->
 stats_file_to_app(File) ->
     [_Desc, _Priv, App|_] = lists:reverse(filename:split(File)),
     erlang:list_to_atom(App).
+
+calculate_start_order(Apps) ->
+    AllApps = calculate_start_order(sort_apps(Apps), []),
+    % AllApps may not be the same list as Apps if we
+    % loaded any dependencies. We recurse here when
+    % that changes so that our sort_apps function has
+    % a global view of all applications to start.
+    case lists:usort(AllApps) == lists:usort(Apps) of
+        true -> AllApps;
+        false -> calculate_start_order(AllApps)
+    end.
+
+calculate_start_order([], StartOrder) ->
+    lists:reverse(StartOrder);
+calculate_start_order([App | RestApps], StartOrder) ->
+    NewStartOrder = load_app_deps(App, StartOrder),
+    calculate_start_order(RestApps, NewStartOrder).
+
+load_app_deps(App, StartOrder) ->
+    case lists:member(App, StartOrder) of
+        true ->
+            StartOrder;
+        false ->
+            case application:load(App) of
+                ok -> ok;
+                {error, {already_loaded, App}} -> ok
+            end,
+            {ok, Apps} = application:get_key(App, applications),
+            Deps = case App of
+                kernel -> Apps;
+                stdlib -> Apps;
+                _ -> lists:usort([kernel, stdlib | Apps])
+            end,
+            NewStartOrder = lists:foldl(fun(Dep, Acc) ->
+                load_app_deps(Dep, Acc)
+            end, StartOrder, Deps),
+            [App | NewStartOrder]
+    end.
+
+sort_apps(Apps) ->
+    Weighted = [weight_app(App) || App <- Apps],
+    element(2, lists:unzip(lists:sort(Weighted))).
+
+weight_app(couch_log) -> {0.0, couch_log};
 
 Review comment:
   Nice!
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to