Commit ebbfe6d8 authored by Aral Balkan's avatar Aral Balkan

Cache feature is not used when running in production

parent 12134dce
......@@ -69,6 +69,10 @@ class App
return instance
@isRunningInDevelopment = (process.env.PORT == undefined)
# To test production behaviour on development, uncomment:
# @isRunningInDevelopment = false
@isRunningInProduction = !@isRunningInDevelopment
# Initialise global properties of the app.
......@@ -79,15 +83,17 @@ class App
@logsDirectory = path.join @privateDirectory, 'logs'
@cacheDirectory = path.join @privateDirectory, 'cache'
@rulePartialsDirectory = path.join @cacheDirectory, 'rule-partials'
@dataForSiteCacheDirectory = path.join @cacheDirectory, 'data', 'site'
@dataForAppCacheDirectory = path.join @cacheDirectory, 'data', 'app'
@indicesDirectory = path.join @cacheDirectory, 'indices'
fs.ensureDirSync @indicesDirectory
#
# Set up cache-related properties if we’re running in development.
# (We don’t use the cache when in production.)
#
if @isRunningInDevelopment
@cacheDirectory = path.join @privateDirectory, 'cache'
@rulePartialsDirectory = path.join @cacheDirectory, 'rule-partials'
@dataForSiteCacheDirectory = path.join @cacheDirectory, 'data', 'site'
@dataForAppCacheDirectory = path.join @cacheDirectory, 'data', 'app'
@indicesDirectory = path.join @cacheDirectory, 'indices'
fs.ensureDirSync @indicesDirectory
# Set up logging.
logFile = path.join @logsDirectory, 'App.log'
......@@ -203,6 +209,11 @@ class App
# Do we need to generate the themes? (We only need to if they have changed.)
weNeedToGenerateThemes: =>
# Always regenerate themes (don’t cache anything) when running on production.
if @isRunningInProduction
return true
cacheDirectory = path.join @homeDirectory, '.private', 'cache'
themeCacheCurrentModifiedTimesFile = path.join cacheDirectory, 'current-modified-times', 'themes.json'
themeCachePreviousModifiedTimesFile = path.join cacheDirectory, 'previous-modified-times', 'themes.json'
......@@ -218,7 +229,7 @@ class App
Promise.try =>
startTime = new Date
if @weNeedToGenerateThemes()
@info "\t ✓ Generating theme…"
@info "\t✓ Generating theme…"
fs.removeAsync @themeForAppDataDirectory
.then => fs.removeAsync @themeForSiteDataDirectory
......@@ -228,7 +239,7 @@ class App
.then => fs.copyAsync @siteThemeDirectory, @themeForSiteDataDirectory
.then => @generateStylesForThemeAtDirectory @themeForAppDataDirectory
.then => @generateStylesForThemeAtDirectory @themeForSiteDataDirectory
.then => endTime = new Date; @info "\t ✓ Theme generated. (#{endTime - startTime} ms)"
.then => endTime = new Date; @info "\t✓ Theme generated. (#{endTime - startTime} ms)"
.catch (e) =>
@error "Fatal: Could not generate themes.\n\n#{e}"
console.trace()
......
......@@ -128,6 +128,15 @@ class Blockdown
# ===== caching for the template also.
#
decideCachePolicy: =>
#
# Note: I do not trust the cache for use in production so we detect that and turn it off.
# ===== A production deployment should always do a full, clean build.
#
if app.isRunningInProduction
console.log "\t✓ Better is running in production, not using the cache feature."
@useContentCache == false
return
cacheDirectory = path.join app.homeDirectory, '.private', 'cache'
themeCacheCurrentModifiedTimesFile = path.join cacheDirectory, 'current-modified-times', 'themes.json'
themeCachePreviousModifiedTimesFile = path.join cacheDirectory, 'previous-modified-times', 'themes.json'
......@@ -200,7 +209,10 @@ class Blockdown
# TODO: Make this async
# TODO: Update render pipeline to only render content (and these partials)
# in the content pass.
@saveRulePartials()
# Note: Only save rule partials if we’re running in development
# ===== (the cache feature is not used in production.)
if app.isRunningInDevelopment
@saveRulePartials()
@saveBlockerListJSON app.dataForSiteDirectory
.then =>
......@@ -484,7 +496,9 @@ class Blockdown
fs.readFileAsync file, 'utf-8'
.then (content) =>
@info "Rendering Blockdown in file #{file}"
if @useContentCache
console.log "\t✓ Rendering Blockdown in file #{file}"
fileComponents = file.split('/')
......@@ -539,20 +553,23 @@ Better is a Safari content blocker for <a href='https://itunes.apple.com/us/app/
#
# Save the index title info for the cache
# (only if we’re running in development. The cache feature
# is currently not used in production.)
#
pathObject = path.parse file
relativeFolder = pathObject.dir.replace contentPath, ''
jsonFileName = "#{pathObject.name}.html.json"
indexCacheFile = path.join app.indicesDirectory, relativeFolder, jsonFileName
indexCacheObject =
category: category
titleOfPageClean: titleOfPageClean
listItem: listItem
@info "Index cache file: #{indexCacheFile}"
@info "Index cache object:"
# console.log indexCacheObject
fs.outputJSONSync indexCacheFile, indexCacheObject
if app.isRunningInDevelopment
pathObject = path.parse file
relativeFolder = pathObject.dir.replace contentPath, ''
jsonFileName = "#{pathObject.name}.html.json"
indexCacheFile = path.join app.indicesDirectory, relativeFolder, jsonFileName
indexCacheObject =
category: category
titleOfPageClean: titleOfPageClean
listItem: listItem
@info "Index cache file: #{indexCacheFile}"
@info "Index cache object:"
# console.log indexCacheObject
fs.outputJSONSync indexCacheFile, indexCacheObject
#
......
......@@ -33,6 +33,8 @@ touch = require 'touch'
Cache = require './Cache2'
Promise = require 'thrush'
class Builder
instance = null
......@@ -112,7 +114,11 @@ class Builder
# Update the cache.
#
updateCache: =>
(new Cache).build()
Promise.try =>
if app.isRunningInProduction
@info "\t✓ App is running in production, not creating cache."
else
(new Cache).build()
#
# Render content in Blockdown format.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment