From e56f300d3e2a6442e3f52068839b5bcce1ac5e35 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 30 Aug 2015 02:11:35 +0200 Subject: [PATCH] use EPHEMERAL_STORAGE instead of HEROKU env afaik you can have persistent storage on heroku, at least via addons and heroku is probably not the only environment where one has a temporary file system --- app.json | 2 +- lib/cache.js | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app.json b/app.json index 40bd9ac..f1cde27 100644 --- a/app.json +++ b/app.json @@ -10,7 +10,7 @@ ], "website": "https://crafatar.com/", "env": { - "HEROKU": "true", + "EPHEMERAL_STORAGE": "true", "BUILDPACK_URL": "https://github.com/mojodna/heroku-buildpack-multi.git#build-env" }, "addons": [ diff --git a/lib/cache.js b/lib/cache.js index 8527300..e8b7d72 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -8,11 +8,11 @@ var fs = require("fs"); var redis = null; // sets up redis connection -// flushes redis when running on heroku (files aren't kept between pushes) +// flushes redis when using ephemeral storage (e.g. Heroku) function connect_redis() { logging.log("connecting to redis..."); // parse redis env - var redis_env = (process.env.REDISCLOUD_URL || process.env.REDIS_URL); + var redis_env = process.env.REDISCLOUD_URL || process.env.REDIS_URL; var redis_url = redis_env ? url.parse(redis_env) : {}; redis_url.port = redis_url.port || 6379; redis_url.hostname = redis_url.hostname || "localhost"; @@ -23,15 +23,15 @@ function connect_redis() { } redis.on("ready", function() { logging.log("Redis connection established."); - if (process.env.HEROKU) { - logging.log("Running on heroku, flushing redis"); + if (process.env.EPHEMERAL_STORAGE) { + logging.log("Storage is ephemeral, flushing redis"); redis.flushall(); } }); - redis.on("error", function (err) { + redis.on("error", function(err) { logging.error(err); }); - redis.on("end", function () { + redis.on("end", function() { logging.warn("Redis connection lost!"); }); } @@ -67,15 +67,14 @@ exp.get_redis = function() { // updates the redis instance's server_info object // callback: error, info object exp.info = function(callback) { - redis.info(function (err, res) { - + redis.info(function(err, res) { // parse the info command and store it in redis.server_info // this code block was taken from mranney/node_redis#on_info_cmd // http://git.io/LBUNbg var lines = res.toString().split("\r\n"); var obj = {}; - lines.forEach(function (line) { + lines.forEach(function(line) { var parts = line.split(":"); if (parts[1]) { obj[parts[0]] = parts[1]; @@ -99,7 +98,7 @@ exp.info = function(callback) { // callback: error exp.update_timestamp = function(rid, userId, hash, temp, callback) { logging.debug(rid, "updating cache timestamp"); - var sub = temp ? (config.caching.local - 60) : 0; + var sub = temp ? config.caching.local - 60 : 0; var time = Date.now() - sub; // store userId in lower case if not null userId = userId && userId.toLowerCase(); @@ -117,8 +116,8 @@ exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) { logging.debug(rid, "caching skin:" + skin_hash + " cape:" + cape_hash); var time = Date.now(); // store shorter null byte instead of "null" - skin_hash = (skin_hash === null ? "" : skin_hash); - cape_hash = (cape_hash === null ? "" : cape_hash); + skin_hash = skin_hash === null ? "" : skin_hash; + cape_hash = cape_hash === null ? "" : cape_hash; // store userId in lower case if not null userId = userId && userId.toLowerCase(); if (skin_hash === undefined) {