Is it possible to connect single Heroku-RedisToGo from 2 different Heroku instances? - ruby-on-rails

I have one Rails instance and another Node.js instance. The plan is to publish information from the Rails project using Redis's pubsub and subscribe by the Node.js program. The node.js program will publish the information to other clients through web sockets (or long polls).
The whole thing is working perfectly while deployed in combination of remote linux machines. On Heroku, Redis To Go is added as an add-on with the Rails instance. Redis can be connected and working fine from this instance.
But I am not getting how to reach to this Redis add-on from the Node.js instance. I've added REDISTOGO_URL from the environment of Rails instance to the environment of Node.js, but it's not working. Sample errors I'm getting (from logs and REPL) are
redis.set("a","b");
==> Error: Connection in pub/sub mode, only pub/sub commands may be used
redis.psubscribe('*');
==> false
Can somebody please show me a way how to accomplish this?

Yes, all you have to do is set the heroku config value similar to:
'heroku config:add REDISTOGO_URL=[your redis togo url]'
Heroku will then add your config var and restart your app.
You can check to make sure it's added correctly by doing a 'heroku config' on both repositories and comparing them.

Related

Connecting Rails to AWS MySQL database

I've recently created a rails app. I pushed the initial files onto github.
My problem is that I want to connect my rails app to AWS in order to use a MySQL database. I keep seeing tutorials on EC2 and Beanstalk, but I am not sure which one I should use. I have all the drivers needed for ruby through the gem installations.
I'm looking to figure out the main differences between Beanstalk and created a MySQL instance as well as what to put in my database.yml file in my rails app to connect to a database. Thank you in advance!
Just to give an idea, after you provision/create your instance on AWS (EC2 or wherever), you will then push your app's code to that remote server somewhere. You can do it manually via scripts, or you can use Capistrano for this. Once your app is deployed to the server, you need to connect to the server via SSH and manually edit the config/database.yml file to point to the staging/production MySQL database. (I'm generalizing, but I think you just need a step in the right direction.)

How to debug ElasticBeanstalk error "X% of the requests are failing with HTTP 5xx"

My problem is similar to AWS: None of the Instances are sending data but has a slightly different error message.
I have a Rails application running on ElasticBeanstalk, and it appears to be running correctly. Periodically, Enhanced Health Monitoring sends me error messages such as:
Environment health has transitioned from Ok to Degraded. 20.0 % of the
requests are failing with HTTP 5xx.
where the percentage varies up to 100%. Even though I've made no changes, a minute later I get a followup message telling me that everything is back to normal:
Environment health has transitioned from Degraded to Ok.
I've downloaded the full logs from ElasticBeanstalk but I don't know exactly where to look (there are around 20 different log files in various directories).
I'm currently using the free AWS tier with the smallest instances of database, server, etc. Could this be the cause? Which of the log files should I be looking in, and what should I be looking for?
I run rails apps on Elastic Beanstalk and have found it helpful to think about Beanstalk as a computer (in this case an Amazon EC2 instance) running your rails app and a web server (either Passenger or Puma). When you get a 500 error, it could be because your rails app didn't properly deploy–in which case Passenger or Puma will return an error—or your app is deployed properly but encountered an error just like it might on your local machine.
In either case, to diagnose an error, download the full logs from your AWS console (open the correct app environment and then choose Logs > Request Logs > Full logs > Download). Deployment errors are harder to diagnose, but I recommend starting by looking in var-XX/logs/log/eb-activity.log. I suspect your error is coming from your rails app itself, in which case I recommend looking in var-XX/app/support/logs/passenger.log and production.log. To find a 500 error, search for "500 Internal" and then treat the error like you would any other rails error.
You can go to the EC2 instance and run the application just like you would run on your local machine and see the logs.
You can ssh into your EC2 instance using the command eb ssh and go to /opt/python/ directory (It will be different for Ruby or other programming languages).
/opt/python/run is the directory where you will find the version of your application which is run from the EC2 instance. Look for the directory venv and app inside run directory.
Note: The above folder structure is for Python but a similar folder structure post deployment can be found for any other programming language. Just look for the standard directory structure for the deployment environment for your programming language.
For Python:
/opt/python: Root of where you application will end up.
/opt/python/current/app: The current application that is hosted in the environment.
/opt/python/on-deck/app: The app is initially put in on-deck and then, after all the deployment is complete, it will be moved to current. If you are getting failures in your container_commands, check out out the on-deck folder and not the current folder.
/opt/python/current/env: All the env variables that eb will set up for you. If you are trying to reproduce an error, you may first need to source /opt/python/current/env to get things set up as they would be when eb deploy is running.
/opt/python/run/venv: The virtual env used by your application; you will also need to run source /opt/python/run/venv/bin/activate if you are trying to reproduce an error.
I know it is a little late but I wanted to comment the trick I use to find the error, I use to connect via ssh and then, once in the app I try to enter "rails console" It uses to fail, but it shows normally the error you´re making. This little trick saved my life several times. Hope it helps!

