How can I use Mongrel for my non-rails ruby webapp? - ruby

I've got a non-RoR but ruby webapp in the works.
So far, I've just been using requests, hacks, and mod_ruby; but I'd really like to try out Mongrel--which seems to work really well for RoR sites.
I looked at the examples given in GitHub, and none of them does dynamic changing of content w/o having to restart Mongrel like you can do in RoR under dev mode.
How can I do this? (Just using load doesn't work.)

I use Sinatra with a Mongrel backend; hello world really is as easy as it says on the front page. I actually started with Sinatra, and then changed the server from webrick to mongrel after experimenting around with which gave better performance.
See this question for how that testing worked out.

Related

Front end separated from back end project using Sinatra

I am planning to create a website that has front end and back end. I was wondering how the FE communicates with the BE.
I saw a project that uses Xampp to run the front end and sinatra for the back end. It needs to start apache, then the backend is fired using the rackup command. I assume the backend runs under Webrick.
Can someone explain how these two ends communicate with each other? If there is a good tutorial for this, I will appreciate it.
Sinatra is a popular option for API-only backend applications. We use Grape with Sinatra or just Sinatra without any dependency.
I have two app examples that can help you:
https://github.com/katgironpe/sinatra-grape
https://github.com/katgironpe/simple-sinatra-mvc
Webrick or Puma can be used with Sinatra, but it's not impossible to run a Ruby app on XAMPP. I did that several years ago. The front-end is probably just consuming the Sinatra API.
You can use Ember CLI project. It can get complex with other options like Angular.js and React.js. Or if you like, just use jQuery.

Running a Ruby script via the web

I have a small Ruby script that I want to run by visiting a page in a browser. This might seem like a dumb question but what's the easiest way to accomplish this? I haven't found very good documentation on how to get Ruby working with Apache so I'm open to any suggestions at this point.
I suggest Sinatra. As shown on that page, it's very lightweight, and Apache is not even necessary. As you get to needing more performance you might use Nginx or Apache as a reverse proxy (serving your static files quickly) and something like Thin to make your application run faster.
But for now, just start using Sinatra. As shown on the home page, you can get started in just a few lines.

Why is my sinatra website so slow?

After asking this question, I started using Sinatra as a way to serve web pages.
This evening, a friend of mine and I started to test the speed of the server.
The file to log in looks like:
require 'rubygems'
require 'sinatra'
require 'haml'
enable :sessions #for cookies!
get '/' do
haml :index
end
And the index.haml looks like:
%title
First Page
%header
%h2 First Page
He's sitting on a recent laptop, as am I, with an Apple 802.11n router between the two of us. We're both running Windows 7. I've also tried these same files on a laptop running Ubuntu 9.10 x64 with Sinatra and all relevant files installed from apt-get.
Sinatra is taking 7 seconds to serve up a single page request, no matter the server OS, Windows or Linux. I see that here the author managed to get over 400 requests/second processed. What gives? (or should this be on SuperUser or the like?)
I'll set aside any opinions on when you should optimize your web application.
Set up different configurations in your Sinatra app for development and production because some of these suggestions, you won't always want to use. In fact, you should probably go ahead and setup and environment similar to how you would deploy in production. You would not deploy by simply running ruby app.rb. You'd want to put apache or nginx in front of your Mongrel. Mongrel will serve up your static files, but that's really only advisable for development mode. In deployment, a web server is going to do a lot better job for that. In short, your deployed environment will be faster than your standalone development environment.
At this point, I wouldn't worry about Mongrel vs. Thin. If Thin is twice as fast - it isn't - then your 7 seconds becomes 3.5. Will that be good enough?
Some things to try ...
I know I just told you to set up a deployment environment, but maybe it's not the server side. Have you tried running YSlow or PageSpeed on your pages? I/O is going to take up more of those 7 seconds (Disclaimer: I'm assuming that there's nothing wrong with your network set up) than the server. YSlow - Firebug actually - will tell you how long each part of your page takes to get to the browser.
One of the things that YSlow told me to do was to put a far forward Expires header on my static assets, which I knew but I was leaving optimization until the end. That's when I realized that there were at least 3 different places that I could specify that header. I'm convincing myself that doing it in nginx is the right place to put it.
If you're happy with those results, then you can look at the server. Off the top of my head, so not exhaustive
Turn on gzip responses.
Combine your stylesheets so there's only one per page request. There may be some Rack Middleware for this, if you don't do it manually.
Cache. I'm trying Rack::Cache.
Use sprites to decrease the number of image downloads you use.
Minify your Javascript. Again, maybe via Rack Middleware.
Rack Middleware is neat, but it uses CPU. So, manually minifying your Javascript adds a new step to your workflow, but on the server, it's faster than Middleware. It's a tradeoff.
Sorry if this was rambly.
I had this problem when running Sinatra with shotgun but not when running my app directly (i.e., ruby -rubygems app.rb). This is because shotgun forks and reloads the application for each request.
I found a thread in Sinatra's mailing list which discussed this issue and people there advised using rerun instead of shotgun. I'm happy to say it solved this issue for me.
Try using Thin as the server. I noticed an increase in performance compared with WEBrick and Mongrel.
gem install thin
When you run your app using ruby TestServer.rb you'll see the following:
Sinatra/0.10.1 has taken the stage on 4567 for development with backup from Thin
I'm running Sinatra inside VMWare Fusion with Vagrant. My app was running slowly (about ten seconds to service a request). Then I found this gem:
Webrick is very slow to respond. How to speed it up?
It seems that WEBrick was (by default) configured to reverse dns lookup on every request, and that was slowing it down.

Ruby (off the Rails) Hosting

Many people have asked about Rails hosting on this site, but I'm not familiar enough with the back end of things to know if there's a difference.
I want to host some Ruby CGI 'webservices', basically just ruby methods that take parameters from a POST request, access a MySQL db and return data.
I've looked at RoR and it seems like overkill for this, from what I can tell it's for speeding up the development of data baesd CRUD sites, which is not at all what I'm doing.
So my question is, does this affect the hosting provider I choose? Does anyone recommend a good Ruby host for CGI operations? I'm not familiar with FastCGI, mod_ruby, Passenger, Mongrel etc. and what they mean for performance, scalability etc. I just want to host my ruby scripts with reasonably good performance, and all the info out there(and here) seems to be focused on rails.
First, if you want lightweight, Sinatra is usually my first pick. Pair it up with rack and Passenger for best results. It's not CGI, but realistically speaking, CGI is rarely a good match-up with Ruby.
Here's the "Hello World!" Sinatra app from the main page:
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end
Hard to get more lightweight than that.
As for providers, anybody that supports Passenger (mod_rack) should be able to handle Sinatra. I'm a big fan of Slicehost personally, but they're a VPS host, which means you need to be able to install and manage the entire stack yourself. If you don't mind paying a tiny bit extra for the infrastructure, Heroku makes installation and deployment dead simple, so long as your needs don't exceed what they provide (sounds like they won't). In the unlikely event that you're only using 5MB or if you're using an external storage mechanism like Amazon RDS, Heroku may actually be free for you.
Update:
Passenger is an Apache module that allows Rack applications to be run inside of Apache.
Rack is a middleware layer that separates the web server and the web framework from each other. This allows web frameworks to run on any web server for which there is an adapter.
Sinatra is a lightweight web framework that runs on top of Rack.
Once Passenger and Rack are installed (gem install rack, gem install passenger) you just need to edit the Apache vhost to point at the config.ru file for your Sinatra app and create the required directories as per the Passenger docs and you'll be good to go.
I think you might want to look into Rack. It allows you to do the kinds of things you're talking about and shrugs off the weight of frameworks like Rails or Merb. Rack applications can be hosted at a place like Heroku.

JRuby / Rack deployment

I know this is pretty exotic, but I want to deploy a Ruby web application (not Rails, but Rack based, so it plugs into most Ruby servers just fine) using JRuby. Google and friends give me a few success stories, but mostly rails related and if not, no details on the deployment are provided. The framework I'm using is Ramaze, if it matters.
Any help would be greatly appreciated.
In my opinion, running a Rack based application with a rackup script is the real Ruby way. And I wanted to apply the same for JRuby too. That is why I've written jetty-rackup http://github.com/geekq/jetty-rackup
We are using it for deploying a Sinatra web application. No Java specific configuration needed. A typical, small config.ru is enough. Embedded jetty web server is used in place of Webrick then.
This is the "just works" gem for me: https://github.com/matadon/mizuno

Resources