Manage cron from rails

I needed to setup some caching and database updating that will run once a day on our ruby on rails application.

I found this link, which gave three different approaches - all of which I didn't like.

A quick and dirty yet tried and true method is to use cron to call wget on a controller script/runner (thanks David) that does everything we need. All I needed to do was write the crontab entries - but that means later deployments will require someone to do that. My approach was to use environment.rb to programmatically edit the crontab. I just added the following to the end of environment.rb:

current_cron = `crontab -l`
rails_cron_jobs =< # m h dom mon dow command
0 18 * * mon-fri /var/www/bart/script/runner -e production 'Report.cache'
0 22 * * mon-fri /var/www/bart/script/runner -e production 'Patient.update_defaulters'
EOF
unless current_cron.match(/Report\.cache/)
puts "Adding #{rails_cron_jobs} to current cron: #{current_cron}"
puts `echo "#{current_cron}\n#{rails_cron_jobs}" | crontab -`
end


It is a total hack, but I like it. We read in the current crontab - see if we have already edited it - if not we edit it. Plain and simple stuff. Now, as we say in Malawi - somebody school me as to why this is evil bad and wrong.

TED gives Africans free laptops

I just found this article from TED. It seems like they are really committed to bringing technology to Africa:


TED and the sponsors behind the conference made an amazing commitment to including innovative young African leaders, thinkers and entrepreneurs into the TED Global conference. Of the 450 attendees at the conference, 100 are “fellows”, here through the generosity of GE, Google and AMD. That generosity took an unexpected extra step this morning, when TED staffer Tom Reilly announced to the assembled fellows that Google and AMD would be donating a new Mac or PC laptop to all fellows, and that Noah Samara from Worldspace would be giving each fellow a satellite radio and an annual subscription.


And it seems like William has now been elevated to celebrity status:


William Kamkwamba, the amazing young Malawian engineer who built his first windmill at age 14, has captured the imagination of many of the people in the crowd. A number of TED attendees have banded together to support him fiscally to complete his high school education and go onto university. A TED staffer is travelling to Malawi next week to start working on finding tutors for William to help prepare him to attend a top high school in Malawi.

Some members of my blogging community, including Ndesanjo Macha, have committed to coaching William… and critically, in coaching TED on how to provide William with help and support without overwhelming him or uprooting him from his family or community. Friends like Ndesanjo have had the experience of growing up in rural communities and moving to huge cities to pursue their education - I hope that he can help TED support William in a way that’s as constructive as possible.

Malawian windmill maker gets standing ovation at TED

Remember William Kamkwamba and his homemade Windmill made from bicycle parts?

After I made that post, Emeka Okafor invited William to attend the TED conference in Tanzania. Soyapi (who is also there) just irc'd me to say that William gave a presentation and received a standing ovation. People like Bono, Larry Page, and lots of other movers and shakers are there.

Congratulations to William!

Finding where ubuntu installs programs

Whenever you install something in Ubuntu it unpacks a .deb file and saves it in the right location. The problem is that I never know the contents of a given package and where each file will end up on my machine.

The answer is dpkg -L .

So for instance, I just did:

apt-get install ktouch

and then used

dpkg -L ktouch

to figure out where the tutorial file got saved to.

(Ktouch is a typing program - we just had a little competition in the office to determine who gets what swag from the OpenMRS conference I just attended.)

How to record and convert real audio streams to mp3

I came up with this one a long time ago - its a nice little hack. It requires mplayer (and the right codecs) and lame. It might work on windows if you have cygwin installed. It should work with any type of stream that mplayer can play, not just real audio streams.

This approach uses a fifo virtual file to make sure that you don’t waste disk space storing raw audio data. It just pushes the audio data into the fifo, and lame pulls it out all asynchronously.


mkfifo soundpipe
lame -b 64 soundpipe filename.mp3 & mplayer -quiet -ao pcm -aofile soundpipe URL_OF_STREAM

Lighttpd on ubuntu

Lighttpd is a great web server. It manages resources frugally, and still manages to be fast. If you have a dedicated super duper server, lighttpd is probably not for you. But perhaps you need your machine to do more than just be a web server, or perhaps you are using a virtual dedicated server (vds, vps, whatever they are called - I use slicehost which is great and inexpensive) which means you have a limited amount of ram and cpu. In that case, lighttpd is a great choice for your web server. The main downside, is that it isn't quite as plug and play as apache is. Apache pretty much runs php out of the box, and installing phpmyadmin just works. There are plenty of guides for getting rails to work with lighttpd, but I couldn't find anything very useful for lighttpd and php (especially with fast-cgi). At any rate, I figured out how to set it in Ubuntu (dapper) and figured I would pass share the experience:

Install php with fast-cgi and lighttpd


sudo apt-get install php4-cgi lighttpd
sudo lighty-enable-mod (then select fast_cgi)

sudo gedit /etc/lighttpd/conf-enabled/10-fastcgi.conf


And change the port line to be this instead: (this seems to be a bug in the default ubuntu configuration)

socket => "/tmp/php.socket"


Now restart lighttpd

sudo /etc/init.d/lighttpd reload


Final step: Take over world


Get phpmyadmin to work with lighttpd
(The following steps assume that you have done part 1 properly)

sudo apt-get install phpmyadmin php4-mysql
sudo gedit /etc/lighttpd/lighttpd.conf


And add an alias for phpmyadmin


alias.url = ("___invalid .... other stuff here
"/phpmyadmin/" => "/usr/share/phpmyadmin" # this is the line to add
)


Reload the server:
sudo /etc/init.d/lighttpd force-reload


(might also need to edit /etc/php.ini and enable mysql, but it might happen automatically too)

Quick and dirty debug logging in ruby on rails

Typically I just to render :text=> "Yo the value is #{debug_me}" and return

But sometimes this doesn't work. Maybe you are in a model file, or a method that gets returned to another method. If that is the case you can always get some debug by raising an exception:

raise "Yo the value is#{debug_me}"

Since exceptions can come from anywhere this will always works. There are other options, like using logger which works pretty well too - but raising an exception is a nice debug hack for your toolkit.