Skip to content

Commit bc0bcf7

Browse files
committed
Merge pull request heroku#250 from heroku/fix-signature-cache-invalidation
test and implementation to invalidate cache on runtime signature changes
2 parents 7a40a98 + 6fd9b8f commit bc0bcf7

File tree

8 files changed

+79
-33
lines changed

8 files changed

+79
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Upcoming
2+
3+
- Fix runtime signature cache invalidation
4+
15
## v77
26

37
- Skip npm bootstrapping with iojs
@@ -15,8 +19,8 @@
1519

1620
## v73 (24/4/2015)
1721

18-
- Patch for caching to disable cache restoration if node_modules already exists (eg from being git submoduled or checked into git)
22+
- Disable cache restoration if node_modules already exists (eg from being git submoduled or checked into git)
1923

2024
## v72 (23/4/2015)
2125

22-
* Accepts `cacheDirectories` array in package.json to override default `node_modules` caching
26+
* Accept `cacheDirectories` array in package.json to override default `node_modules` caching

bin/compile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,9 @@ header "Installing binaries"
8989
install_bins | indent
9090

9191
restore_cache() {
92-
local cache_status=$(get_cache_status)
92+
local cache_status="$(get_cache_status)"
9393

94-
if [ "$cache_status" == "disabled" ]; then
95-
echo "Skipping (cache disabled)"
96-
elif [ "$cache_status" == "invalidated" ]; then
97-
echo "Skipping (cache invalidated)"
98-
else
94+
if [ "$cache_status" == "valid" ]; then
9995
local cache_directories=$(get_cache_directories)
10096
if [ "$cache_directories" == "" ]; then
10197
echo "Loading 1 from cacheDirectories (default):"
@@ -104,6 +100,8 @@ restore_cache() {
104100
echo "Loading $(echo $cache_directories | wc -w | xargs) from cacheDirectories (package.json):"
105101
restore_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories
106102
fi
103+
else
104+
echo "Skipping cache ($cache_status)"
107105
fi
108106
}
109107

@@ -124,6 +122,7 @@ build_dependencies | indent
124122

125123
cache_build() {
126124
local cache_directories=$(get_cache_directories)
125+
127126
echo "Clearing previous node cache"
128127
clear_cache
129128
if [ "$cache_directories" == "" ]; then
@@ -133,6 +132,7 @@ cache_build() {
133132
echo "Saving $(echo $cache_directories | wc -w | xargs) cacheDirectories (package.json):"
134133
save_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories
135134
fi
135+
save_signature
136136
}
137137

138138
header "Caching build"

lib/cache.sh

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ create_signature() {
55
}
66

77
save_signature() {
8-
echo "$(get_signature)" > $CACHE_DIR/node/signature
8+
echo "$(create_signature)" > $CACHE_DIR/node/signature
99
}
1010

1111
load_signature() {
@@ -16,19 +16,11 @@ load_signature() {
1616
fi
1717
}
1818

19-
signature_changed() {
20-
if ! [ "$(create_signature)" == "$(load_signature)" ]; then
21-
return 1
22-
else
23-
return 0
24-
fi
25-
}
26-
2719
get_cache_status() {
2820
if ! ${NODE_MODULES_CACHE:-true}; then
29-
echo "disabled"
30-
elif signature_changed; then
31-
echo "invalidated"
21+
echo "disabled by config"
22+
elif [ "$(create_signature)" != "$(load_signature)" ]; then
23+
echo "new runtime signature"
3224
else
3325
echo "valid"
3426
fi
@@ -66,6 +58,7 @@ restore_cache_directories() {
6658

6759
clear_cache() {
6860
rm -rf $CACHE_DIR/node
61+
mkdir -p $CACHE_DIR/node
6962
}
7063

7164
save_cache_directories() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A fake README, to keep npm from polluting stderr.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "node-buildpack-test-app",
3+
"version": "0.0.1",
4+
"description": "node buildpack integration test app",
5+
"repository" : {
6+
"type" : "git",
7+
"url" : "http://github.com/example/example.git"
8+
},
9+
"dependencies": {
10+
},
11+
"engines": {
12+
"node": "0.12.6"
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A fake README, to keep npm from polluting stderr.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "node-buildpack-test-app",
3+
"version": "0.0.1",
4+
"description": "node buildpack integration test app",
5+
"repository" : {
6+
"type" : "git",
7+
"url" : "http://github.com/example/example.git"
8+
},
9+
"dependencies": {
10+
},
11+
"engines": {
12+
"node": "0.12.7"
13+
}
14+
}

test/run

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
#!/usr/bin/env bash
22
# See README.md for info on running these tests.
33

4+
testBuildWithCache() {
5+
cache=$(mktmpdir)
6+
7+
compile "stable-node" $cache
8+
assertCaptured "Skipping cache (new runtime"
9+
assertEquals "1" "$(ls -1 $cache/node | grep node_modules | wc -l | tr -d ' ')"
10+
assertCapturedSuccess
11+
12+
compile "stable-node" $cache
13+
assertNotCaptured "- node_modules (not cached - skipping)"
14+
assertCapturedSuccess
15+
16+
rm -rf "$cache/node/node_modules"
17+
compile "stable-node" $cache
18+
assertCaptured "- node_modules (not cached - skipping)"
19+
assertCapturedSuccess
20+
}
21+
22+
testSignatureInvalidation() {
23+
cache=$(mktmpdir)
24+
env_dir=$(mktmpdir)
25+
26+
compile "node-0.12.6" $cache
27+
assertCaptured "Downloading and installing node 0.12.6"
28+
assertCapturedSuccess
29+
30+
compile "node-0.12.7" $cache
31+
assertCaptured "Downloading and installing node 0.12.7"
32+
assertCaptured "Skipping cache (new runtime"
33+
assertCapturedSuccess
34+
}
35+
436
testModulesCheckedIn() {
537
cache=$(mktmpdir)
638
compile "modules-checked-in" $cache
@@ -205,19 +237,6 @@ testNodeModulesCached() {
205237
assertCapturedSuccess
206238
}
207239

208-
testBuildWithCache() {
209-
cache=$(mktmpdir)
210-
211-
compile "stable-node" $cache
212-
assertCaptured "- node_modules (not cached - skipping)"
213-
assertEquals "1" "$(ls -1 $cache/node | grep node_modules | wc -l | tr -d ' ')"
214-
assertCapturedSuccess
215-
216-
compile "stable-node" $cache
217-
assertNotCaptured "- node_modules (not cached - skipping)"
218-
assertCapturedSuccess
219-
}
220-
221240
testBuildWithUserCacheDirectories() {
222241
cache=$(mktmpdir)
223242

0 commit comments

Comments
 (0)