Posts Tagged ‘ruby’

Handeling retries, timeouts and errors in Ruby

Saturday, August 28th, 2010

I have now coded many apps that uses SOAP, Rest and normal HTTP/Regex-parsing. The problem in many of these apps, is that the system I am communicating with sometimes timeouts or returns invalid XML, which makes my scripts crash or at least very sad.

I then came up with a very handy-retry class, which implements the Timeout-class from Ruby’s standard-lib and extends it with being able to retry the given operating catching specific or all exceptions.

The problem is that every time you just want et to retry your code-block, you have to write a whole new loop, keep count of the times you have retried, implement the timeout-functionality, and even though this is properly between 10 to 20 lines of code in Ruby, it is very annoying – and lets face it, you should not write the same code three times.

To use this retry-functionality, you can do something like this:

count = 0
Knj::Retry.try(:tries => 5, :timeout => 3, :errors => [RuntimeError]) do
   count += 1
   print "Try #{count.to_s}\n"
   raise "Test"
end

In my opinion the script pretty much speaks for itself. It will try to run the block 5 times, if the block takes more than 3 seconds to execute it will try again, if the block raises an error of the class RuntimeError, it will be caught and the block will then be tried again.

It it hits the maximum executions, the exception will actually be thrown and not caught. If “:errors” is not given, it will catch all errors except Interupt and SystemExit (though you can give it a :exit => false to not catch the SystemExit).

This way of handling retries and timeouts makes it much more safe for me to write SOAP-, Rest- and HTTP/Regex-parsing scripts – and have drastically reduced my error reports from these scripts to almost zero.

If you want to have look at the code the Knj::Retry-class, visit “knjrbfw” at Github.com here:

http://github.com/kaspernj/knjrbfw/blob/master/retry.rb

Ruby-GTK behavior in JRuby-GTK (using Java’s GTK)

Friday, June 25th, 2010

When I first started playing around with JRuby, I quickly found out, that the GTK-extension for Ruby did not work with JRuby, since it was a native C-extension, which could not be loaded into Java.

To load GTK in JRuby anyway, I had to install the Java GTK on my Ubuntu machine. This is pretty easy – just fire off a “sudo aptitude install libjava-gnome-java” and your done…

Java’s GTK is a lot different from the Ruby version. First of all a lot of the Ruby-objects has optimized constructors. You can construct a window with a title by doing:

window = Gtk::Window.new("The title")

In Java this is not possible, so if you give the argument your app fails. This meant that JRuby wasnt able to handle some of my GTK-apps. I then decided to write a kind of wrapper, which would emulate the behavior of the Ruby-GTK in JRuby-GTK (Java’s GTK). This turned out to be a lot harder than I first thought.

First off I had to make dynamic events-classes for the connect-method, to first do it the Java-way and then call blocks in Ruby to turn it into the Ruby way – which I had never done before.

All in all I was able to get the following examples to work in JRuby with Java’s GTK loaded:

require "knj/jruby-gtk2/gtk2.rb"
require "knj/jruby-gtk2/gladexml.rb"

class WinAppEdit
	def initialize
		@glade = GladeXML.new("win_app_edit.glade"){|h|method(h)}
		@glade["window"].show_all
	end

	def on_btnSave_clicked
		print "Save clicked.\n"
	end

	def on_btnCancel_clicked
		print "Cancel clicked.\n"
	end

	def on_window_destroy
		print "Destroyed!\n"
		Gtk.main_quit
	end
end

WinAppEdit.new

Gtk.main

And the normal one:

require "knj/jruby-gtk2/gtk2.rb"

button = Gtk::Button.new("Test")
button.connect("clicked") do
	print "Clicked!\n"
end

win = Gtk::Window.new("Trala")
win.add(button)
win.show_all

win.connect("destroy") do
	print "Destroy!\n"
	Gtk.main_quit
end

Gtk.main

Why go through all of this, just so my Ruby-GTK applications can run through JRuby? Scalability, real threads, packaged Java-apps and so on…

The whole thing can be checked out at my account on GitHub:

http://github.com/kaspernj/knjrbfw

http://github.com/kaspernj/knjrbfw/blob/master/jruby-gtk2/gtk2.rb

http://github.com/kaspernj/knjrbfw/blob/master/jruby-gtk2/gladexml.rb

Be warned that this project is far from done – its actually more a proof of concept for myself :-)

Ruby and GTK on the N900

Sunday, January 10th, 2010

I finally compiled Ruby and GTK packages for the N900. It was a very big disappointment, when I found out that neither the official or the extras repositories had any ruby-gtk packages – even though Ruby itself had been packaged (but not optified).

Because of this, I decided to compile my own Ruby packages, which I have just tested on my phone (I cross-compiled them on my laptop).