Skip to content

Commit

Permalink
move WEB_CONCURRENCY logic to separate file (#467)
Browse files Browse the repository at this point in the history
All buildpacks use `profile/WEB_CONCURRENCY.sh` now which will be overwritten by the later buildpacks to avoid earlier buildpacks setting defaults for later ones at startup, with wrong defaults for some languages.
  • Loading branch information
dzuelke authored and jmorrell committed Aug 30, 2017
1 parent f480159 commit 940c813
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
35 changes: 35 additions & 0 deletions profile/WEB_CONCURRENCY.sh
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

calculate_concurrency() {
WEB_CONCURRENCY=${WEB_CONCURRENCY-$((MEMORY_AVAILABLE/WEB_MEMORY))}
if (( WEB_CONCURRENCY < 1 )); then
WEB_CONCURRENCY=1
fi
echo $WEB_CONCURRENCY
}

log_concurrency() {
echo "Detected $MEMORY_AVAILABLE MB available memory, $WEB_MEMORY MB limit per process (WEB_MEMORY)"
echo "Recommending WEB_CONCURRENCY=$WEB_CONCURRENCY"
}

detect_memory() {
local default=$1
local limit=$(ulimit -u)

case $limit in
256) echo "512";; # Standard-1X
512) echo "1024";; # Standard-2X
16384) echo "2560";; # Performance-M
32768) echo "14336";; # Performance-L
*) echo "$default";;
esac
}

export MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(detect_memory 512)}
export WEB_MEMORY=${WEB_MEMORY-512}
export WEB_CONCURRENCY=$(calculate_concurrency)

if [ "$LOG_CONCURRENCY" = "true" ]; then
log_concurrency
fi
38 changes: 0 additions & 38 deletions profile/nodejs.sh
@@ -1,41 +1,3 @@
calculate_concurrency() {
MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(detect_memory 512)}
WEB_MEMORY=${WEB_MEMORY-512}
WEB_CONCURRENCY=${WEB_CONCURRENCY-$((MEMORY_AVAILABLE/WEB_MEMORY))}
if (( WEB_CONCURRENCY < 1 )); then
WEB_CONCURRENCY=1
fi
WEB_CONCURRENCY=$WEB_CONCURRENCY
}

log_concurrency() {
echo "Detected $MEMORY_AVAILABLE MB available memory, $WEB_MEMORY MB limit per process (WEB_MEMORY)"
echo "Recommending WEB_CONCURRENCY=$WEB_CONCURRENCY"
}

detect_memory() {
local default=$1
local limit=$(ulimit -u)

case $limit in
256) echo "512";; # Standard-1X
512) echo "1024";; # Standard-2X
16384) echo "2560";; # Performance-M
32768) echo "14336";; # Performance-L
*) echo "$default";;
esac
}

export PATH="$HOME/.heroku/node/bin:$HOME/.heroku/yarn/bin:$PATH:$HOME/bin:$HOME/node_modules/.bin"
export NODE_HOME="$HOME/.heroku/node"
export NODE_ENV=${NODE_ENV:-production}

calculate_concurrency

export MEMORY_AVAILABLE=$MEMORY_AVAILABLE
export WEB_MEMORY=$WEB_MEMORY
export WEB_CONCURRENCY=$WEB_CONCURRENCY

if [ "$LOG_CONCURRENCY" = "true" ]; then
log_concurrency
fi
10 changes: 5 additions & 5 deletions test/run
Expand Up @@ -326,35 +326,35 @@ testBuildWithUserCacheDirectoriesCamel() {
}

testConcurrency1X() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=512 capture $(pwd)/profile/nodejs.sh
LOG_CONCURRENCY=true MEMORY_AVAILABLE=512 capture $(pwd)/profile/WEB_CONCURRENCY.sh
assertCaptured "Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=1"
assertCapturedSuccess
}

testConcurrency2X() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 capture $(pwd)/profile/nodejs.sh
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 capture $(pwd)/profile/WEB_CONCURRENCY.sh
assertCaptured "Detected 1024 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=2"
assertCapturedSuccess
}

testConcurrencyPerformanceM() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=2560 capture $(pwd)/profile/nodejs.sh
LOG_CONCURRENCY=true MEMORY_AVAILABLE=2560 capture $(pwd)/profile/WEB_CONCURRENCY.sh
assertCaptured "Detected 2560 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=5"
assertCapturedSuccess
}

testConcurrencyPerformanceL() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=14336 capture $(pwd)/profile/nodejs.sh
LOG_CONCURRENCY=true MEMORY_AVAILABLE=14336 capture $(pwd)/profile/WEB_CONCURRENCY.sh
assertCaptured "Detected 14336 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=28"
assertCapturedSuccess
}

testConcurrencyCustomLimit() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 WEB_MEMORY=256 capture $(pwd)/profile/nodejs.sh
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 WEB_MEMORY=256 capture $(pwd)/profile/WEB_CONCURRENCY.sh
assertCaptured "Detected 1024 MB available memory, 256 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=4"
assertCapturedSuccess
Expand Down

0 comments on commit 940c813

Please sign in to comment.