Progress bar for migration

Snail on wall
Probably it is time to replace my notebook with new one, but before this will happen I need to deal with what I got ;) So problem is with migrations. If they just add/remove database fields it is no problem. But recently I often do a lot computations on my data sets in those. Not that those sets are huge, but staring on console window waiting as those operations end is somewhat frustrating.

Buying new notebook probably would help, but there are some low end solutions. Not for time for those operations, but for lack of feedback.

So welcome to Simple Migration Progress Bar:

#!/usr/bin/env ruby

STDOUT.sync=true
cnt = 0
width = 60
total_cnt=200

while cnt < total_cnt do
  cnt += 1
  prgrs =(cnt.to_f/total_cnt*width).to_i+1
  prgrs = width -1 if prgrs >= width
  str = "|%#{prgrs}s%#{width-prgrs}s\r"
  printf str ,">","|"
  sleep 0.05
end

Ugly code, seems soooo C-ish. Maybe someone will write more Ruby version?

Changing domain for Rails application

Often we have to change domain on which does our Rails application work. Usually it is no problem, since we can run application accessible under both URLs and wait for Google to index new URLs.

But it is not always as easy. For example Google Maps API key is bound to application URL, so with URL change we need change Google Maps API key too. And serve proper one depending on which URL was used. So more logical solution is to force all requests coming to old URL to be redirect with HTTP 301 to proper URL. It is also better since we won’t get penalty from Google for duplicated content, and when we turn off the old domain smaller part of traffic will be lost (HTTP 301 mean that search engine should change URL pointing to page, since it is Moved Permanently)

WWW home page
Image from SXC.hu

In most cases You can do it with redirect feature from webserver like NGiNX, Apache or whatever You do use. However it mean You has to use scary regexps and not always is easy to modify webserver config (very common in corporate environment ;)) )

In such case You has to built this into Rails application. You can do it with following code in app/controllers/application.rb:

 def redirect_domain
    uri = URI.parse request.url
    if uri.host != CONF[:main_host]
      uri.host = CONF[:main_host]
      headers["Status"] = "301 Moved Permanently"
      redirect_to uri.to_s
      return
    end
    return true
  end

and add before_filter :redirect_domain. Of course You have to define CONF[:main_host] as proper hostname for Your service (without any http:// or trailing slashes, just domainname.com). You should be aware, that it does not affect any static HTML file, since they are served by webserver, without calling Rails (this includes pages cached via page caching).

This is very simple solution and does not deal with situation if Your application uses some sub domains.

Does anybody know other solution for easy domain change on live application?

Run-N-Share is still alive!

I was not posting in RNS category from long time, but this does not mean I have abandoned RNS, no way!

I have recently deployed new code base version. From user perspective there is no much changes. Most notable, RNS now calculates and displays route length during route creation and edition. So You can use Run-N-Share if You want to measure exact route length. How to do it? Just go to creation page start drawing route and it current length will be displayed on the right.

From other plans I can say, that I have used my talk on RuPy 2008 to start work on feature I planed to add long time ago. It is Facebook application for RNS! As for now it is not fully ready, but I plan to finish it before end of April.

I can not give any dates, but very obvious feature for site like RNS is some integration with GPS devices. There is chance that some support will be available, since I have discovered GPSBabel, software for converting files with data from GPS devices to different formats. It looks like I don’t need to worry how to parse many file formats – GPS support seems much easier to be built into RNS. However – I need to make some test if GPSBabel really will help me.

RuPy 2008

This year I was able to attend RuPy on two days, not like year ago. It makes huge difference ;) Pozna? by night with Ruby and Python zealots was remarkable experience.

Talking about experience – I need to tell what happened when I was returning from Pozna? to Warsaw. I was on RuPy by car, so after my talk (which was last one – I suggest You never agree to be the last speaker :) ) I jumped in and was heading to Warsaw. Just after I left Pozna? I did something unusual for me – I picked up a hitchhiker. He has shown me few shortcuts, which made my journey much easier. What is special in that? This guy, Wiktor Racinkowski was born blind. I was astonished by his courage and openness. This trip was for him something normal, as he said, he is traveling that way since late 70-s, so it make almost 30 years.

He has phenomenal memory – told me about his journeys with very detailed routes, dates and a lot of stories… I guess he is able to remember route after he go through it two times. Incredible.

If You do speak Polish You can listen to this reportage about hitchhiking from Polish public radio, where Wiktor is featured.

Returning to more geeky things – the whole experience of RuPy was awesome. I have met a lot new very interesting people and hope that from those contacts something will be born – maybe finally hack fest in Warsaw become reality – keep Your fingers crossed for this idea :)

My talk was how to write application using Rails and Facebook platform – it should be available on YT, but hard to say when – I will post a link when I got it.