Ruby on Rails unicorn throws errors - ruby-on-rails

I setup a droplet with Ruby on Rails on digitalocean and I removed the rails directory (App) and replaced it with the one I had locally. It is working fine, if I connect with ssh and go to the IP with the port 3000. However whenever I try to go to port 80, then it throws me an error:
http://prntscr.com/2qoljb
I have checked the logs and it says the following:
Rails Error: Unable to access log file. Please ensure that /home/rails/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
Rails Error: Unable to access log file. Please ensure that /home/rails/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
I, [2014-02-08T18:40:59.192086 #735] INFO -- : worker=1 ready
I, [2014-02-08T18:40:59.198211 #731] INFO -- : worker=0 ready
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:00 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 43ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:01 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 47ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:02 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 2ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:02 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 2ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
The tables should exist, as everything is working completely fine, if I manually type in the IP with port 3000...
Thanks for your help in advance
EDIT
After setting permissions and the owner I am getting the following error instead:
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.3.5/lib/bundler/runtime.rb:216: warning: Insecure world writable dir /home/rails in PATH, mode 040777
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.3.5/lib/bundler/runtime.rb:216: warning: Insecure world writable dir /home/rails in PATH, mode 040777
I, [2014-02-08T19:27:30.231141 #1984] INFO -- : worker=1 ready
I, [2014-02-08T19:27:30.292714 #1981] INFO -- : worker=0 ready

First two lines explicitly talk about wrong file permissions but the Unicorn process shoud run under the same user as log file created by. I guess the capistrano and Unicorn run under different users.
If you are not sure what users should be and you have no problems with security just run:
$ chmod 0666 /home/rails/log/production.log
Once you restart the Unicorn the issue should gone.

I would not care about the problem with the log file in the moment. I do not think that is the root cause.
What catches my eye is Could not find table 'users'. The app does not find a table users in the database or does not find the database at all. Did check your credentials to the production database in config/database.yml? Did you run migrations on the database? Perhaps connect to the production database and check if the schema looks like you expect it to be.

Related

Where do I find the error log of a Rails app on the production server?

My app is working fine on a local server. After deploying it on production (AWS EC2), I see this "classic" Rails error page:
I thought that the errors are logged to the file current/log/production.log, but when I looked in it, there's no error captured. I can only see there the following:
I, [2019-06-09T12:12:04.353438 #12855] INFO -- : Started GET "/constact-us" for 185.44.76.84 at 2019-06-09 12:12:04 +0000
I, [2019-06-09T12:12:04.355034 #12855] INFO -- : Processing by MyAppSite::SiteController#contact_us as HTML
There's logged accessing the URL, but not the error message. Where do I find it? I added some pure HTML/image to that template, so I think the error must be related to some issue with assets (and precompilation).
However, where do I find the full error message?
I am looking to the config/environments/production.rb file and regarding logs, there's "only" this line:
config.log_level = :info
Any advise how to figure out the error message?
EDIT: I just realized that I also have integrated Rollbar to the app and it hasn't caught the error either.
EDIT 2: error from the nginx log:
2019/06/09 13:47:14 [error] 987#0: *7824941 upstream prematurely closed connection while reading response header from upstream, client: IP, server: www.my_website.com, request: "GET /contact-us HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp_production.sock:/contact-us", host: "www.my_website.com
Thank you

Authenticated requests for an Rails API backend and React frontend fail when app deployed to Heroku

I have an app which uses Rails API backend and React frontend. It works locally but after I deployed it to Heroku all the requests that require authentication fail and give
Error: "Token is invalid"
I have checked the headers and the token is there. Can't figure out by looking at Heroku logs what the problem is.
Did someone have a similar issue?
Thanks!
A snippet from Heroku logs:
2017-10-26T17:56:36.419481+00:00 app[web.1]: I, [2017-10-
26T17:56:36.419403 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Parameters: {"auth"=>{}}
2017-10-26T17:56:36.420592+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420511 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] [active_model_serializers] Rendered
ActiveModel::Serializer::Null with Hash (0.18ms)
2017-10-26T17:56:36.421050+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420931 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Filter chain halted as :authenticate_token! rendered or
redirected
2017-10-26T17:56:36.421378+00:00 app[web.1]: I, [2017-10-
26T17:56:36.421175 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Completed 403 Forbidden in 2ms (Views: 1.1ms |
ActiveRecord: 0.0ms)
Heroku has settings where you can define variables. It's possible you have a .env variable or something similar that needs to be defined there, because Heroku isn't recognizing it.

Ruby on Rails, image compilation and precompilation

I just deployed a rails app and I am having an error when I load a page saying:
We're sorry, but something went wrong.
In the production.log I see that message:
Started GET "/" for 128.179.252.130 at 2014-11-30 08:32:28 +0000
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (0.4ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (2.4ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (./icons/icon_nameplate.png isn't precompiled):
37:<ul class="dropdown-menu">
38:
39:<li>
40: <%= menu_dropdown('Profile', "./icons/icon_nameplate.png", "Nameplate Icon", current_user) %>
41:</li>
So I believe the error comes from this part of the message:
./icons/icon_nameplate.png isn't precompiled
I searched the internet, found three possible solution, none of which did work:
1. config.assets.compile = true (in production.rb file)
2. config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] (still in production.rb file)
3. bundle exec rake assets:precompile RAILS_ENV=production
All of this changed absolutely nothing, and the website still shows the same error. So I am turning for you to help.
Thank you very much in advance.
What finally did the trick for me is completely restart the rails app. This is described in the link below.
The recommended version did the trick for me which is:
Create (or update) a file called restart.txt in the tmp folder of your Rails application on the server.
After that no problem at all.
Cheers

Heroku - Application Error

I've created simple application with Ruby on Rails and I’ve tried to commit it on Heroku. I’ve followed this Getting Started on Heroku guide,
I finished it and try to open my page but I still see an Error:
Application Error:
An error occurred in the application and your page could not be
served. Please try again in a few moments.
If you are the application owner, check your logs for details.
Anybody know how to deal with it?
I don’t know what was happen but I’ve done this step, unfortunately I have another problem, I run a few commands:
# git add .
# git commit -m "my commit"
On branch master nothing to commit (working directory clean)
# git push heroku
master Everything up-to-date
# heroku open Opening
http ://eerie-meadow-9207.heroku.com/
# heroku restart
Restarting processes... done
# heroku open Opening
http ://eerie-meadow-9207.heroku.com/
And I see a message:
We're sorry, but something went wrong.
We've been notified about this issue and we'll take a look at it shortly.
From heroku logs [timestamps removed for clarity]:
app[web.1]: Started GET "/" for 77.236.11.34 at 2011-10-31 11:50:38 -0700
app[web.1]: Processing by StoreController#index as HTML
app[web.1]: Completed 500 Internal Server Error in 3ms
heroku[router]: GET eerie-meadow-9207.heroku.com/ dyno=web.1 queue=0 wait=0ms service=13ms status=500 bytes=728
heroku[nginx]: 77.236.11.34 - - [31/Oct/2011:11:50:38 -0700] "GET / HTTP/1.1" 500 728 "-" "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.04 (lucid) Firefox/3.6.23" eerie-meadow-9207.heroku.com
app[web.1]:
heroku[web.1]: State changed from up to bouncing
heroku[web.1]: State changed from bouncing to created
heroku[web.1]: State changed from created to starting
heroku[web.1]: Starting process with command `thin -p 40376 -e production -R /home/heroku_rack/heroku.ru start`
heroku[web.1]: Process exited
app[web.1]: >> Maximum connections set to 1024
app[web.1]: >> Listening on 0.0.0.0:40376, CTRL+C to stop
app[web.1]: >> Thin web server (v1.2.6 codename Crazy Delicious)
heroku[web.1]: State changed from starting to up
app[web.1]:
app[web.1]: Started GET "/" for 77.236.11.34 at 2011-10-31 11:50:59-0700
app[web.1]:
app[web.1]: Processing by StoreController#index as HTML
app[web.1]: Completed 500 Internal Server Error in 4ms
app[web.1]:
app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: relation "products" does not exist
app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
app[web.1]: WHERE a.attrelid = '"products"'::regclass
app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
app[web.1]: ORDER BY a.attnum
app[web.1]: ):
app[web.1]: app/controllers/store_controller.rb:3:in `index'
app[web.1]:
app[web.1]:
app[web.1]: cache: [GET /] miss
heroku[router]: GET eerie-meadow-9207.heroku.com/ dyno=web.1 queue=0 wait=0ms service=81ms status=500 bytes=728
heroku[nginx]: 77.236.11.34 - - [31/Oct/2011:11:50:59 -0700] "GET / HTTP/1.1" 500 728 "-" "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.04 (lucid) Firefox/3.6.23" eerie-meadow-9207.heroku.com
app[web.1]:
app[web.1]:
app[web.1]: Started GET "/" for 77.236.11.34 at 2011-10-31 11:54:00-0700
app[web.1]: Processing by StoreController#index as HTML
I cannot understand it because on my netbook it works on localhost, any ideas?
Try using the command
heroku run rake db:migrate
Try restarting your app with heroku restart. Assuming your app runs fine locally, this should do the trick. It solved the issue for me as I only refactored the schema and the db:reset did not seem to do the trick.
Most of the time heroku issues are due to lack of privileges to access file system. In order for your application to work on heroku you must make sure it wont try to write anything to disk (but in temporary folder).
The typical example is js/css compiling process, e.g. with compass, for which you will find a solution on their knowledge database: http://devcenter.heroku.com/articles/using-compass
You should look further in the heroku logs see if there is any "permission denied" issue at application startup or during the first requests.
http://www.youtube.com/watch?v=p_3dIPgXgkg
If you already have your application built, go to the point in the video when he begins pushing the sample app to heroku. Unfortunately heroku returns hard to solve errors when uploading 3.1 apps for the first time. Follow the troubleshooting steps in the video and you'll be fine.
I had this issue when I was starting to create a discord bot and make it online. And I figured out the reason for this was using the node latest current version(v17.1), I wasted about 2 days for this and tried downgrading node version to v16.7.And to my surprise it worked!.
So I recommend downgrading to v16.7 or the latest LTS version when you are checking this and do install the current latest version
https://nodejs.org/ca/blog/release/v16.7.0/
Here's the full code
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// WShen the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
const http = require('http');
const PORT = process.env.PORT || 3000;
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Bot is up and running');
}
const server = http.createServer(requestListener);
server.listen(PORT);
setInterval(function() {
http.get("http://<your app name>.herokuapp.com");
}, 300000); // every 5 minutes (300000)
Note:Last 3 lines of this code is completely additional and what they do is stop heroku from putting app to sleep every 30 minutes
I had the same problem. Have a look at your Procfile and check if you have added the proper command in it such as web: python run.py. I wasn't able to connect then I placed the command heroku logs --tail to see what was the problem and I saw that within my Procfile I had a type mistake such as >>>web: oython run.py<<< making the app not work. So, pay attention to it. command: $echo web: python run.py > Procfile
Just make sure that in the data base, you have allowed "any IP Address" to connect. This is called whitelisting the IP Address. Example: 0.0.0/0. This is because Heroku, each time it spins up, uses a different IP address. But you can change it later using Heroku docs and the docs of your Database service provider.
https://i.stack.imgur.com/Ud2VJ.png
You can use this image as an example which is a screenshot of MongoDB Atlas
Before you start please allow access to your DB network from anywhere.
Well I have also faced this "Application error" problem once
that time I used these command lines
git init
git add .
git commit -m "my first commit"
heroku create YourAppName
git push heroku main
But as you know I got the "Application error"
Then I used these commands
heroku config:set NPM_CONFIG_PRODUCTION=false
git add .
git commit -m "my first commit"
git push heroku main
After that "Application error" problem was solved.
Well, I'm not sure if it works for you or not but I guess you can try at least.

Heroku: see params and sql activity in logs?

When i view my heroku logs on the server (with heroku logs --tail --app myapp) i see something like this:
2011-06-21T14:09:25+00:00 app[web.1]: Started PUT "/reports/19.xml" for 77.89.149.137 at 2011-06-21 07:09:25 -0700
2011-06-21T14:09:25+00:00 heroku[router]: PUT myapp.heroku.com/reports/19.xml dyno=web.1 queue=0 wait=0ms service=7ms status=401 bytes=28
2011-06-21T14:09:26+00:00 heroku[nginx]: PUT /reports/19.xml HTTP/1.1 | 77.89.149.137 | 656 | http | 401
While in my local log i'd see something like this:
Started PUT "/reports/19" for 127.0.0.1 at 2011-06-21 15:27:01 +0100
Processing by ReportsController#update as XML
Parameters: {"report"=>{"workflow_status"=>"3"}, "id"=>"19"}
Person Load (0.9ms) SELECT "people".* FROM "people" WHERE "people"."email" = 'madeupemai#lkklj.com' LIMIT 1
Report Load (0.4ms) SELECT "reports".* FROM "reports" WHERE "reports"."id" = 19 LIMIT 1
DEPRECATION WARNING: Object#returning has been deprecated in favor of Object#tap. (called from update_report at /home/max/work/rails_apps/flamingo_container/flamingo/vendor/plugins/resource_this/lib/resource_this.rb:135)
Creating scope :open. Overwriting existing method Task.open.
Task Load (2.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 14 LIMIT 1
Completed 200 OK in 1648ms (Views: 568.2ms | ActiveRecord: 3.2ms)
Ie with a lot more information, particularly the params, info from the router, generated sql, any templates rendered etc etc.
Does anyone know how i can get my heroku log to be as verbose as my development one? I've done the following already:
1) Set the log level in the relevant (rails 3) environment file:
config.log_level = :debug
2) Told heroku to use a different logger level, as described on http://devcenter.heroku.com/articles/logging
heroku config:add LOG_LEVEL=DEBUG --app myapp #from CLI
Neither has made any difference...any ideas anyone?
thanks, max
You're essentially wanting to show the SQL / params output in the Heroku logs. You can do this by adding the line shown below to the config block within your production.rb file:
MyAppNameHere::Application.configure do
# add this line
config.logger = Logger.new(STDOUT)
end
By the way, setting the log level to debug just means that Rails.logger.debug will output to the logs when you're on Heroku
In your production.rb add config.log_level = :debug and redeploy. That will give you the same logs as development mode :)
The detailed log you want is generated by the function start_processing in log_subscriber.rb.
action_controller/log_subscriber.rb:
def start_processing(event)
payload = event.payload
params = payload[:params].except(*INTERNAL_PARAMS)
info " Processing by #{payload[:controller]}##{payload[:action]} as #{payload[:formats].first.to_s.upcase}"
info " Parameters: #{params.inspect}" unless params.empty?
end
I checked with rails 3.0.4 in development and production environment. In both environments, we have the detailed logs.
This is an info level log. That's why the debug log level is not changing the output.
I installed the plugin used by heroku rails_log_stdout (Heroku logging) but I still have the desired output.
Right now, I can't test with heroku to find out why you don't have all the logs.
In the heroku example (see Heroku logging, section log retrieval), we don't see the "Processing" and "Parameters" lines. I think that either this method is not called when the app is running on heroku (it is somehow disabled) or heroku skips logs starting with whitespaces. Could you try to log messages starting with whitespaces and see if heroku is showing them?
Rails doesn't generate those logs when in production mode. http://groups.google.com/group/heroku/browse_thread/thread/d778fafedc9a378a
The real problem is actually due to the way Heroku works in conjunction with rails. The right way to solve this is to add: gem 'rails_12factor', group: :production
See https://devcenter.heroku.com/articles/rails-integration-gems for more info on the matter
Looks like the puma server doesn't play well with Heroku. In my project, I tried everything, but it still wouldn't log. I then replaced Puma with Unicorn, and bam, full logs are being shown.

Resources