From 31ec4d20d39d33ef2889dfd3be07c20c424c7ebd Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 28 Jan 2016 12:48:21 +0700 Subject: [PATCH] fix: dependency calculation has numerous issues. Loop were not supported, and many bug crippled the order of the calculation. Added also some caching to avoid recalculating this each time. --- bin/compose | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/bin/compose b/bin/compose index ad1c0f2..13dc0b9 100755 --- a/bin/compose +++ b/bin/compose @@ -648,25 +648,37 @@ export -f get_service_deps _rec_get_depth() { - local elt=$1 - - #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies" - if [ "${depths[$elt]}" ]; then + local elt=$1 dep deps max cache_file="$state_tmpdir/$FUNCNAME.cache.$1" + [ "${depths[$elt]}" ] && return 0 + if [ -e "$cache_file" ]; then + # debug "$FUNCNAME: cache hit ($*)" + depths[$elt]=$(cat "$cache_file") return 0 fi - deps=$(get_service_deps "$elt") || return 1 - if [ -z "$deps" ]; then - depths[$elt]=0 - fi + visited[$elt]=1 + #debug "Setting visited[$elt]" + #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies" + deps=$(get_service_deps "$elt") || { + debug "Failed get_service_deps $elt" + return 1 + } + # debug "$elt deps are:" $deps max=0 for dep in $deps; do + [ "${visited[$dep]}" ] && { + #debug "Already computing $dep" + continue + } _rec_get_depth "$dep" || return 1 - if (( "${depths[$dep]}" > "$max" )); then + #debug "Requesting depth[$dep]" + if (( ${depths[$dep]} > max )); then max="${depths[$dep]}" - fi + fi done + # debug "Setting depth[$elt] to $((max + 1))" depths[$elt]=$((max + 1)) + echo "${depths[$elt]}" > $cache_file } export -f _rec_get_depth @@ -685,11 +697,10 @@ get_ordered_service_dependencies() { fi declare -A depths - visited=() + declare -A visited heads=("${services[@]}") while [ "${#heads[@]}" != 0 ]; do array_pop heads head - visited+=("$head") _rec_get_depth "$head" || return 1 done