Rails send_file not working with files larger than 400mb - ruby-on-rails

I used Helicon Zoo to set up a rails application on a Windows Server 2008 machine.
My problem is downloading files above 400MB.
In my rails app I use the following to send files to a client:
app/controllers/hosted_files_controller.rb
class HostedFilesController < ApplicationController
before_filter :authenticate_user!
around_filter :catch_not_foun
def download
#footprint = UserAbility.new(current_user).footprints.find(params[:id])
send_file path
end
private
def path
if #footprint.subpath?
#path = "#{HOSTED_FILES_PATH}\\#{#footprint.subpath}\\#{#footprint.filename}"
else
#path = "#{HOSTED_FILES_PATH}\\#{#footprint.filename}"
end
end
def catch_not_found
yield
rescue ActiveRecord::RecordNotFound
recover_and_log "We couldn't find that record.", "No record found using the id (#{params[:id]})"
rescue ActionController::MissingFile
recover_and_log "We couldn't find the file on our server.", "The file was not found at the following path: #{#path}"
end
def recover_and_log (displayed, logged)
logger.info "!!! Error: #{logged}"
redirect_to root_url, alert: displayed
end
end
I have config.action_dispatch.x_sendfile_header commented out in the production.rb file since I am not using Apache or Nginx.
This works great for all the files on the server that are below ~400MB. After I get above it, I get a 500 internal server error from Helicon Zoo that says the following:
Helicon Zoo module has caught up an error. Please see the details below.
Worker Status
The process was created
Windows error
The pipe has been ended. (ERROR CODE: 109)
Internal module error
message: ZooApplication backend read Error.
type: ZooException
file: Jobs\JobBase.cpp
line: 566
version: 3.1.98.508
STDERR
Empty stderr
Does anyone have any idea what is going on? I'm at a loss.
I've tried:
increasing the buffer_size on send_file (didn't work)
play around with memory settings in IIS for the application pool (didn't work)
change x_sendfile_header to X-Sendfile and X-Accel-Redirect (didn't work)
I'm considering trying to install Apache on the Windows Server and using the x_sendfile_header to offload sending the file to Apache, but I'm afraid of messing up the already (almost) working application.
Does anyone have any ideas of how to fix this?

By default with current version of Helicon Zoo Ruby applications are installed as FastCGI Ruby Rack connector. Since FastCGI protocol is a blocking protocol it may have some limitations on request timeout or request max size. If you need to send large files I suggest you to go "Ruby 1.9 Rack over HTTP with Thin" route instead. I suppose you've been following Ruby on Rails (2.3.x and 3.x.x) instruction. Now just follow additional steps from Ruby Rack over HTTP with Thin instruction, like running "gem install thin" command and editing web.config as follows:
In the < handlers> section comment out two lines that follows
< !-- Ruby 1.9 over FastCGI -->
Uncomment two lines that follows
< !-- Ruby 1.9 over HTTP, using Thin as a back-end application server -->
In the <environmentVariables> section uncomment line
< !-- Use this APP_WORKER with HTTP Ruby engine and Thin. Thin need to be installed.
< add name="APP_WORKER" value="GEM_HOME\bin\thin start" />
Another solution, since you already using Helicon products, would be installing Helicon Ape that provides support for X-Sendfile HTTP header (see documentation) header as in Apache and is free for several sites per server. This solution would be even better since low level WinHTTP code will be used to send data, which will decrease load to the server and improve response speed.

Related

Rails Production: Premature end of script headers

I'm attempting to host my rails app on a vps. The issue that I'm running into is when I try to navigate to my rails app, I get the following error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
When I check out the virtual server's error log it tells me the following:
Premature end of script headers:
And sometimes that's followed with a feed or a contact or a referer: myurl.com/ or nothing at all.
I've seen plenty of posts regarding this error messages elsewhere but none so far have resolved the matter. The general consensus seems to be that it likely has to do with the rails environment variable or file.
As this is my first attempt at putting a rails app on a production server, I figure this is plausible. Below is my environment.rb
cat config/environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
As for the server, I'm using Ubuntu 12.04 with Webmin/Virtualmin and apache2 with passenger. Ruby 2.1.5p273 was installed using RVM and all gems are up to date.
Any thoughts would be helpful!
Today I was confronted with exactly the same problem. In my case it was the line 'secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>' in config/secrets.yml (but the env variable was not defined on the server). Please, look in §3.3 of http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html.

Unable to publish message with Faye Ruby client

First off, I've already look at:
faye ruby client is not working
I tried the recommendations and its still not working for me.
Heres my code:
def broadcast(channel, data=nil, &block)
return if Rails.env.test?
if data.nil? && block_given?
data = capture(&block)
end
client = Faye::Client.new(APP_CONFIG['faye_url'])
client.publish(channel, data)
end
I tried using Net::HTTP.post_form and the server froze with no errors or warnings or anything. I've tried putting it into an EM.run block with no luck. I can publish to Faye with curl just fine and its sent on to subscribers but for some reason the ruby client just isn't working.
I'm using faye-rails, ruby 1.9.3 and rails 2.3.13.
The server is behind nginx, I tried both the ngnix ip/port and the thin ip/port. Still didn't work.
It works fine in development, just not in production.
Update:
I disabled both WebSockets and EventSource to force it to use long polling so it would work through ngnix without any errors.
It is also running as rack middleware so it shouldn't need any additional ports.

Rails application issue after deployment - 404 error

I'm new to RoR.
I was able to install Rails and host it in Webrick (Sample App with "Welcome" controller) in my windows.
Now i have a Unix Weblogic Server along with a dedicated domian.
After exporting the .WAR file using Warbler, i accessed the Oracle Admin Console from where i deployed the .WAR file in the dedicated domain. I did all this for the Sample app with only the Welcome controller in it.
But even after deploying the WAR file, on accessing the Domain along with the Port Number (:9002) i ended up with 404 file not found error On looking at the server logs,there wasn't any records relating to any error. The Application must have been deployed properly. I assume that i must have missed out on some basic configurations in the routes.rb or similar files before deploying. Can anyone Guess what are all the possibilities and if possible can anyone help me by pointing to any tuts that cover the Steps to be carried out for configuration before deployment. do i need to install both JRuby and Rails inside the server before depolyment?
I can't really guess with Eror 404 only.
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
map '/mydepartment' do
run Myapp::Application
end
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
One more thing you also add this to your config/environments/production.rb and config/environments/development.rb (if on production mode):
config.action_controller.asset_path = proc { |path| "/abc#{path}" }
otherwise when you call your helpers such as stylesheet_link_tag in your views, they will generate links without the "/abc".
Also, find some guides you may refer for good support.
JRubyOnRailsOnBEAWeblogic.
Use JRuby with JMX for Oracle WebLogic Server 11g
Let me know if it is not resolved.

Context not being set properly with warbler and tomcat

I'm trying to deploy a Rails app to Tomcat with a war file generated by Warbler. The war file deploys to /myproject-rails-gui without any problems but, when I try to access a page on the app, I'm getting:
ArgumentError: wrong number of arguments (1 for 0)
send at org/jruby/RubyKernel.java:2097
Railtie at /home/myproject/apache-tomcat-7.0.22/webapps/myproject-rails-gui/WEB-INF/gems/gems/actionpack-3.0.10/lib/action_controller/railtie.rb:54
It's failing when it tries to set relative_url_root. There are other Stack Overflow articles that (correctly) point out that relative_url_root is deprecated and you should set the RAILS_RELATIVE_URL_ROOT environment variable instead.
From the Rails project on GitHub
module ActionController
class Base
# Deprecated methods. Wrap them in a module so they can be overwritten by plugins
# (like the verify method.)
module DeprecatedBehavior #:nodoc:
def relative_url_root
ActiveSupport::Deprecation.warn "ActionController::Base.relative_url_root is ineffective. " <<
"Please stop using it.", caller
end
def relative_url_root=
ActiveSupport::Deprecation.warn "ActionController::Base.relative_url_root= is ineffective. " <<
"Please stop using it.", caller
end
I haven't had any luck figuring out how to do that using Warbler and Tomcat, though. Any suggestions? For what it's worth, the app works fine when I run it in the root context.
My environment:
Warbler 1.3.2
Tomcat 7.0.22
JRuby 1.6.5
Rails 3.0.10
After a lot of digging, it looks like this particular exception was being caused by a missing parameter in ActionController::DeprecatedBehavior.relative_url_root= in Rails v3.0.10. Miguel, your issue might be related but, if you're seeing it in Rails v3.1.1, then it's slightly different. The file that I had to modify to fix it doesn't exist in v3.1.1.
I filed issue 3645 in the Rails project on Github, fixed it in a fork, and issued a pull request to them. Hopefully this will be fixed in a new release of Rails 3.0.
In the meantime, if you want to use my fixed version, it's available at https://github.com/mhuffnagle/rails/tree/3-0-stable.

Rails sends 0 byte files using send_file

I can't get send_file(Model.attachment.path) to work.
It doesn't fail, instead, it sends a 0 byte size file to the client, the file names are correct though.
This problem started happening after I did a big migration from Rails 2.3.8 to 3.
There were a lot of other things that took place in this migration and I will try my best to detail all of them.
Distrubution change/Server Change. Rackspace RHEL5 to Linode Ubuntu 10.04LTS
Ruby version change, 1.8.6 -> 1.9.2
Rails version change, 2.3.8 -> 3.0.0
httpd platform change, apache2 -> nginx (However I tried on apache2 as well and it did not work).
I moved the attachments via ftp as they were not part of my git repositories so they were published via cap deploy, instead manual ftp remote(RHEL5) to local(Win7) then local(Win7) to remote(Ubuntu10).
I do know that FTPing does not retain the file permissions through the transfers, so what I've also done is mimicked the chmods that were seen on my previous servers so they are almost identical. (users/groups are different, set to root:root instead of olduser:olduser).
A snippet of the request to download a attachment from my production log.
Started GET "/attachments/replies/1410?1277105698" for 218.102.140.205 at 2010-09-16 09:44:31 +0000
Processing by AttachmentsController#replies as HTML
Parameters: {"1277105698"=>nil, "id"=>"1410"}
Sent file /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc (0.2ms)
Completed 200 OK in 78ms
Everything's okay. Let me also rule out local issues, I've tried downloading via Chrome on both Win7 and Ubuntu (on Vbox).
Let me also assure you that the path is indeed correct.
root#li162-41:/srv/app/current# tail /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc
#
#
%17nw
HQ��+1ae����
%33333333333(��QR���HX�"%%��#9
��#�p4��#P#��Unknown������������G��z �Times New Roman5��Symbol3&�
�z �Arial5&�
So to sum up the question, how do I get send_file to actually send files instead of fake 0 byte junk.
send_file has :x_sendfile param which defaults to true in Rails 3.
This feature offloads streaming download to front server - Apache (with mod_xsendfile) or lighttpd, by returning empty response with X-Sendfile header with path.
Nginx uses X-Accel-Redirect header for same functionality but you have to
configure Rails properly in proper environment file:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Rails 3 update: this line already exists in production.rb, just uncomment it.
Add sendfile on; to your nginx config to utilize header sent by Rails.
Remember the absolute path must be used and nginx must have read access to file.
Another way for aliased files:
For better security I use aliases in nginx instead of absolute paths,
however send_file method checks existence of file which fails with alias.
Thus I changed my action to:
head(
'X-Accel-Redirect'=> file_item.location,
'Content-Type' => file_item.content_type,
'Content-Disposition' => "attachment; filename=\"#{file_item.name}\"");
render :nothing => true;
In Rails 3, just uncomment the line config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' in production.rb inside environments folder.
Yes, I had the same problem with X-sendfile being enabled by default in Rails 3 too.
If you have large volume of "send_file" calls,
you can just comment-out following line in config/environments/production.rb:
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
Then send_file method started working perfectly.
Because I can't install x-sendfile extension to Apache, I just searched a little and found this.
I hope it helps.
I've had similar issues with send_file() in the past, using send_data() instead saved me back then (e.g. send_data File.read(filename), :disposition => 'inline', :type => "some/mimetype")
On Rails 4, I realize my problem is that I deleted the temporary file which I've generated to send to user.
If I didn't delete the file, send_file works. I've not tested on thin but it works great on Passenger 5 as stand-alone server.

Resources