Archive for the ‘Linux’ Category

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).

Glade auto connect in JRuby

Sunday, November 15th, 2009

I have been playing a lot around with JRuby lately (since I was scared away from IronRuby by another lame Microsoft-patent). I have been really disappointed by the Gtk-implementation in Java – it really misses a lot of the good stuff from PHP-GTK – like the auto-connect functionality.

I decided I wanted to write my own Glade Auto Connect functionality to learn some more about JRuby – it turned out a bit harder than I expected. In the end I got it working with some of the basic widgets (GtkButton::Clicked, GtkWindow::destroy, GtkTreeViewSelection::changed and so on). I am adding support for more events as I continue my adventure into JRuby.

It works like this – when you want to spawn a new Glade-object, you do it through my function:

class MyWindow
  def initialize
    @glade = GladeAutoConnect("gladefile.glade", "window", self)
  end

  def on_window_destroy
    print "The window was closed!\n"
  end
end

No more connecting events manually – just write the event-names in the Glade-application directly. This functionality really shortens the code – which is what I like about Ruby: Less code to write. Right now it produces some eval-code though, which I plan to remove, when I learn more about how to dynamically make new classes in Ruby.

The code can bee seen here:
http://knjrbfw.kaspernj.org/jruby_gtk.rb

Upgraded to Karmic!

Friday, October 30th, 2009

I finally upgraded to Karmic. This is the first time I have not been part of the beta or alpha.

Still, I noticed some problems. First of all, I still use KDE 3 – I am still scared of KDE 4 since the initial unstable release and dont dare using it… yet.

I had no items in KControl what so ever. I solved this by installing “kdebase-kde3″ and “kpersonalizer-kde3″ and executing this command:

sudo ln -s /opt/kde3/etc/xdg/menus/kde-settings.menu /etc/xdg/menus
sudo ln -s /opt/kde3/etc/xdg/menus/applications-merged/kde-essential.menu /etc/xdg/menus/applications-merged/kde-essential.menu

Further more I could configure the CPU frequency applet in Gnome to change frequency without requesting my password. This can be solved by entering this:

gksudo gedit /usr/share/polkit-1/actions/org.gnome.cpufreqselector.policy

Scroll to the end of the file and change the “…admin_auth_passwd” (or something) to “yes”.

This post serves mostly as my own personally note for the future :-)

PHP 5.3 and PHP-GTK packages

Wednesday, July 8th, 2009

Some days ago I compiled and packaged PHP 5.3 and the GTK-extension. I also put this in my Ubuntu-repository (i386 only):

http://wiki.kaspernj.org/index.php/KnjUbuntuRepository

You can install it like this:

sudo aptitude install php53-cli php53-gtk2

You can then start you PHP-GTK-scripts like this:

php53-gtk myscript.php

You can also start it without the Gtk-extension with:

php53 myscript.php

Notice all the extensions has to be loaded through dl() og the “php.ini” which is placed in “/opt/php53/lib/”. Also the SQLite3 is built static, where all the other extensions are build shared.

Happy hacking…

knjInterface

Wednesday, April 15th, 2009

When I switched to Debian some time ago on the OpenMoko, I decided to program my own small interface for opening applications and doing basic stuff on it.

As of tonight, it can actually receive calls as well by listening to and sending dbus-events. If will, of course, also make some software to make calls and to handle SMS.

I will post some screenshots at the wiki, if anyone is interested. The page is located on this url:

http://wiki.kaspernj.org/index.php/KnjInterface

knjMokoEmail

Tuesday, April 14th, 2009

I just finished the first version of knjMokoEmail, which is, as the name suggests, an application for accessing email-accounts from the OpenMoko.

Currently it only works on OpenMoko Debian, because I have not been able to compile PHP5-IMAP for OM2008 or SHR (I have really tried though).

The application is currently able to sync emails down to a SQLite3-database, search for emails, show the latest emails and of course read emails.

It is possible to set up multiple accounts with IMAP, IMAP-SSL, POP3 and POP3-SSL.

Trying out Debian for the OpenMoko again

Sunday, April 12th, 2009

While I have been playing with my OpenMoko, I have spent so many ours trying to compile or cross-compile stuff for the OpenMoko… Time I would much rather spent coding stuff.

Today I decided to try out Debian again. I dont really like Zhone (crashes, windows overlaps each other – just annoying), so I decided to quickly code my own very simple and kind of ugly interface-application.

After using some hours on it, I am very produ of the result. It cant read and sort all applications under “/usr/share/applications” and start them if choosen. I even got it to start with X.

I set up a small OpenMoko Debian repo, where the application will be available for anyone interessted soon.

Also, the third OpenMoko phone has been discontinued! Wtf? This is just another reason why not to use to official OpenMoko software. If they stop their future development, I can always port my work to another phone running Debian – like HTC Dream or HTC Magic.

Contacts and messages in the OpenMoko

Monday, March 23rd, 2009

I finally found some good documentation on the DBUS-stuff for the OpenMoko. I wrote two frameworks for manipulating with both contacts and messages.

I have made simple functions to add new contacts, edit contacts, delete contacts, list contacts, list messages and delete messages.

Hopefully I will be able to use this in some kind of way in my email and calendar applications.

I remember back when I wrote J2ME (Java)… It was a hell to find out how to list contacts on Nokia phones… Thank you OpenMoko – even though the Python scripts is a bit slow to react ;-)