Using redis with heroku - heroku

This is my first time using redis and the only reason I am is because I'm trying out autocomplete search tutorial. The tutorial works perfectly in development but I'm having trouble setting up redis for heroku.
I already followed these steps on the heroku docs for setting up redis but when I run heroku run rake db:seed I get Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
I'm not very familiar with heroku so if you guys need any more information let me know.
Edit
I've completed the initializer steps shown here and when I run heroku config:get REDISCLOUD_URL the result is exactly the same as the Redis Cloud URL under the config vars section of my Heroku settings.
Following the documentation, I then set up config/initializers/redis.rb like so:
if ENV["REDISCLOUD_URL"]
$redis = Redis.new(:url => ENV["REDISCLOUD_URL"])
end
Just to check, I tried substituting the actual URL for redis cloud inside the if block instead of just the REDISCLOUD_URL variable but that didn't work. My error message hasn't changed when I try to seed the heroku db.

It’s not enough to just create a $redis variable that points to the installed Redis server, you also need to tell Soulmate about it, otherwise it will default to localhost.
From the Soulmate README you should be able to do something like this in an initializer (instead of your current redis.rb initializer, which you won’t need unless you are using Redis somewhere else in your app):
if ENV["REDISCLOUD_URL"]
Soulmate.redis = ENV["REDISCLOUD_URL"]
end
Looking at the Soulmate source, an easier way may be to set the REDIS_URL environment variable to the Redis url, either instead of or as well as REDISCLOUD_URL, as it looks like Soulmate checks this before falling back to localhost.

Your code is trying to connect to a local Redis instance, instead the one from Redis Cloud - make sure you've completed the initializer step as detailed in order to resolve this.

Related

spring-data-mongodb/k8s "Database name must not contain slashes, dots, spaces, quotes, or dollar signs"

I'm at a real loss on this one. I've been attempting to get my application running with a replica set in Kubernetes for awhile. I'm setting: spring.data.mongodb.uri=${MYAPP_MONGODB}:mongodb://localhost:27017/myapp
in application.properties and using Spring Data to access my objects.
Locally using a local MongoDB container it works fine even if I set the env var to my remote databases locally I can connect to them and work just fine. But when I put the value of MYAPP_MONGODB into k8s secrets when the container boots I get quoted error from the title. The value is like this:
mongodb://myuser:mypasswd#1.1.1.1:27017,2.2.2.2:27017,3.3.3.3:27017,4.4.4.4:27017,5.5.5.5:27017/myapp
I reviewed the source and still baffled as to why this is happening. Pulling the secret from the k8s environment it is correct.
Any help is much appreciated!
It sounds like your secret in k8s might be setup incorrectly. I would try uploading your secrets again and decoding them to make sure they are correct. Careful for random line breaks :)

How do I share Redis key/values across multiple instances with rackup/Sinatra?

I'm trying to use Redis as my session store, which seem to work just fine. However I can't figure out how to let multiple instances of Sinatra access the same session. This is what I have in my config.ru:
require 'redis-rack'
use Rack::Session::Redis, :redis_server => "redis://#{ENV['REDIS_HOST']}:6379/0"
I must be missing an argument to set this, but the documentation is lacking for this case:
https://github.com/redis-store/redis-rack
Maybe that's not what I want to achieve this behavior?
The end goal is to be deploying my Sinatra application with docker to a clustered environment so I can release new versions without downtime. So whatever let's me share the rack session between multiple instances works. I suppose I could create a redis object manually and not use the session keyword, just seems like the wrong way to do it.

Config variables not visible as environment variables in Heroku app

I've set a few custom config variables. I can see them in my application's settings->config variables. I can also see the values with the heroku config command. But when I start my application the environment variables are not there. I use (System/getenv "MY_VARIABLE_NAME") in Clojure to fetch them.
Is it because I try to retrieve them at boot time? Are they only available later? Or is there some twitch which I can get rid of by doing some trick? I've used config variables in Heroku before and they've worked, I don't know what's the problem here...
I was trying to retrieve client ID and secret for oauth authentication with Google from a config variable with System/getenv. I use a library called Friend to do this. Problem is, the set up for oauth parameters in that library is done via macros. And macro expansion happens compile-time. Heroku config variables are not available as environment variables during compilation (for good reasons). They are, however available via filesystem which was my solution to the problem. So instead of:
(System/getenv "MY_APP_GOOGLE_CLIENT_ID")
I'm using this:
(slurp (str (System/getenv "ENV_DIR") "/" "MY_APP_GOOGLE_CLIENT_ID"))
And it works!

