How to use rails 2.0.2 without a database
April 9th, 2008
Step 1
Run the rake task to freeze rails 2.0.2 into the project so your not reading from gems:
rake rails:freeze:gems
Step 2
Remove the database.yml file from RAILS_ROOT/config/
Step 3
Change line 21 in RAILS_ROOT/config/enviroment.rb to:
config.frameworks -= [ :active_record ]
Bobs your uncle, that should be it!
Monitoring Merb Processes in the Wild with God
March 13th, 2008
Before you start reading, this post only applies for Merb 0.5.3, any newer version is totally untested
When you take Merb out into the wild, it does, unfortunatly, suffer a lot of the same problems as the mongrel handler than runs Rails.
There is however a saviour out there – God – To clarify, im not talking about the man upstairs; rather the process monitoring tool which rocks at restarting bloated mongrel processes on *nix based OS.
The Merb handler does not have any way of restarting a running cluster, so you physically have to stop, then start a new merb process. This is somewhat out of sync with how the god process handling and restarting works in that you define start, stop and critically, restart paramaters. To get around this we have to use a hacky sleep then start method – its not ideal, but hey, it works :)
Rather than letting merb handle the process forking, what were going to do is let God handle the writing of Pids and managing of the process. Ok, less of all this talk and lets take a look at some configuration code for the God configuration.
# MIT License
#
# Copyright (c) 2008, Tim Perrett
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall
# be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
SITE_LOCATION = "/var/www/sites/domain.com"
MERB_ROOT = "#{SITE_LOCATION}/current"
MERB_CONFIG = "#{SITE_LOCATION}/shared/config/merb.yml"
MERB_ENVIROMENT = 'production'
PROCESS_USER = 'timperrett'
PROCESS_GROUP = 'timperrett'
%w{4006 4007}.each do |port|
God.watch do |w|
pid_path = File.join(MERB_ROOT, 'log',"merb.#{port}.pid")
w.name = "merb-#{port}"
w.interval = 30.seconds # default
w.start = "#{MERB_ROOT}/script/merb -u #{PROCESS_USER} \
-G #{PROCESS_GROUP} -M #{MERB_CONFIG} -p #{port} \
-e #{MERB_ENVIROMENT} -d"
w.log = "/home/timperrett/godmerb.log"
w.stop = "(cd #{MERB_ROOT}; merb -k #{port})"
w.restart = "(cd #{MERB_ROOT}; merb -k #{port}); sleep 1; \
#{MERB_ROOT}/script/merb -u #{PROCESS_USER} -G \
#{PROCESS_GROUP} -M #{MERB_CONFIG} -p #{port} \
-e #{MERB_ENVIROMENT} -d"
w.start_grace = 5.seconds
w.restart_grace = 20.seconds
w.pid_file = File.join(MERB_ROOT, "log/merb.#{port}.pid")
# w.group = "merbs"
w.behavior(:clean_pid_file)
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 10.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 51.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.transition = :unmonitored
c.retry_in = 5.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
end
Im not sure that this is ideal, but it certainly seems to work for me and importantly keeps the site running without problems – which is a dang sight better than them becoming unresponsive!
I hope this might help someone, somewhere….
Is Rails no longer the flavor of the month?
January 5th, 2008
Firstly, I read this blog post by Zed Shaw. Quite frankly I was disgusted that someone who has given so much to our community has been treated so poorly by prominent figures within our very inner circles and the major vendors. I must point out I do not know Zed, Ive never met him, nor will I likely ever meet him, but yet he helped me many times way back in the early days of mongrel, as im sure he did with countless others – and for that, he gets my respects and applause.
His blog posts about the problems with Rails echoed conversations I have had multiple times of late with various other developers and architects. People feel rails is bloated and has a lot of inefficiencies – which is true, and whilst 2.1 is leaner, it could still do with a good going over at fat camp… That sounds bad I admit, but I am not ignorant of the fact that we do have an awful lot to thank Rails (and DHH especially) for – the ideas behind Rails have changed the way the whole development community look at there working practices from start to finish. The shear magnitude of that is mind-blowing.
Personally, Rails has changed the way I look at code, the way I think and more – this blog is even powered by rails. Despite all that, when I build a Rails project now, I cant help but feel, well, a little bit dirty. Initially in my Rails career I loved the way that the bulk of the complexity was abstracted far far away down the rabbit hole – thats not because I was an incompetent coder – it was just new and exciting, and seemed like such a great idea at the time (like so many other things…). Now however, I feel like we need more; more control, more transparency in our frameworks and tools. Less black boxes and more turner-prize type glass ones!
I ask you the reader one thing: Is it time we said farewell to Rails? Or is it a case of the grass is greener on the other side?
Some of you reading this might be thinking that this is all well and good, but what the hell are we going to use to build our projects. Well, I have been putting a lot of time into the Merb framework recently, and it is, categorically, awesome, not to mention thread safe ;) Working with it feels just, well, better. Ezra has crafted a great framework that has taken heed of Rails’ shortcomings, that my friends can only be a good thing…
Anyway, I’ll be posting some Merb tutorials up here soon, so I hope you will give it a try and let me know how you get on.
Happy coding guys, and farewell Rails, its been emotional!
MetaProgramming with Ruby
December 22nd, 2007
Of late of been writing some nice gems makings heavy use of meta programming. If anyone is looking at doing something similar, I would high recommend you check out these resources:
http://www.infoq.com/presentations/metaprogramming-ruby
http://www.rubyplus.org/
The talk about self and its meaning was extremely interesting - its really surprising how flexible it can be in the right hands :)
Ruby SMS gateway client for sign-up.to
October 5th, 2007
Well, I finally got round to getting my own RubyForge project! Its been a while coming now, but I decided to release some of the work ive done using the (SignUpTo)[http://sign-up.to] SMS gateway as a gem :)
Its still very early stages, as i only needed something rough for the project I was working on, however i put it out anyway – its rough but it does work I assure you! lol
You can check the project page at (signupto.rubyforge.org)[http://signupto.rubyforge.org] and download the source files (here)[http://rubyforge.org/frs/?group_id=4542].
Otherwise, if you wanna install via gems, just do the usual:
sudo gem install signupto
So how do I use it?
For this example, we’ll use rails, at thats probally where people might use it most often… In your boot.rb do
gem 'signupto'
Then require the files in your enviroment.rb
require 'net/sms/signupto'
That will then let you use the classes anywhere in your app :) such as
account = Net::SMS::SignupTo::Account.new(‘YOUR ACCOUNT HASH’, ‘CUSTOMER’)
message = Net::SMS::SignupTo::Message.new(RECIPIENT_NUMBER,
’NAME OF SENDER (max 12 chars)’,
‘BODY OF MESSAGE, NON-URI encoded’)
send = Net::SMS::SignupTo::Send.new(account, message)
The SMS’s should then just fly out the door! Apologies as I know this is a very rushed first post about this, but I have places to go and people to see ;) I’ll be bolstering the unit tests and examples soon. Stay tuned people!!
Running Pound reverse proxy on windows server 2003
September 22nd, 2007
Pound is the unix reverse proxy software popularized by Rails deployment over the past 8-12 months. Pound is a unix tool, and was never meant to run under windows - it is however possible to make it run under windows if you package it up with some key DLL files from cygwin :)
I must stress here that this is not necessarily the best thing to be doing, this post is more about how to do it if you really want too rather than a "do this, I really recommend it" type post. There are also some issues with pound and its behavior with X-FORWARDED_FOR headers, but i wont bang on about that here; refer to google for more info. Anyway... back to the post.....
Take the latest pound distro from here then compile it from within Cygwin like so
./configure --without-ssl --disable-log --disable-dynscale
then
make all
Then you should be left with a working binary of pound within your build directory. In order to then make this work in a standalone fashion (I use the term standalone loosely as its still using the cygwin environment) you need to package several DLL files from cygwin:
cygssl-0.9.8.dll (required)
cygcrypto-0.9.8.dll (required)
cygwin1.dll (required)
pound.exe (required, should be fairly obvious!)
cygrunsrv.exe (optional, include only if you want to install pound as a service)
with these files you can then run pound, and even install it as a service on the host machine. If you do want to install the service, run the following command:
cygrunsrv --install Pound --path X:\applications\pound-2.3.2\bin\pound.exe --args "-f X:\applications\pound-2.3.2\config\pound.cfg" --stdout X:\applications\pound-2.3.2\log\pound.log --stderr X:\applications\pound-2.3.2\log\pound.log
Where "X:\applications\pound-2.3.2" is the real path to where ever it is on the file system you chose to install.
If you don't want to compile your own, then you can download my version of pound 2.3.2 for windows here. It goes without saying I take no responsibility for any problems you might run into by using my compiled version; it worked for me thats all I can say!