Is one rails server per application?

I have two questions about rails server:
Do I have to start the server from within the application folder?
Is the server I started only for that application?
If they are true, this does not quite make sense to me, since why do I need to start multiple servers?
Or is there some kind of master configuration, so that one server can route to different applications? Is Capistrano for this purpose?
I'm going to assume you're talking about the rails server command, for running a local rails server to test your application, and that you're not talking about setting up a rails application on a remote server. Please mention if that is not the case.
Yes, you must execute rails server from within the root folder of your rails application.
Yes, the server you started is only for that application. It's a self-contained thing.
You should not need to start multiple servers. Even if you have multiple applications, you probably don't need to have more than one running at a time. So, you can shut down the rails server in one application (Ctrl-C) and then cd to your new application, and start a new rails server there with rails server.
If you do need to run two local rails applications at once, you can do so by running them on different ports. So, the first one, you can just execute rails server and it will make your site available at localhost:3000 (because port 3000 is the default port). The next one, you can specify a port other than 3000 - eg. rails server -p 3001 to get a rails app at localhost:3001.
Capistrano is for deploying your applications to a remote server, not for running them locally on your own computer. So, it is not relevant here. What you may be interested in is http://pow.cx/
Again, I've assumed you're talking about running your rails app locally on your own computer. If you're referring to deploying it to the internet on a server, then you can ignore this answer.

Should the database.yml be configured when testing locally for Heroku with foreman?

Can someone explain to me what they do when they initialise a rails app locally with foreman (part of Heroku toolbelt) (using postgreSQL), destined to run on Heroku?
I'm going by this guide: developing locally with foreman and what I don't understand is if we are expected to specify database username and passwords or if foreman is supposed to handle it as Heroku itself does?
This perplexes me a little as if we are supposed to modify the database.yml to hook it up to postgreSQL, then what is the point of using foreman instead of rails server?
If it does handle it, how does it handle it, and how would I configure my pg_hba.conf to respect it? Something like local all myuser trust?
Yes, database.yml needs to be configured with valid information for your development and test databases.
Foreman is only running what's in your Procfile, not ripping things out and plugging different things in like Heroku does.
So why do you want to use Foreman instead of rails server? Because it:
Runs all roles defined in your Procfile with one command
Automatically loads your .env
Will fail if any of your roles fail (so less scratching your head because some necessary backend service isn't running)

Deploying Rails App to AWS Elastic Beanstalk, time out

I have an existing app that runs fine on localhost. I tried to deploy it to AWS Elastic Beanstalk today via the step-by-step instructions on this page
After using git aws.push to deploy it onto AWS, I wait for the status of the environment to go to green, and try to access the url it gives me. I get a timeout error after a long while of waiting.
I know this is really not much to go off of, but I'm equally stumped. Is there some kind of server side error log or console log (similar to what I see when I 'rails s' on my computer) so I can see what the AWS environment is writing out? I'm not even sure where to start debugging here.
Your first step is to snapshot your logs from the logs tab of your elastic beanstalk's environment you deployed to. This will give you a snapshot of the recent log activity. If you do need to ssh into the EC2 instance that backs your Elastic Beanstalk instance, it's not immediately evident how to do that. You first need to generate a key pair and download it something like my-key.pem. Then associate that key with the Elastic beanstalk application by editing the configuration and on the first tab, add my-key (note it's just the name, not the actual key). To ssh into your EC2 instance you use ssh -i /path/to/my-key.pem ec2-user#
You get the ec2 public url from the ec2 console. Hope all this helps.
First you'll need to ssh into your server
https://stackoverflow.com/a/4921866/274695
Then, you can look your application logs (passenger.log, production.log) at /var/app/support/logs/

Resources