in order to use RedisToGo on development, I need to set the environment variable as follows:
ENV["REDISTOGO_URL"] = 'redis://username:password#my.host:6389'
How can I create a redis connection with the username, password and my.host setting, assuming I already have redis-server installed? I think there's a commandline command I have used to achieve this before but I can't seem to remember it now and I can't find any information on this online. Suggestions?
My setup is similar and uses the following:
# config/initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(host: uri.host, port: uri.port, password: uri.password)
development & test environments:
ENV['REDISTOGO_URL'] = 'redis:://#localhost:6379'
And then you don't need to set anything for production as long as you have the gem and RedisToGo installed on your Heroku instance.
Related
I am working on an application that has set its default host to localhost:
config.action_controller.default_url_options = { host: "localhost" }
config.action_mailer.default_url_options = { host: "localhost" }
This works fine if you're serving your app on a specific port (like http://localhost:3000). I am using Pow so that I can access the app from a URL like http://myapp.dev.
How can I change this setting so that it will work with my domain as well as the other developers using localhost? I need to generate full URLs since they will be used in emails. Is it possible to pass some sort of config value to Pow?
This is the best way I found:
1- Create a config/smtp_settings.example.yml file:
development:
:method: :letter_opener
:url_options:
:host: localhost
:port: 3000
production: &production
:method: :smtp
:url_options:
:host: www.the_site.com
:smtp_settings:
address: smtp.gateway.com
port: 465
user_name: someone#somewhere.com
# etc ...
staging:
<<: *production
test:
:method: :test
:url_options:
:host: test.host
Notes:
This uses letter_opener as development mailer, change to your own method
There is a staging environment defined as an exact copy of the production environment, remove if you don't use staging
2- Remove config.action_mailer.delivery_method and config.action_mailer.default_url_options and config.action_mailer.smtp_settings from all files under config/environments/ folder
3- Add in the file config/application.rb the following:
config.action_mailer.delivery_method = config_for(:smtp_settings)[:method]
config.action_mailer.default_url_options = config_for(:smtp_settings)[:url_options]
config.action_mailer.smtp_settings = config_for(:smtp_settings)[:smtp_settings] if config_for(:smtp_settings)[:method] == :smtp
Notes: config_for has been introduced in the latest version of Rails (4.2.0 as per writing). If you use an older version of rails you must manually load the yml files
And the trick on top of all of that to answer your question:
Push to your repository the file config/smtp_settings.example.yml and ignore the file config/smtp_settings.yml.
Each developper of your team would have to create their own config/smtp_settings.yml by copying the file smtp_settings.example.yml and changing the host to match their machine's IP address, so the mail send by each developer leads to their own machine.
You off course requires you to start the local development server binded to 0.0.0.0 so it's accessible from other hosts (considering your security environment off course)
You could use an environment variable to configure a default host. See Luc Boissaye's answer on how to set this up with dotenv. If you don't add the .env to your repo, each developer can create his own .env and configure his (or her) own preferences.
But you can also use existing environment variables to configure the default_url_options. For example Pow.cx sets the POW_DOMAINS variable:
config.action_mailer.default_url_options = {
ENV['POW_DOMAINS'].present? ? 'my-app.dev' : 'localhost'
}
As far as I know you only need to set the config.action_controller.default_url_options if you want to force it to some default. When you use the rails _path helpers in your views, this will generate a relative URL (/path). If you use the _url helpers, this will generate an absolute URL (http://your.host/path), but both the protocol (http) and host (your.host) are request based.
In development, I like to use dotenv gem(https://github.com/bkeepers/dotenv).
I put in .env :
DOMAIN=myapp.dev
And I put inside config/route.rb:
MyApplication::Application.routes.draw do
default_url_options host: ENV['DOMAIN']
In production I define on heroku my domain with this command :
h config:set DOMAIN=myapp.herokuapp.com
or with a custom domain :
h config:set DOMAIN=superdomain.com
This will check if Application root directory is symlinked in .pow of current user and set the host accordingly.
if File.exists?(Dir.home+"/.pow/#{Rails.root.basename}")
config.action_mailer.default_url_options = { host: "#{Rails.root.basename}.dev" }
else
config.action_mailer.default_url_options = { host: "localhost" }
end
Described on Action Mailer Basics:
Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the :host parameter yourself.
As the :host usually is consistent across the application you can configure it globally in config/application.rb:
config.action_mailer.default_url_options = { host: 'example.com' }
Because of this behavior you cannot use any of the *_path helpers inside of an email. Instead you will need to use the associated *_url helper. For example instead of using
<%= link_to 'welcome', welcome_path %>
You will need to use
<%= link_to 'welcome', welcome_url %>
The use of Pow makes things a little trickier but thankfully it integrates with XIP.io so you can access myapp.dev from machines on the local network. Just follow the instructions here: Accessing Virtual Hosts from Other Computers.
The question doesn't mention this, but it might be useful information - If you'd like to actually send mail in your dev (or perhaps test/staging) environment, Instead of seeing mail just in the rails console (with something like powder applog), I'd recommend just hooking it up to a gmail.
You need to override default_url_options in your application controller (at least in rails 3)
http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
class ApplicationController < ActionController::Base
def default_url_options
if Rails.env.production?
{:host => "myproduction.com"}
else
{}
end
end
end
I am using Redis with my Rails app. I have sidekiq gem installed too. My redis server runs in the same machine in default port.
I created a initializer which initalizes a redis lient.
config/initializers/redis.rb
$redis = Redis.new(:host => 'localhost', :port => 6379)
I have another initalizer that sets the number of accounts currently active in the system.
config/initializers/z_account_list.rb
$redis.set('accounts',Account.count);
In one of my views i am using this piece of code.
<div class="more-text">and <%= "#{$redis.get('accounts')}" %> more...</div>
When i set the value for accounts manually in redis without using the initializer, everything works fine. But when i add the initializer, i get
ActionView::Template::Error (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.):
I searched for the error. But most solutions are for resque and has something to do with after_fork. Since i am new to Rails and Redis and since i am not using Resque i am getting a little confused. Please help me out on this.
In forked environments like pushion passenger, we have to reconnect to redis whenever a worker is forked. My biggest confusion was where to put the reconnection statements. In many blogs it was suggested to put it in config/environments.rb. But it didn't work for me.
I added
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
$redis.client.disconnect
$redis = Redis.new(:host => 'localhost', :port => 6379)
Rails.logger.info "Reconnecting to redis"
else
# We're in conservative spawning mode. We don't need to do anything.
end
end
end
to config/initializers/redis.rb
and everything started working fine.
This is my first experience with redis, I am implementing autocomplete on the search form with soulmate and redis.
I have installed redis on my local machine and I have to do redis-server to make sure redis is working.
To make it work on heroku I have used redis_to_go and followed the instruction given on the link.
However it seems redis server is not getting started as I keep getting the error Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED).
I have created a redis.rb file in initializer which has the following code :-
ENV["REDISTOGO_URL"] ||= "redis://redistogo:972612d8048aad8#tarpon.redistogo.com:9436/"
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
I am expecting this piece of code to start redis server for me.
What else do I need to do to make redis work on heroku ?
I solved this after going back and forth with Redis Cloud support.
I needed to make a file - config/initializers/soulmate.rb:
Soulmate.redis = ENV["REDISCLOUD_URL"]
Hope that helps someone else!
After banging my head for 2 days I think implementing autocomplete with rails is a poor option in production.
I was able to implement it on my development machine but redis + heroku was a nightmare for me and very poor support by the redis_to_go team.
I have implemented autocomplete search using typeahead.js instead.
Although you've moved on, to answer the original question: I found a discussion about this in the Soulmate issues list: https://github.com/seatgeek/soulmate/pull/20 (extra info: the fix was merged into the gem well before this question was posed).
So, to solve the problem: add 'ENV["REDIS_URL"] = ENV["REDISTOGO_URL"]' to redis.rb.
My own redis.rb now looks like this:
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
ENV["REDIS_URL"] = ENV["REDISTOGO_URL"]
I'm using Redis with split gem in a RoR application hosted on Heroku.
I've configured it with RedisToGo using the following codes:
/config/initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
/config/application.rb
config.gem 'redis'
When I try to
REDIS.set("foo","bar")
on Heroku console, it works fine. It shows Redis ToGo address.
However, when I try to load the application I get the following error:
Errno::ECONNREFUSED: Connection refused - Unable to connect to Redis on localhost:6379
Howcome REDIS is responding correctly, with correct address in Heroku console, but it shows localhost address when the application calls it?
I was able to fix it, and I'll let the solution registered:
I wasn't initializing Split.redis, and therefore it was trying to create a default Redis, which has localhost as host.
So I created the following initializer
/config/initializers/split.rb
Split.redis = REDIS
and then Split could find it!
I've added the Redistogo nano add-on on Heroku and I've tested it out in the console successfully. However when my app tries to connect with Redis I get the following error:
Heroku Log file:
2011-10-12T08:19:50+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - Unable to connect to Redis on 127.0.0.1:6379):
2011-10-12T08:19:50+00:00 app[web.1]: app/controllers/sessions_controller.rb:14:in `create'
Why is it trying to access Redis on localhost?
My Redis.rb in the config/initializers folder has this, which is almost certainly the problem.
#What's pasted below is pasted ad verbatim. I don't know what to change the values to.
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Are you using Resque? If so, you'll need to tell Resque which Redis to use.
Resque.redis = REDIS
If not, then the code you've posted about is NOT setting your REDIS connection up.
Try this:
heroku config --long | grep REDIS
to see what your REDISTOGO_URL is. You might have set it accidentally.