Is it Possible to Alias Config Variables on Heroku?

I'm currently writing a tutorial on setting up Wordpress on Heroku. Right now I'm using the ClearDB add-on which sets a CLEARDB_DATABASE_URL ENV variable automatically. Is it possible to alias the ENV variable through Heroku as DATABASE_URL?
It's not possible to alias an config var or refer to one from another. I asked a similar question and this is what they said:
I'm afraid that Config Variables can't refer to each other in this
way, as they are simple a collection of Names and Values, with no
interpolation or calculation available to the values.
You might like try a
profile
file...
I've had a similar issue - where I'm trying to use an app in a pipeline connected to 2 different heroku DB's - in order to keep all he environments consistent in code, I did the following:
Heroku Configs:
DATABASE_URL=XXXXXXXX - this was the first DB that heroku attached
HEROKU_POSTGRESQL_JADE_URL=XXXX - this was the second DB that heroku attached (the key name changes in each environment)
SECOND_DB_KEY_NAME=HEROKU_POSTGRESQL_JADE_URL
(ie. after each environment was set up - I added a reference to the new key)
This second DB key name, does not change if the DB credentials refresh.
In code, I then did the following at start up:
const databaseUrlKey = process.env.SECOND_DB_KEY_NAME
process.env['SECOND_DATABASE_URL'] = process.env[databaseUrlKey]
Maybe I'll just say something stupid, but why not just do this:
heroku config:set DATABASE_URL=CLEARDB_DATABASE_URL
In code:
ENV[ENV['DATABASE_URL']]
I'm not sure this information will be helpful for anyone but just in case:
This question involves an eroneous assertion. The ClearDB add-on does not set a CLEARDB_DATABASE_URL ENV variable. The ClearDB add-on creates a CLEARDB_DATABASE_URL config var. When the app is started an ENV variable is created from the config var. These two variables are different and could even have different values if you changed the ENV variable in your code base.
Of course, within your code base, you can do whatever you want with the ENV variables.
As to whether config vars can reference other config vars, or other ENV variables, or vica versa - I don't know. But surely this would be something pretty hacky, and contrary to intended use, and proper coding practice, and socially responsible behavior.

How to use DATABSE_URL in Play 2.0 (Scala) for local connection to PostgreSQL 9.1 & Heroku?

I have developed a first web application using a local PostgreSQL 9.1 on OSX (Lion 10.7.4) using Play! Framework 2.0.3. I started with my database connection defined in conf/application.conf (relative to application's directory) with
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/fotoplay"
db.default.user=foo
db.default.password=bar
(username and password have been changed before posting) This works for writing and testing. I now want to deploy to Heroku. I have set up the Procfile (in application's directory) with a single line:
web: target/start -Dhttp.port=${PORT} ${JAVA_OPTS}
-DapplyEvolutions.default=false
-Ddb.default.driver=org.postgresql.Driver
-Ddb.default.url=$DATABASE_URL
I have exported DATABASE_URL in .bash_profile so that I can echo $DATABASE_URL at the system command line and get
postgres://foo:bar#localhost/photoplay
At Heroku, I have set up an instance of postgresql. I'm not sure on how to write a database evolution to populate heroku, so I turned that off evolutions and populated my database manually. My current evolutions build tables and populate them with some initial data from some csv files, so I would need to place the csv files somewhere accessible to heroku. At least for a first pass, I don't want to tackle this issue just yet. But as a result of the manual steps, I have a populated database on Heroku.
With this configuration, my application runs correctly locally. However, I am not using DATABASE_URL, which seems wrong. I pushed this to HEROKU and is is not able to connect to the database. The error is:
Caused by: org.postgresql.util.PSQLException:
FATAL: password authentication failed for user "foo"
If I remove the username & password, I break my local configuration. I should be able to use DATABASE_URL 'instead of' the older syntax, but I don't quite know how to do that. I don't think that I can figure this out experimentally (wandering high dimensional spaces is hopeless, and there are several possible configuration settings that might be involved & are subject to typos). Any guidance on how I should set up application.conf would be appreciated.
Worth a try: in your application.conf file, change your db.default.url parameter in order to include the username & password in the URL, and remove the db.default.user and db.default.password parameters:
db.default.url="postgres://foo:bar#localhost/fotoplay"

Resources