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
This commit is contained in:
jomo 2015-08-30 02:11:35 +02:00
parent ac9cd93c8e
commit e56f300d3e
2 changed files with 12 additions and 13 deletions

View File

@ -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": [

View File

@ -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) {