I’ve been searching for a solution for attachments in MongoMapper + Sinatra… and CarrierWave (http://carrierwave.rubyforge.org/) looks awesome. Just need a spare hour or two to start playing…
I’ve worked with spreadsheets a fair bit - both into and out of Ruby and (cough!) PHP apps… and this looks like a seriously neat library for using Google, Excel (incl. .xlsx) and OpenOffice spreadsheets in Ruby.
Props to Ryan Bates for the tweet: http://twitter.com/rbates/status/8969980156
After Ryan Bates’ really awesome Railscast I figured it was time to get delayed_job happening in my application to process asynchronous stuff – in this case, delivering email “notifications”. If you want to know more about DJ, the Railscast serves as a great starting point.
DelayedJob is awesome, and comes with a rake task that spools up and runs the jobs. What I needed was a way to have Ubuntu control DJ – in particular, to automatically start up if the system has been rebooted… and to give me a quick easy way of probing for the status… and this is where God comes in – the pure ruby monitoring system that it’s author, Tom Preston-Werner describes as “like Monit, only awesome”.
For this I’m standing on the shoulders of giants – check out the links below to the places from which I’ve learned and obtained the DJ-fu.
There’s no scope here to go through how DJ works. A good starting point is the DJ config that the github guys use. Suffice to say… if it’s all apples, you should be able to run the rake task and get it to successfully execute:
rake jobs:work
sudo gem install god
For this I’m massively indebted to Tim Riley’s very cool post & script.
Start with a config file – we’ll put it in Ubuntu’s preferred place: /etc/default/god. This script is insanely simple – just sets a var that God uses to find our delayed_job config.
#config for GOD - I point it to a file in my RAILS_ROOT
GOD_CONFIG=/path/to/rails/config/delayed_job.god
And then get an init script – at /etc/init.d/god. This, adapted from Tim Riley’s one (see the link above), should do the trick – it points back to our file at /etc/default/god and sticks its logs and pids in Ubuntu-friendly places:
NAME=god DESC=god
set -e
test -x /usr/bin/god || exit 0
test -f /etc/default/god && . /etc/default/god [ $GOD_CONFIG ] || exit 0
. /lib/lsb/init-functions
RETVAL=0
case “$1” in
start)
echo -n “Starting $DESC: ”
/usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log
RETVAL=$?
echo “$NAME.”
;;
stop)
echo -n “Stopping $DESC: ”
kill cat /var/run/god.pid
RETVAL=$?
echo “$NAME.”
;;
restart)
echo -n “Restarting $DESC: ”
kill cat /var/run/god.pid
/usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log
RETVAL=$?
echo “$NAME.”
;;
status)
/usr/bin/god status
RETVAL=$?
;;
*)
echo “Usage: god {start|stop|restart|status}”
exit 1
;;
esac
exit $RETVAL
This ensures that when the system starts up it’ll kick off our DJ task.
sudo update-rc.d god defaults
sudo /etc/init.d/god start Starting god: god.
Checking the status should be equally simple.
sudo /etc/init.d/god status dj: dj-0: up
That should have you up and running…
One of the great things about Rails is that of course it automatically does logging… and has different levels of logging for each environment.
Despite this, I had a couple of gripes: 1. The logger doesn’t have a formatter - i.e. in particular, it doesn’t include timestamps. 2. It doesn’t automatically rotate - and the development log can get MASSIVE (I’ve often had them 5GB+)
Additionally, I thought it’d be great if I could separate logs from ActiveRecord and ActionController.
Thankfully, Rails lets us choose the logger that we want to use. We’ll create our own and ask Rails to use it.
We’re using the default ruby Logger (see “http://ruby-doc.org/core/classes/Logger.html”:http://ruby-doc.org/core/classes/Logger.html).
In environment.rb:
active_record_logger = Logger.new(File.join(RAILS_ROOT, "log", "\#{RAILS_ENV}_active_record_log.log"), 'weekly')
action_controller_logger = Logger.new(File.join(RAILS_ROOT, "log", "\#{RAILS_ENV}_action_controller_log.log"), 'weekly')
active_record_logger.formatter = action_controller_logger.formatter = Logger::Formatter.new
Some notes and options here: * We’re putting the log file in the default location (RAILS_ROOT/log/) * This will still produce separate logs for each environment (dev, test, prod, etc) * This will do weekly rotation - but you could easily switch to ‘daily’ or ‘monthly’ * We can also configure it to a maximum number of old logs + maximum size - see “the docs”:http://ruby-doc.org/core/classes/Logger.html#M001002 for more info.
In the initializer block in environment.rb…
Rails::Initializer.run do |config|
config.active_record.logger = active_record_logger
config.action_controller.logger = action_controller_logger
#...
end
That should be it. Enjoy.
Being an Ubuntu user, and not a staunch advocate of one of the main text editors (Vim, Emacs), I thought I’d have a go at installing RubyMine. They’ve recently been advertising everywhere in the Ruby world (RailsEnvy, Railscasts, etc)
Installing is fairly straightforward.
1. Get the app and a trial / real installation key. The latest version when I grabbed it was v1.1
2. Unpack it, and move it to /opt/rubymine
tar -xvzf rubymine-1.1.tar.gz
(tar output... extracted a folder named rubymine952)
sudo mv rubymine942 /opt/rubymine
3. Sort out java.
sudo apt-get install sun-java6-jre sun-java6-sdk
Rubymine expects the java executable to be at /bin/java, but Ubuntu has it at /usr/bin/java
sudo ln -s /usr/bin/java /bin/java
4. Add a launcher. I’m adding it to the top panel. Right-click on the panel and choose “Add to Panel”.

There’s an icon at /opt/rubymine/bin/rubymine.png

… and the application is /opt/rubymine/bin/rubymine.sh,
Now to see if RubyMine is any good!